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.
There's also another option called drop_console which has been added recently (late 2013)
This is added to the grunt init config like this:
As taken from the grunt-contrib-uglify github documents
In the lastest uglify-js ( v2.4.3), a new compress option ‘pure_funcs’ is added. If I add the console.log functions to this array, it will be removed in the minified js file. The test code below shows how this option works. This is exactly what I want.
Quotes from https://github.com/mishoo/UglifyJS2
If you are using gulp, use 'gulp-strip-debug' to remove console, alert and debugger. https://www.npmjs.com/package/gulp-strip-debug
You can use Groundskeeper to do this though it will be a separate step.
for -c option, set drop_console to be true:
uglifyjs app.js -m -c drop_console=true -o app.min.js