我正在使用 webpack 并想要部署我的网站。如果我缩小并捆绑 JavaScript 代码,则会出现此错误:
Parse error: Unexpected token: name (
Button
)
这是我未捆绑的代码:
'use strict';
export class Button { // <-- Error happens on this line
constructor(translate, rotate, text, textscale = 1) {
this.position = translate;
this.rotation = rotate;
this.text = text;
this.textscale = textscale;
}
}
请注意,在捆绑代码中,关键字 export
已被删除。在开发过程中,没有抛出任何错误。在这里你可以找到我的WebPack配置文件:
var webpack = require('webpack');
var PROD = true;
module.exports = {
entry: "./js/entry.js",
output: {
path: __dirname,
filename: PROD ? 'bundle.min.js' : 'bundle.js'
},
module: {
loaders: [
{
test: /\.css$/,
loader: "style-loader!css-loader"
}
]
},
plugins: PROD ? [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
output: {
comments: false,
},
})
] : []
};
如果我将 PROD
更改为 false,则不会出现错误,如果为 true,则上面会出现错误。我的问题是我可以在 Webpack 中启用 ES6 吗?
最佳答案
不确定您是否仍在寻找此问题的答案,但现在您可以包含 uglifyjs-webpack-plugin 的测试版作为 webpack 插件,它将使用 uglify-es可以缩小 ES6 代码。
npm install uglifyjs-webpack-plugin
然后在你的 webpack.config.js 中:
const Uglify = require("uglifyjs-webpack-plugin");
module.exports = {
entry: ...,
output: ...,
plugins: [
new Uglify()
]
};
关于webpack - 如何使用 Webpack 缩小 ES6 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44287584/