grunt-jw-uglify

Minify files with UglifyJS.

grunt-contrib-uglify

Minify files with UglifyJS.

Getting Started

This plugin requires Grunt ~0.4.0

If you haven't used Gruntbefore, be sure to check out the Getting Startedguide, as it explains how to create a Gruntfileas well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

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

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

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

Uglify task

Run this task with the grunt uglifycommand.

Task targets, files and options may be specified according to the grunt Configuring tasksguide.

Options

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

mangle

Type: BooleanObjectDefault: {}

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

compress

Type: BooleanObjectDefault: {}

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

beautify

Type: BooleanObjectDefault: false

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

report

Choices: false'min''gzip'Default: false

Either do not report anything, report only minification result, or report minification and gzip results. This is useful to see exactly how well Uglify is performing, but using 'gzip'can add 5-10x runtime task execution.

Example ouput using 'gzip':

Original: 198444 bytes.
Minified: 101615 bytes.
Gzipped:  20084 bytes.

sourceMap

Type: StringFunctionDefault: undefined

The location to output the sourcemap. If a function is provided, the uglify destination is passed as the argument and the return value will be used as the sourceMap name.

sourceMapRoot

Type: StringDefault: undefined

The location where your source files can be found. This option sets the root location in the sourcemap file itself.

sourceMapIn

Type: StringDefault: undefined

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

sourceMappingURL

Type: StringFunctionDefault: undefined

The location of your sourcemap. Defaults to the location you use for sourceMap, override if you need finer control. Provide a function to dynamically generate the sourceMappingURL based off the destination.

sourceMapPrefix

Type: NumberDefault: undefined

The number of directories to drop from the path prefix when declaring files in the source map.

wrap

Type: StringDefault: undefined

Wrap all of the code in a closure, an easy way to make sure nothing is leaking. For variables that need to be public exportsand globalvariables are made available. The value of wrap is the global variable exports will be available as.

exportAll

Type: BooleanDefault: false

When using wrapthis will make all global functions and variables available via the export variable.

preserveComments

Type: BooleanStringFunctionDefault: undefinedOptions: false'all''some'

Turn on preservation of comments.

  • falsewill 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)
  • Functionspecify your own comment preservation function. You will be passed the current node and the current comment and are expected to return either trueor false

banner

Type: StringDefault: 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.processdocumentation)

Usage examples

Basic compression

In this example, running grunt uglify:my_target(or grunt uglifybecause uglifyis 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: falseto 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 exceptarray in the mangleoptions.

// 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 sourceMapoption.

// 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 Documentationfor 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: trueto 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 documentationfor more information.

Note that manual configuration will require you to explicitly set beautify: trueif 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']
      }
    }
  }
});

Banner comments

In this example, running grunt uglify:my_targetwill prepend a banner created by interpolating the bannertemplate string with the config object. Here, those properties are the values imported from the package.jsonfile (which are available via the pkgconfig property) plus today's date.

Note: you don't have to use an external JSON file. It's also valid to create the pkgobject 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']
      }
    }
  }
});

Conditional compilation

You can also enable UglifyJS conditional compilation. This is commonly used to remove debug code blocks for production builds.

See UglifyJS global definitions documentationfor more information.

// Project configuration.
grunt.initConfig({
  uglify: {
    options: {
      compress: {
        global_defs: {
          "DEBUG": false
        },
        dead_code: true
      }
    },
    my_target: {
      files: {
        'dest/output.min.js': ['src/input.js']
      }
    }
  }
});

Release History

  • 2013-03-13   v0.2.0   No longer report gzip results by default. Support reportoption.
  • 2013-01-29   v0.1.2   Added better error reporting Support for dynamic names of multiple sourcemaps
  • 2013-02-14   v0.1.1   First official release for Grunt 0.4.0.
  • 2013-01-17   v0.1.1rc6   Updating grunt/gruntplugin dependencies to rc6. Changing in-development grunt/gruntplugin dependency versions from tilde version ranges to specific versions.
  • 2013-01-08   v0.1.1rc5   Updating to work with grunt v0.4.0rc5. Switching back to this.files api.
  • 2012-11-27   v0.1.0   Work in progress, not yet officially released.

Task submitted by "Cowboy" Ben Alman

This file was generated on Fri Mar 15 2013 15:50:39.

HomePage

https://github.com/gruntjs/grunt-contrib-uglify

Repository

https://github.com/gruntjs/grunt-contrib-uglify


上一篇:grunt-license-collection
下一篇:grunt-inline

相关推荐

官方社区

扫码加入 JavaScript 社区