JSPM

  • Created
  • Published
  • Downloads 320173
  • Score
    100M100P100Q181256F

Minify files with UglifyJS.

Package Exports

  • grunt-contrib-uglify
  • grunt-contrib-uglify/tasks/lib/uglify
  • grunt-contrib-uglify/tasks/uglify
  • grunt-contrib-uglify/tasks/uglify.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (grunt-contrib-uglify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

grunt-contrib-uglify Build Status

Minify files with UglifyJS.

Note that this plugin has not yet been released, and only works with the latest bleeding-edge, in-development version of grunt. See the When will I be able to use in-development feature 'X'? FAQ entry for more information.

Getting Started

If you haven't used grunt before, be sure to check out the Getting Started guide, as it explains how to create a gruntfile as well as install and use grunt plugins. Once you're familiar with that process, install this plugin with this command:

npm install grunt-contrib-uglify --save-dev

Uglify task

Run this task with the grunt uglify command.

This task is a [multi task][] so any targets, files and options should be specified according to the [multi task][] documentation. [multi task]: https://github.com/gruntjs/grunt/wiki/Configuring-tasks

Options

This task primarily delegates to UglifyJS2, so please consider the UglifyJS documentation as required reading for advanced configuration.

mangle

Type: Boolean Object
Default: {}

Turn on or off mangling with default options. If an Object is specified, it is passed directly to ast.mangle_names() and ast.compute_char_frequency() (mimicking command line behavior).

compress

Type: Boolean Object
Default: {}

Turn on or off source compression with default options. If an Object is specified, it is passed as options to UglifyJS.Compressor().

beautify

Type: Boolean Object
Default: false

Turns on beautification of the generated source code. An Object will be merged and passed with the options sent to UglifyJS.OutputStream().

sourceMap

Type: String
Default: undefined

Specify the location to output the source map.

sourceMapRoot

Type: String
Default: undefined

The location where your source files can be found.

sourceMapIn

Type: String
Default: undefined

The location of an input source map from an earlier compilation, e.g. from CoffeeScript.

sourceMappingURL

Type: String
Default: undefined

The location of your sourcemap. Defaults to the location you use for sourceMap, override if you need finer control

preserveComments

Type: Boolean String Function
Default: undefined
Options: false 'all' 'some'

Turn on preservation of comments.

  • false will strip all comments
  • 'all' will preserve all comments in code blocks that have not been squashed or dropped
  • 'some' will preserve all comments that start with a bang (!) or include a closure compiler style directive (@preserve @license @cc_on)
  • Function specify your own comment preservation function. You will be passed the current node and the current comment and are expected to return either true or false

Type: String
Default: empty string

This string will be prepended to the beginning of the minified output. It is processed using grunt.template.process, using the default options.

(Default processing options are explained in the grunt.template.process documentation)

Usage examples

Basic compression

In this example, running grunt uglify:my_target (or grunt uglify because uglify is a [multi task][]) will mangle and compress the input files using the default options.

// Project configuration.
grunt.initConfig({
  uglify: {
    my_target: {
      files: {
        'dest/output.min.js': ['src/input1.js', 'src/input2.js']
      }
    }
  }
});

No mangling

Specify mangle: false to prevent changes to your variable and function names.

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

Reserved identifiers

You can specify identifiers to leave untouched with an except array in the mangle options.

// Project configuration.
grunt.initConfig({
  uglify: {
    options: {
      mangle: {
        except: ['jQuery', 'Backbone']
      }
    },
    my_target: {
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

Source maps

Configure basic source map output by specifying a file path for the sourceMap option.

// Project configuration.
grunt.initConfig({
  uglify: {
    my_target: {
      options: {
        sourceMap: 'path/to/source-map.js'
      },
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

Advanced source maps

You can specify the parameters to pass to UglifyJS.SourceMap() which will allow you to configure advanced settings.

Refer to the UglifyJS SourceMap Documentation for more information.

// Project configuration.
grunt.initConfig({
  uglify: {
    my_target: {
      options: {
        sourceMap: 'path/to/source-map.js',
        sourceMapRoot: 'http://example.com/path/to/src/', // the location to find your original source
        sourceMapIn: 'example/coffeescript-sourcemap.js', // input sourcemap from a previous compilation
        }
      },
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

Beautify

Specify beautify: true to beautify your code for debugging/troubleshooting purposes. Pass an object to manually configure any other output options passed directly to UglifyJS.OutputStream().

See UglifyJS Codegen documentation for more information.

Note that manual configuration will require you to explicitly set beautify: true if you want traditional, beautified output.

// Project configuration.
grunt.initConfig({
  uglify: {
    my_target: {
      options: {
        beautify: true
      },
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    },
    my_advanced_target: {
      options: {
        beautify: {
          width: 80,
          beautify: true
        }
      },
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

In this example, running grunt uglify:my_target will prepend a banner created by interpolating the banner template string with the config object. Here, those properties are the values imported from the package.json file (which are available via the pkg config property) plus today's date.

Note: you don't have to use an external JSON file. It's also valid to create the pkg object inline in the config. That being said, if you already have a JSON file, you might as well reference it.

// Project configuration.
grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),
  uglify: {
    options: {
      banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
        '<%= grunt.template.today("yyyy-mm-dd") %> */'
    },
    my_target: {
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

Release History

  • 2012-11-27   v0.1.0   Work in progress, not yet officially released.

Task submitted by "Cowboy" Ben Alman

This file was generated on Thu Dec 13 2012 15:42:18.