Captain Obvious
Captain Obvious

Reputation: 785

uglifyjs drop_console / pure_funcs is not working in webpack

I have the following configuration, but it still does not remove console.log statements:

new webpack.optimize.UglifyJsPlugin({
    compress: {
        warnings: false,
        pure_funcs: ['console.log'],
        drop_console: true,
        comments: false
    },
    pure_funcs: ['console.log'],
    drop_console: true,
    comments: false
})

What am I doing wrong?

Upvotes: 4

Views: 4326

Answers (3)

frustigor
frustigor

Reputation: 402

It is not the reason of uglifyjs in my case. It is caused by babel-loader which will transform console.log to (e = console).log.

I do not know how to fix it now. Finally, I have to use a babel plugin named babel-plugin-transform-remove-console to remove console.

However I do want to use UglifyJsPlugin.

This is a hint for those who can find out a resolution.

Upvotes: 1

mp31415
mp31415

Reputation: 6689

I had the same problem with drop_console not working in my react script setup (on Windows 10, React-script version 0.8.5).

Trying to reproduce this problem I created a brand new app, added console.log somewhere in App.js and drop_console: true in webpack.config.prod.js. However in this simple setup drop_console works and console.log is removed.

As it still didn't work in my real app I installed strip-loader:

npm install --save-dev strip-loader

then edited webpack.config.prod.js in node_modules\react-scripts\config (without ejecting from react):

var WebpackStrip = require('strip-loader'); // around line 20
...
// inserted in module/loaders between babel and style loaders, around line 168
  { 
    test: /\.js$/, 
    loader: WebpackStrip.loader('debug', 'console.log')
  },

Sure enough, all console.log statements were removed (npm run build). I then removed all my changes from the config and console.log were still being removed. I uninstalled strip-loader and the build still successfully removes console.log statements.

I cannot explain this behaviour, but at least it works (although somewhat magically).

Upvotes: 0

Jamie S
Jamie S

Reputation: 78

It's possible that the messages you are getting are debugging messages in the console, rather than console.log. I had a similar issue where I thought using drop_console would suffice. I had to add drop_debugger as well, so given your example, this should remove all console output.

new webpack.optimize.UglifyJsPlugin({
  compress: {
    warnings: false,
    drop_console: true,
    drop_debugger: true
  },
  comments: false  
})

Upvotes: 2

Related Questions