Uglifyjs无效的选项CacheKeys并缩小

时间:2018-08-05 05:29:10

标签: uglifyjs webpack-4

UglifyJs插件中没有此选项的原因吗?我正在按照https://webpack.js.org/plugins/uglifyjs-webpack-plugin/ **

进行文档处理
  

ValidationError:UglifyJs插件无效选项

     

options ['ecma']是无效的附加属性   options ['cacheKeys']是无效的附加属性   options ['minify']是无效的附加属性

package.json

"terser": "^3.8.1",
"uglifyjs-webpack-plugin": "^1.2.7",
"webpack": "^4.16.3",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5",
"webpack-hot-middleware": "^2.22.3"

webpack.config

minimizer: [
      new UglifyJsPlugin({
        // Uncomment lines below for cache invalidation correctly
        ecma:6,
        cache: true,
        cacheKeys:(defaultCacheKeys,file) =>{
          return Object.assign(
            {},
            defaultCacheKeys,
            { terser: require('terser/package.json').version },
          );
        },
        minify:(file, sourceMap) => {
          // https://github.com/fabiosantoscode/terser#minify-options
          const terserOptions = { parse: {
                                        // parse options
                                    },
                                    compress: {
                                        // compress options
                                        "ecma": 6
                                    },
                                    mangle: {
                                        // mangle options

                                        properties: {
                                            // mangle property options
                                        }
                                    },
                                    output: {
                                        // output options
                                    },
                                    sourceMap: {
                                        // source map options
                                    },
                                    ecma: 5, // specify one of: 5, 6, 7 or 8
                                    keep_classnames: false,
                                    keep_fnames: false,
                                    ie8: false,
                                    module: false,
                                    nameCache: null, // or specify a name cache object
                                    safari10: false,
                                    toplevel: false,
                                    warnings: false };

          if (sourceMap) {
            terserOption.sourceMap = {
              content: sourceMap,
            };
          }

          return require('terser').minify(file, terserOptions);
        }
      })
    ]

2 个答案:

答案 0 :(得分:0)

新的UglifyJsPlugin 期望键为 uglifyOptions 的对象在此处丢失。请按照以下说明更新webpack配置。

[
  new UglifyJsPlugin({
    uglifyOptions: {
      ecma: 8,
      ...
    }
  })
]

答案 1 :(得分:0)

我遇到了同样的问题。我的错误是我没有遵循 symfony 中的命名规则。

assetic:
    filters:
        uglifyjs:
            # the path to the uglifyjs executable
            bin: /usr/local/bin/uglifyjs 

我通过将过滤器别名从 uglifyjs 更改为 uglifyjs2 来解决

assetic:
    filters:
        uglifyjs2:
            # the path to the uglifyjs executable
            bin: /usr/local/bin/uglifyjs