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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With