Search code examples
javascriptgruntjsgrunt-contrib-uglify

Stop Grunt Contrib Uglify from removing unused Javascript


There is a function within my Javascript source file that I need to keep within my distribution for posterity (specifically for a sniffer to sniff out some information). It is not being called, but it needs to stay.

The Grunt task, grunt-contrib-uglify is removing this method because it is uncalled in my application.

How can I utilize the compression provided by grunt-contrib-uglify, without removing any code that is deemed unusable by this Grunt library?

Thanks.


Solution

  • Set options: unused to false

    grunt.initConfig({
      uglify: {
        options: {
          compress: {
            unused: false
          }
        },
        my_target: {
          files: {
            'dest/output.min.js': ['src/input.js']
          }
        }
      }
    });
    

    Source: UglifyJS global definitions documentation

    unused : true, // drop unused variables/functions