简体   繁体   English

UglifyJS webpack 插件抛出:意外的令牌:名称(功能)

[英]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.

相关问题 Webpack UglifyJS运行到意外令牌中 - Webpack UglifyJS running into unexpected token 意外的令牌名称(PubNubAngular)UglifyJs - Unexpected token name (PubNubAngular) UglifyJs UglifyJs 意外标记:关键字 «const» Webpack 4 - UglifyJs Unexpected token: keyword «const» Webpack 4 来自UglifyJs的bundle.js中的webpack错误-ERROR意外的令牌:name(urlParts) - webpack error -ERROR in bundle.js from UglifyJs Unexpected token: name (urlParts) UglifyJS 抛出意外标记:keyword (const) with node_modules - UglifyJS throws unexpected token: keyword (const) with node_modules Webpack抛出意外的令牌错误 - Webpack throws Unexpected token error UglifyJs意外令牌错误 - UglifyJs unexpected token error UglifyJS2每次都会解析错误(意外的令牌:名称(Abide)) - UglifyJS2 thows parse error (Unexpected token: name (Abide)) everytime 错误 - [webpack] 'dist':来自 UglifyJs 的 list-documents-web-part.js 意外令牌:名称(样式)[list-documents-web-part.js:73666,7] - Error - [webpack] 'dist': list-documents-web-part.js from UglifyJs Unexpected token: name (style) [list-documents-web-part.js:73666,7] uglifyjs是一个javascript类 - 意外的令牌错误 - uglifyjs a javascript class - unexpected token error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM