简体   繁体   中英

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');
    }
...

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.

There appears to be a bug in grunt-contrib-uglify that only removes the global_defs from one file. I noticed DEBUG statements would be removed, as requested by options, if I reduced my files to one file

Did not work:

 ...
 files: {
          'yayMin.js' : [ ..., anotherfile.js, somefile.js, anotherfile2.js, ... ]
       }
 ...

Worked:

...
files: {
          'yayMin.js' : [ somefile.js ]
       }
...

Final Grunt Task

uglify: {
'build-minify' : {
        options: {
          mangle: false,
          compress: {
            global_defs: {
              DEBUG: false
            },
            dead_code: true
          },
          wrap: true
        },
        files : {
          'justSayNoToDebug.js' : [
            'ahHaveDebugsInMe.js'
          ]
        }
      },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM