Webpack V4:使用Webpack&删除console.logs。丑化

时间:2018-03-04 22:24:16

标签: webpack compression uglifyjs

这个答案之前就像一个魅力:

https://stackoverflow.com/a/41041580/3894981

然而,由于Webpack v4它不再起作用了。从那以后它抛出:

  

错误:webpack.optimize.UglifyJsPlugin已被删除,请使用   相反,config.optimization.minimize。

为了使其在Webpack v4中运行,有什么必要?

我已经尝试过使用以下内容而没有运气:

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

if (process.argv.indexOf('-p') !== -1) {
  // compress and remove console statements. Only add this plugin in production
  // as even if drop_console is set to false, other options may be set to true
  config.plugins.push(new uglifyJsPlugin({
    compress: {
      'drop_console': true
    }
  }));
}

2 个答案:

答案 0 :(得分:10)

你仍然把它放在config.plugins中,你试过把它放在config.optimization.minimizer中吗?

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

...

optimization: {
  minimizer: [
    new UglifyJSPlugin({
      uglifyOptions: {
        compress: {
          drop_console: true,
        }
      }
    })
  ]
}

答案 1 :(得分:1)

这不再适用于Webpack 4,需要使用Terser插件,请参见https://stackoverflow.com/a/41041580/378506