Search code examples
javascriptnode.jsgruntjsuglifyjsgrunt-contrib-uglify

Conditionally removing DEBUG during uglifyjs task on Grunt


I would like to remove my DEBUG statments at build, and I noticed on http://lisperator.net/uglifyjs/compress I can define global_defs: { DEBUG : false } to remove anything wrapped in a debug clause if (DEBUG) {}

My uglify task doesn't seem to remove any DEBUG section, any thoughts on what I'm doing wrong?

I'm using grunt-contrib-uglify v0.3.3

Here is my grunt task: ...

uglify: {
   ...
   simple: {
       options : {
          mangle: false,
          compress: {
            global_defs: {
              DEBUG: false
            },
            dead_code: true
          }
       },
       files: {
          'yayMin.js' : [ ..., somefile.js, ... ]
       }
   },...
...

somefile.js

...
    if (DEBUG) {
       console.log('epic fail - fix your build');
    }
...

Solution

  • Also check out grunt-groundskeeper which removes pragmas written like this:

    // <debug>
    doSomething();
    // </debug>
    
    // <validation>
    performSomeValidationOnlyNeededDuringDevelopment();
    // </validation>
    

    It also has options to remove calls to console.log() and debugger; statements.