覆盖Webpack 4.25.1在生产模式下对Uglify的使用

时间:2018-11-13 19:37:34

标签: javascript reactjs webpack webpack-4

根据文档,当module.exports = { mode: 'production' }时,默认情况下它使用UglifyJS:

https://webpack.js.org/concepts/mode/#mode-production

据我了解,该库已被废弃,并且“充满了错误”。建议改用terser

https://github.com/terser-js/terser

对于该特定插件,如何覆盖mode: 'production'的默认用法?仅将terser添加到plugins: []就足够了吗?

1 个答案:

答案 0 :(得分:1)

确定在文档中找到答案:

optimization.minimzer

  

通过提供一个或多个自定义的UglifyjsWebpackPlugin实例,使您可以覆盖默认的最小化程序。

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  //...
  optimization: {
    minimizer: [
      new UglifyJsPlugin({ /* your config */ })
    ]
  }
};

您只需将所有UglifyJsPlugin的内容替换为terser

const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
  //...
  optimization: {
    minimizer: [new TerserPlugin()]
  }
};

https://webpack.js.org/configuration/optimization/#optimization-minimizer