Can uglify-js remove the console.log statements?

Jeffrey picture Jeffrey · Nov 20, 2013 · Viewed 31.3k times · Source

I'm using uglify-js to minify the source code. I want to remove the console.log statements of the original source code. Is it possible? Or is there any other compressor tool supports this?

I use the code as below in Node.js.

var uglify = require('uglify-js');
var originalSourceCode = 'var name = function(){var str = "test"; return str}; console.log("log data");';
var minifiedCode = uglify.minify(originalSourceCode, {
                fromString : true,
                mangle: {},
                warnings: true
            });
console.log(minifiedCode);

The output is:

$node m.js
{ code: 'var name=function(){var a="test";return a};console.log("log data");',
  map: 'null' }

In the minified code the console.log isn't removed.

Answer

Joshua picture Joshua · Jan 9, 2014

There's also another option called drop_console which has been added recently (late 2013)

drop_console -- default false. Pass true to discard calls to console.* functions

This is added to the grunt init config like this:

grunt.initConfig({
  uglify: {
    options: {
      compress: {
        drop_console: true // <-
      }
    },
    my_target: {
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

As taken from the grunt-contrib-uglify github documents