6

This answer worked like a charm previously:

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

However, since Webpack v4 it doesn't work anymore. Since then it throws:

Error: webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead.

What is necessary here in order to make it work in Webpack v4?

I've tried using the following without luck:

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
    }
  }));
}
dude
  • 5,678
  • 11
  • 54
  • 81

3 Answers3

14

You're still putting it in config.plugins, have you tried putting it in config.optimization.minimizer?

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

...

optimization: {
  minimizer: [
    new UglifyJSPlugin({
      uglifyOptions: {
        compress: {
          drop_console: true,
        }
      }
    })
  ]
}
Dominic
  • 62,658
  • 20
  • 139
  • 163
0

This no longer works with Webpack 4, need to use Terser plugin, see here https://stackoverflow.com/a/41041580/378506

sMyles
  • 2,418
  • 1
  • 30
  • 44
  • I can't agree to your statement. Just tried the accepted answer (Dominics solution) and it worked perfectly using Webpack 4. Maybe you tried the code in plugin options instead of optimization options? I did that at first before reading the whole thread :) – Luckyfella Jul 23 '19 at 13:52
0

Since there is no need for installing UglifyJSPlugin separately, you make TerserPlugin do this for you, same as @Dominic answer but in TerserPlugin options:

...
...
...
 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,
            },
          },
        }),
Mhand
  • 61
  • 3