Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do any of these javascript minifiers remove debug statements?

It would be nice if I could put debug/console log statements in my javascript, then have a js minifier/compacter remove these when it compiles.

Does this exist?

like image 780
Blankman Avatar asked Aug 01 '11 22:08

Blankman


1 Answers

Not that I'm aware of, though Google's Closure Compiler will allow you to accomplish something similar if you wish:

/** @define {boolean} */
var DEBUG_MODE = true;

var debug;
if (DEBUG_MODE) {
    /** @param {...} args */
    debug = function(args) { console.log.apply(console, arguments); }
} else {
    /** @param {...} args */
    debug = function(args) {}
}

debug('foo', {a: 5});

If you set DEBUG_MODE to false (you can even do this on the command-line if you wish), then when advanced optimizations are enabled (you must do some work if you want to use these, but they're handy), the empty implementation of debug will be used instead, which will cause calls to it to be "inlined" (optimized out of existence).

You could extend this to have more complicated debugging functions than the above (which simply forwards to console.log); for instance you could make one that accepted a function argument that is called in debug mode, and not called outside of debug mode.

like image 70
Jeremy Roman Avatar answered Sep 29 '22 13:09

Jeremy Roman