我试图在我的应用程序中使用对象扩展运算符。这些是我的档案。
webpack.config.js
const webpack = require('webpack')
const path = require('path');
const BUILD_DIR = path.resolve(__dirname + '/build')
const APP_DIR = path.resolve(__dirname + '/app')
module.exports = {
entry: APP_DIR + '/index.jsx',
output: {
filename: 'bundle.js',
path: BUILD_DIR,
publicPath: '/',
},
devServer: {
inline: true,
contentBase: BUILD_DIR,
port: 3333,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
}
]
},
resolve: {
extensions: [".js", ".json", ".jsx", ".css"],
}
};
.babelrc(在项目根文件夹中)
{
"presets": ["es2015", "react"],
"plugins": ["transform-object-rest-spread"]
}
的package.json
{
"name": "shoop",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"dependencies": {
"webpack": "^2.4.1"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"webpack-dev-server": "^2.4.5"
},
"scripts": {
"dev": "webpack-dev-server",
"compile": "webpack"
},
"author": "",
"license": "ISC"
}
当我尝试运行下面的代码时,我会得到一个"意外的令牌"消息:
index.jsx
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // 1
console.log(y); // 2
console.log(z); // { a: 3, b: 4 }
答案 0 :(得分:0)
将test: /\.js$/
更改为test: /\.jsx?$/