javascriptgrunt-contrib-uglify

Preserve 'debugger' keyword grunt uglify


I'm developing a library in JS and I want to preserve 'debugger' keyword after using grunt-contrib-uglify because is intentional usage, but default behavior (obviously) is delete all debuggers.

My grunt file:

module.exports = function (grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        uglify: {
            CrackerTrapProductionMinJS: {
                options:{
                    banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
            '<%= grunt.template.today("yyyy-mm-dd") %> */\n',
                drop_console: true,
                mangle: {
                    reserved: ['debugger']
                  }
                },
                files: {
                    './build/cracker-trap.min.js': './build/cracker-trap.ob.js'
                }
            }
        },          
    });

    grunt.loadNpmTasks('grunt-contrib-uglify');

    grunt.registerTask('default', ['uglify']);
};

And the method that I want to preserve the debugger keyword to validate if user has developer tools opened:

function timeValidation() {
    var startTime = new Date();
    debugger;
    var endTime = new Date();

    return endTime - startTime > 100;
}

Complete code in: https://github.com/bioverflow/cracker-trap


Solution

  • Looks like you just have to specify the compress options object inside your grunt-contrib-uglify setup section, and set drop_debugger to false as is shown here

    So your setup should be something like this:

    uglify: {
      CrackerTrapProductionMinJS: {
        options:{
          banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %> */\n',
          drop_console: true,
          compress: {
            drop_debugger: false
          },
          files: {
            './build/cracker-trap.min.js': './build/cracker-trap.ob.js'
          }
        }
      }
    }
    

    Though I don't have gulp running so, I cannot verify