I've been using the suggestion in http://jstarrdewar.com/blog/2013/02/28/use-uglify-to-automatically-strip-debug-messages-from-your-javascript to define my DEBUG constants in my code so that they will run before being minified. I then pass in the constant with global_defs to remove it.
This is my first attempt:
(function(){ 'use strict'; if ( typeof DEBUG === 'undefined' ) DEBUG = true; if ( DEBUG ) alert( 'DEBUG1' ); })();
But it doesn't work because in strict mode you can't set an undeclared variable.
(function(){ //'use strict'; if ( typeof DEBUG === 'undefined' ) DEBUG = true; if ( DEBUG ) alert( 'DEBUG2' ); })();
This does work, but it's not using strict mode, and the variable is leaked to global scope.
(function(){ 'use strict'; if ( typeof DEBUG === 'undefined' ) var DEBUG = true; if ( DEBUG ) alert( 'DEBUG3' ); })();
This works well, but the debug code won't be removed:
(function(){"use strict";if(DEBUG===void 0)var DEBUG=!0;DEBUG&&alert("DEBUG3")})();
Is this last example meant to be correct behaviour? Can you give any advice on how to use conditional compilation?