[英]UglifyJS webpack plugin throws: Unexpected token: name (features)
I used to have problems with UglifyJS for Webpack and ES6 modules:我曾经遇到过 Webpack 和 ES6 模块的 UglifyJS 问题:
ERROR in static/js/vendor.6ccd9e38979a78765c7a.js from UglifyJs Unexpected token: name (features) [./node_modules/pica/lib/mathlib.js:19,0][static/js/vendor.6ccd9e38979a78765c7a.js:39003,6]
来自 UglifyJs 的 static/js/vendor.6ccd9e38979a78765c7a.js 中的错误意外令牌:名称(功能)[./node_modules/pica/lib/mathlib.js:19,0][static/js/vendor.6ccd9e38979a78765c7a.js:39003, 6]
I read that the new beta version of the Webpack plugin supports ES6:我读到 Webpack 插件的新测试版支持 ES6:
https://github.com/webpack-contrib/uglifyjs-webpack-plugin https://github.com/webpack-contrib/uglifyjs-webpack-plugin
new webpack.optimize.UglifyJsPlugin({
uglifyOptions: {
ie8: false,
ecma: 8, // I also tried 7 and 6
parse: {},
mangle: {
properties: {
// mangle property options
}
},
output: {
comments: false,
beautify: false
},
compress: {},
warnings: true
}
}),
However, now I get another error:但是,现在我收到另一个错误:
ERROR in static/js/vendor.6ccd9e38979a78765c7a.js from UglifyJs Unexpected token: name (features) [static/js/vendor.6ccd9e38979a78765c7a.js:39003,6]
来自 UglifyJs 的 static/js/vendor.6ccd9e38979a78765c7a.js 中的错误意外令牌:名称(功能)[static/js/vendor.6ccd9e38979a78765c7a.js:39003,6]
What could be the problem?可能是什么问题呢?
You can try installing babel-preset-env
and adding presets": [ "env" ]
to your webpack.config.js or babelrc
. 您可以尝试安装
babel-preset-env
并将presets": [ "env" ]
到webpack.config.js或babelrc
。
Uglify cannot parse ES6 on its own( as far as I know), so you need to transpile your code down to ES5, post-processing your generated JS with babel, or use a different minifier. Uglify无法自行解析ES6(据我所知),因此您需要将代码转换为ES5,使用babel对生成的JS进行后处理,或使用其他缩小器。 My recommendation is Babelify to which I switched after having constant errors with Uglify.
我的推荐是Babelify ,我在与Uglify一起出现错误后切换到了Babelify。
Edit: The problem might be in your new webpack.optimize.UglifyJsPlugin
declaration, There are problems with using this declaration with Webpack 3+. 编辑:问题可能在您的
new webpack.optimize.UglifyJsPlugin
声明中,使用此声明与Webpack 3+有问题。 You need to import the uglifyjs-webpack-plugin
and change plugin declaration to new UglifyJSPlugin
(example). 您需要导入
uglifyjs-webpack-plugin
并将插件声明更改为new UglifyJSPlugin
(示例)。 Here is a reference . 这是一个参考 。
Example: 例:
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
const config = {
...
plugins: [
new UglifyJSPlugin({ uglifyOptions: { ...options } })
]
}
For anyone arriving here stuck for various reasons on webpack3 and webpack.optimize.UglifyJsPlugin
:对于到达这里的任何人由于各种原因在 webpack3 和
webpack.optimize.UglifyJsPlugin
上卡住了:
For us, the answer was to let our webpack babel-loader transpile those node_modules.对我们来说,答案是让我们的 webpack babel-loader 转译那些 node_modules。 Instead of sending all node_modules through the babel loader (which isn't recommended for performance reasons), you can exclude all apart from specific packages and package name patterns like this:
不是通过 babel 加载器发送所有 node_modules(出于性能原因不推荐这样做),您可以排除特定包和 package 名称模式之外的所有内容,如下所示:
exclude: /node_modules\/(?.(react-markdown|mdast-util-.*|micromark-.*)\/).*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.