Webpack V4:使用Webpack和Uglify移除控制台日志

6

这个答案之前完美地起到了作用:

https://dev59.com/hFgR5IYBdhLWcg3wqOxW#41041580

然而,自Webpack v4以来,它不再起作用。此后,它会抛出以下错误:

Error: 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
    }
  }));
}
3个回答

14

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

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

...

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

0

我不能同意你的说法。我刚试了被接受的答案(Dominic的解决方案),在使用Webpack 4时它完美地工作。也许你是在插件选项而不是优化选项中尝试代码?在阅读整个线程之前,我也曾这样做 :) - Luckyfella

0

由于不需要单独安装UglifyJSPlugin,您可以让TerserPlugin为您完成此操作,与@Dominic的答案相同,但在TerserPlugin选项中:

...
...
...
 optimization: {
      minimize: isProduction,
      minimizer: [
        new TerserPlugin({
          minify: TerserPlugin.uglifyJsMinify,
          // `terserOptions` options will be passed to `uglify-js`
          // Link to options - https://github.com/mishoo/UglifyJS#minify-options
          terserOptions: {
            compress: {
              drop_console: true,
            },
          },
        }),

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接