Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack V4: Remove console.logs with Webpack & Uglify

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
    }
  }));
}
like image 600
dude Avatar asked Mar 04 '18 22:03

dude


People also ask

How do I get rid of the console log in Webpack?

You can use terser-webpack-plugin compress option pure_funcs parameter to selectively drop console functions and keep the ones that you want such as console.

How do you get to the console log in Webpack?

log printed in your terminal you can add a console. log inside webpack. config file. If you are not providing the webpack-dev-server a port your app should run on 80 so opening the browser there and opening the browser console and you should be able to see the "starting!


1 Answers

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,
        }
      }
    })
  ]
}
like image 179
Dominic Avatar answered Oct 20 '22 13:10

Dominic