UglifyJS webpack插件抛出:意外的令牌:名称(功能) [英] UglifyJS webpack plugin throws: Unexpected token: name (features)

查看:314
本文介绍了UglifyJS webpack插件抛出:意外的令牌:名称(功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经遇到过UglifyJS for Webpack和ES6模块的问题:

I used to have problems with UglifyJS for Webpack and ES6 modules:


静态/ js / vendor.6ccd9e38979a78765c7a.js中的错误UglifyJs
意外的令牌:名称(要点)
[./node_modules/pica/lib/mathlib.js:19,0][static/js/vendor.6ccd9e38979a78765c7a.js:39003,6]

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]

我读到Webpack插件的新测试版支持ES6:

I read that the new beta version of the Webpack plugin supports ES6:

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:


来自UglifyJs的静态/ js / vendor.6ccd9e38979a78765c7a.js中的错误
意外的令牌:名称(功能)
[static / js / vendor.6ccd9e38979a78765c7a.js:39003,6]

ERROR in static/js/vendor.6ccd9e38979a78765c7a.js from UglifyJs Unexpected token: name (features) [static/js/vendor.6ccd9e38979a78765c7a.js:39003,6]

可能是什么问题?

推荐答案

您可以尝试安装 babel-preset-env 并添加预设:[env] 到您的webpack.config.js或 babelrc

You can try installing babel-preset-env and adding presets": [ "env" ] to your webpack.config.js or babelrc.

Uglify无法自行解析ES6(据我所知),因此您需要将代码转换为ES5,使用babel对生成的JS进行后处理,或使用其他缩小器。我的建议是 Babelify ,我在与Uglify一起出现错误后切换到了这里。

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. My recommendation is Babelify to which I switched after having constant errors with Uglify.

编辑:问题可能出在你的 new webpack.optimize.UglifyJsPlugin 声明中,使用Webpack 3+的声明有问题。您需要导入 uglifyjs-webpack-plugin 并将插件声明更改为 new UglifyJSPlugin (示例)。这是参考

The problem might be in your new webpack.optimize.UglifyJsPlugin declaration, There are problems with using this declaration with Webpack 3+. You need to import the uglifyjs-webpack-plugin and change plugin declaration to new UglifyJSPlugin(example). Here is a reference.

示例:

const UglifyJSPlugin = require('uglifyjs-webpack-plugin')

    const config = {
      ...
      plugins: [
        new UglifyJSPlugin({ uglifyOptions: { ...options } })
      ]
    }

这篇关于UglifyJS webpack插件抛出:意外的令牌:名称(功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆