Search code examples
javascriptgruntjsgrunt-contrib-connect

Using Grunt uglify task to mangle JS code


I'm attempting to use the Grunt uglify task to mangle my javascript code, and while I can make minify work, I can't figure out how to pass the 'mangle' option. My Gruntfile contains:

uglify: {
  dist: {
    mangle: true,
    files: {
      '<%= yeoman.dist %>/scripts/scripts.js': [
        '<%= yeoman.dist %>/scripts/scripts.js'
      ]
    }
  }
},

...

  grunt.registerTask('build', [
    'clean:dist',
    'wiredep',
    'useminPrepare',
    'concurrent:dist',
    'autoprefixer',
    'concat',
    'ngAnnotate',
    'uglify',
    'copy:dist',
    'cdnify',
    'cssmin',
    'filerev',
    'usemin',
  ]);

With this setup, when I run "grunt build" the uglify task reports success, and my javascript is minified, but not mangled. Can anyone tell me what I'm doing wrong?


Solution

  • You need to put mangles in an options object, either at task or target level, for example:

    uglify: {
      dist: {
        options: {
            mangle: true,
        },
        files: {
          '<%= yeoman.dist %>/scripts/scripts.js': [
            '<%= yeoman.dist %>/scripts/scripts.js'
          ]
        }
      }
    },