Exclude debug JavaScript code during minification Exclude debug JavaScript code during minification javascript javascript

Exclude debug JavaScript code during minification


here's the (ultimate) answer for closure compiler :

/** @const */var LOG = false;...LOG && log('hello world !'); // compiler will remove this line...

this will even work with SIMPLE_OPTIMIZATIONS and no --define= is necessary !


Here's what I use with Closure Compiler. First, you need to define a DEBUG variable like this:

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

It's using the JS annotation for closure, which you can read about in the documentation.

Now, whenever you want some debug-only code, just wrap it in an if statement, like so:

if (DEBUG) {  console.log("Running in DEBUG mode");}

When compiling your code for release, add the following your compilation command: --define='DEBUG=false' -- any code within the debug statement will be completely left out of the compiled file.


A good solution in this case might be js-build-tools which supports 'conditional compilation'.

In short you can use comments such as

// #ifdef debugvar trace = debug.getTracer("easyXDM.Rpc");trace("constructor");// #endif

where you define a pragma such as debug.

Then when building it (it has an ant-task)

//this file will not have the debug code<preprocess infile="work/easyXDM.combined.js" outfile="work/easyXDM.js"/>//this file will        <preprocess infile="work/easyXDM.combined.js" outfile="work/easyXDM.debug.js" defines="debug"/>