javascriptgruntjsgrunt-contrib-uglify

Is it possible to exclude parts of javascript with grunt-contrib-uglify?


I am using grunt-contrib-uglify for my js build process and would very much like to be able to exclude certain parts of code from the final build.

So something that would look like:

/*BuildExcludeBegin*/
Run this code during development, but don't include it in the minified.js
/*BuildExcludeEnd*/

Does anyone know if this is doable with grunt-contrib-uglify?


Solution

  • grunt-contrib-uglify does not offer that. But you can use: https://www.npmjs.com/package/grunt-strip-code

    Quote from that page:

    grunt-strip-code

    The grunt-strip-code plugin is used to remove sections of code from production builds that are only needed in development and test environments. grunt-strip-code uses start and end comments to identify the code sections to strip out. For example:

    /* test-code */
    removeMeInProduction();
    /* end-test-code */
    
    doNotRemoveMe();
    

    A use-case for this practice is to make private JavaScript functions accessible to unit tests without exposing them in production builds. This blog post goes into more detail about the concept and implementation.