Preserve 'debugger' keyword grunt uglify
Asked Answered
B

1

2

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

Boyd answered 29/1, 2018 at 23:23 Comment(2)
I'm not sure if this is applicable to your setup, but in development mode, you shouldn't use uglify. Make a separate development config file to not minify code at all, and only use uglify in productionBally
@AndyRay do you realize OP wants it to remain in prod?Willett
G
5

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

Grig answered 29/1, 2018 at 23:35 Comment(6)
@CamiRodriguez I updated my post and moved the compress outside the mangle object, I think I misread the docs, as far as uglifyjs goes that is what is on their docsGrig
Is it possible to preserve a specific debugger statement, removing all others, similar to how you can preserve a specific comment with @preserve?Sideboard
@Sideboard Not that I know off, I could be wrong though, maybe raise an issue on the github page?Grig
Thanks! options: { banner: "/*some comment*/\n", compress: { drop_debugger: false }, },Hydrops
The compress property is a property of options object!Dionysus
oh, you are right @RomanGrinyov, I should probably indent that a little better :)Grig

© 2022 - 2024 — McMap. All rights reserved.