How to use conditional compilation in strict mode

344 views
Skip to first unread message

Dannii Willis

unread,
Mar 27, 2013, 12:01:54 AM3/27/13
to ugli...@googlegroups.com
(From https://github.com/mishoo/UglifyJS2/issues/161, I forgot this group existed. Sorry!)

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?


Ash Blue

unread,
Apr 18, 2014, 4:20:12 AM4/18/14
to ugli...@googlegroups.com
Have you tried window.DEBUG?
Reply all
Reply to author
Forward
0 new messages