[英]Babel Loader and Webpack + React syntax error
I'm using React + Webpack and Babel Loader for my project. 我正在为我的项目使用React + Webpack和Babel Loader。
When I'm trying to generate the bundle code, the following error is thrown (for every reducer I have when using {... state}): 当我尝试生成捆绑代码时,抛出以下错误(对于使用{... state}时所拥有的每个reducer):
@ ./src/reducers/index.js 29:21-47
@ ./src/index.js
ERROR in ./src/reducers/logged_navbar.js
Module build failed: SyntaxError: C:/xampp/htdocs/scoala-de-iarna/src/reducers/logged_navbar.js: Unexpected token (8:12)
6 | let data = _.mapKeys(action.payload.data, 'id');
7 | state.navbarLogged = data;
> 8 | return { ...state };
| ^
9 | default: return state;
10 | }
11 | }
However, when testing the app on the developer build, this error is not shown. 但是,在开发人员版本上测试应用程序时,不会显示此错误。
webpack.config.js: webpack.config.js:
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'src/bundle');
var APP_DIR = path.resolve(__dirname, 'src');
var config = {
entry: APP_DIR + '/index.js',
output: {
path: BUILD_DIR,
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
include: APP_DIR,
exclude: /node_modules/,
loader: 'babel-loader',
}
]
}
};
module.exports = config;
.babelrc: .babelrc:
{
"presets" : ["es2015", "react"]
}
The object spread operator is currently still a stage 3 proposal, and not finalized. 对象传播算子目前仍是第3阶段的提议,尚未最终确定。 For this reason, it is not included in the babel core.
因此,它不包含在babel核心中。
To compile it with babel, include the stage 3 preset in your babel config : 要使用babel进行编译,请在babel配置中包括stage 3预设 :
{
"presets" : ["es2015", "stage-3", "react"]
}
Install and add "stage2" to presets 安装“ stage2”并将其添加到预设
npm install --save-dev babel-preset-stage-2
"presets" : ["es2015", "react", "stage-2"]
Install and add babel-plugin-transform-object-rest-spread to plugins 安装babel-plugin-transform-object-rest-spread并将其添加到插件
npm install --save-dev babel-plugin-transform-object-rest-spread
{ "plugins": ["transform-object-rest-spread"] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.