Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can uglify-js remove the console.log statements?

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.

like image 411
Jeffrey Avatar asked Nov 20 '13 09:11

Jeffrey


People also ask

What does Uglify do?

Uglify JS is a JavaScript library for minifying JavaScript files. To 'uglify' a JavaScript file is to minify it using Uglify. Uglification improves performance while reducing readability. Encryption: This is the process of translating data, called plain data, into encoded data.

What is the difference between minify and uglify?

Minification is just removing unnecesary whitespace and redundant / optional tokens like curlys and semicolons, and can be reversed by using a linter. Uglification is the act of transforming the code into an "unreadable" form, that is, renaming variables/functions to hide the original intent...

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.


2 Answers

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

like image 190
Joshua Avatar answered Sep 21 '22 06:09

Joshua


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.

// file: m.js var uglify = require('uglify-js'); var originalSourceCode = 'var name = function(){var str = "test"; return str}; console.log("log data" + name());'; var minifiedCode = uglify.minify(originalSourceCode, {                 fromString : true,                 mangle: {},                 warnings: true,                 compress:{                     pure_funcs: [ 'console.log' ]                 }             }); console.log(minifiedCode);  $node m.js WARN: Dropping side-effect-free statement [?:1,53] { code: 'var name=function(){var n="test";return n};',   map: 'null' } 

Quotes from https://github.com/mishoo/UglifyJS2

pure_funcs -- default null. You can pass an array of names and UglifyJS will assume that those functions do not produce side effects. DANGER: will not check if the name is redefined in scope. An example case here, for instance var q = Math.floor(a/b). If variable q is not used elsewhere, UglifyJS will drop it, but will still keep the Math.floor(a/b), not knowing what it does. You can pass pure_funcs: [ 'Math.floor' ] to let it know that this function won't produce any side effect, in which case the whole statement would get discarded. The current implementation adds some overhead (compression will be slower).

like image 24
Jeffrey Avatar answered Sep 20 '22 06:09

Jeffrey