Define your own debugging function that will wrap around console.log or anything else you want, and make sure the minifier can easily determine if it is non-op if you make it empty. After that, as soon as you comment on the body out function, most minifiers should find that there is nothing to call or embed and completely remove references to your function.
Here is an example:
(function() { function debug() { console.log(arguments) } function main() { debug(123) alert("This is production code!") debug(456) } main() })()
I put it in an anonymous function to limit the scope of the debug and did not allow it to be bound to the window - this allows the minifier to easily decide whether it is necessary or not. When I embed this code in the online GCC , I get:
(function(){function a(){console.log(arguments)}a(123);alert("This is production code!");a(456)})();
But as soon as I add // to console.log to make debug empty, GCC compiles it into:
(function(){alert("This is production code!")})();
... completely removes all traces of debugging code!
Oleg V. Volkov
source share