尝试运行Webpack生产版本时出现错误。
node_modules/webpack/bin/webpack.js -p --config webpack.config.production.js
...
ERROR in app.js from UglifyJs
Unexpected token: keyword (const) [app.js:18757,0]
...
查看此错误,我了解到这很可能是ES6代码找到进入uglifyjs-webpack-plugin
但是如果我有babel安装程序将代码转换为es5,为什么会这样呢?
.babelrc
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "6.10",
"esmodules": true
}
}
]
],
"plugins": ["@babel/plugin-proposal-object-rest-spread"]
}
webpack通用配置
module.exports = (env, options) => ({
...
module: {
rules: [
{
test: /\.js$/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"]
}
},
exclude: /node_modules/
},
...
还有我的产品配置
...
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
...
const webpackCommonConfig = require("./webpack.config.common")();
module.exports = merge(webpackCommonConfig, {
mode: "production",
optimization: {
minimizer: [
new UglifyJsPlugin({ cache: false, parallel: false, sourceMap: false }),
...
]
}
});
我在这里的假设是,babel将在优化运行之前将js转换为es5。我想念什么?
这是我写完这篇文章后才发现的有趣发现。 https://babeljs.io/docs/en/babel-preset-env#forcealltransforms
尽管
注意:targets.uglify已过时,将在下一个主要版本中予以删除。