You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

319 lines
9.3 KiB

  1. # grunt-contrib-uglify [![Build Status](https://secure.travis-ci.org/gruntjs/grunt-contrib-uglify.png?branch=master)](http://travis-ci.org/gruntjs/grunt-contrib-uglify)
  2. > Minify files with UglifyJS.
  3. ## Getting Started
  4. This plugin requires Grunt `~0.4.0`
  5. If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
  6. ```shell
  7. npm install grunt-contrib-uglify --save-dev
  8. ```
  9. Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
  10. ```js
  11. grunt.loadNpmTasks('grunt-contrib-uglify');
  12. ```
  13. ## Uglify task
  14. _Run this task with the `grunt uglify` command._
  15. Task targets, files and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide.
  16. ### Options
  17. This task primarily delegates to [UglifyJS2][], so please consider the [UglifyJS documentation][] as required reading for advanced configuration.
  18. [UglifyJS2]: https://github.com/mishoo/UglifyJS2
  19. [UglifyJS documentation]: http://lisperator.net/uglifyjs/
  20. #### mangle
  21. Type: `Boolean` `Object`
  22. Default: `{}`
  23. 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).
  24. #### compress
  25. Type: `Boolean` `Object`
  26. Default: `{}`
  27. Turn on or off source compression with default options. If an `Object` is specified, it is passed as options to `UglifyJS.Compressor()`.
  28. #### beautify
  29. Type: `Boolean` `Object`
  30. Default: `false`
  31. Turns on beautification of the generated source code. An `Object` will be merged and passed with the options sent to `UglifyJS.OutputStream()`
  32. #### report
  33. Choices: `false` `'min'` `'gzip'`
  34. Default: `false`
  35. 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.
  36. Example ouput using `'gzip'`:
  37. ```
  38. Original: 198444 bytes.
  39. Minified: 101615 bytes.
  40. Gzipped: 20084 bytes.
  41. ```
  42. #### sourceMap
  43. Type: `String` `Function`
  44. Default: `undefined`
  45. The location to output the sourcemap. If a function is provided, the uglify destination is passed as the argument
  46. and the return value will be used as the sourceMap name.
  47. #### sourceMapRoot
  48. Type: `String`
  49. Default: `undefined`
  50. The location where your source files can be found. This option sets the root location in the sourcemap file itself.
  51. #### sourceMapIn
  52. Type: `String`
  53. Default: `undefined`
  54. The location of an input source map from an earlier compilation, e.g. from CoffeeScript.
  55. #### sourceMappingURL
  56. Type: `String` `Function`
  57. Default: `undefined`
  58. The location of your sourcemap. Defaults to the location you use for sourceMap, override if you need finer control. Provide
  59. a function to dynamically generate the sourceMappingURL based off the destination.
  60. #### sourceMapPrefix
  61. Type: `Number`
  62. Default: `undefined`
  63. The number of directories to drop from the path prefix when declaring files in the source map.
  64. #### wrap
  65. Type: `String`
  66. Default: `undefined`
  67. Wrap all of the code in a closure, an easy way to make sure nothing is leaking.
  68. For variables that need to be public `exports` and `global` variables are made available.
  69. The value of wrap is the global variable exports will be available as.
  70. #### exportAll
  71. Type: `Boolean`
  72. Default: `false`
  73. When using `wrap` this will make all global functions and variables available via the export variable.
  74. #### preserveComments
  75. Type: `Boolean` `String` `Function`
  76. Default: `undefined`
  77. Options: `false` `'all'` `'some'`
  78. Turn on preservation of comments.
  79. - `false` will strip all comments
  80. - `'all'` will preserve all comments in code blocks that have not been squashed or dropped
  81. - `'some'` will preserve all comments that start with a bang (`!`) or include a closure compiler style directive (`@preserve` `@license` `@cc_on`)
  82. - `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`
  83. #### banner
  84. Type: `String`
  85. Default: empty string
  86. This string will be prepended to the beginning of the minified output. It is processed using [grunt.template.process][], using the default options.
  87. _(Default processing options are explained in the [grunt.template.process][] documentation)_
  88. [grunt.template.process]: https://github.com/gruntjs/grunt/wiki/grunt.template#wiki-grunt-template-process
  89. ### Usage examples
  90. #### Basic compression
  91. 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.
  92. ```js
  93. // Project configuration.
  94. grunt.initConfig({
  95. uglify: {
  96. my_target: {
  97. files: {
  98. 'dest/output.min.js': ['src/input1.js', 'src/input2.js']
  99. }
  100. }
  101. }
  102. });
  103. ```
  104. #### No mangling
  105. Specify `mangle: false` to prevent changes to your variable and function names.
  106. ```js
  107. // Project configuration.
  108. grunt.initConfig({
  109. uglify: {
  110. options: {
  111. mangle: false
  112. },
  113. my_target: {
  114. files: {
  115. 'dest/output.min.js': ['src/input.js']
  116. }
  117. }
  118. }
  119. });
  120. ```
  121. #### Reserved identifiers
  122. You can specify identifiers to leave untouched with an `except` array in the `mangle` options.
  123. ```js
  124. // Project configuration.
  125. grunt.initConfig({
  126. uglify: {
  127. options: {
  128. mangle: {
  129. except: ['jQuery', 'Backbone']
  130. }
  131. },
  132. my_target: {
  133. files: {
  134. 'dest/output.min.js': ['src/input.js']
  135. }
  136. }
  137. }
  138. });
  139. ```
  140. #### Source maps
  141. Configure basic source map output by specifying a file path for the `sourceMap` option.
  142. ```js
  143. // Project configuration.
  144. grunt.initConfig({
  145. uglify: {
  146. my_target: {
  147. options: {
  148. sourceMap: 'path/to/source-map.js'
  149. },
  150. files: {
  151. 'dest/output.min.js': ['src/input.js']
  152. }
  153. }
  154. }
  155. });
  156. ```
  157. #### Advanced source maps
  158. You can specify the parameters to pass to `UglifyJS.SourceMap()` which will
  159. allow you to configure advanced settings.
  160. Refer to the [UglifyJS SourceMap Documentation](http://lisperator.net/uglifyjs/codegen#source-map) for more information.
  161. ```js
  162. // Project configuration.
  163. grunt.initConfig({
  164. uglify: {
  165. my_target: {
  166. options: {
  167. sourceMap: 'path/to/source-map.js',
  168. sourceMapRoot: 'http://example.com/path/to/src/', // the location to find your original source
  169. sourceMapIn: 'example/coffeescript-sourcemap.js', // input sourcemap from a previous compilation
  170. }
  171. },
  172. files: {
  173. 'dest/output.min.js': ['src/input.js']
  174. }
  175. }
  176. }
  177. });
  178. ```
  179. #### Beautify
  180. Specify `beautify: true` to beautify your code for debugging/troubleshooting purposes.
  181. Pass an object to manually configure any other output options passed directly to `UglifyJS.OutputStream()`.
  182. See [UglifyJS Codegen documentation](http://lisperator.net/uglifyjs/codegen) for more information.
  183. _Note that manual configuration will require you to explicitly set `beautify: true` if you want traditional, beautified output._
  184. ```js
  185. // Project configuration.
  186. grunt.initConfig({
  187. uglify: {
  188. my_target: {
  189. options: {
  190. beautify: true
  191. },
  192. files: {
  193. 'dest/output.min.js': ['src/input.js']
  194. }
  195. },
  196. my_advanced_target: {
  197. options: {
  198. beautify: {
  199. width: 80,
  200. beautify: true
  201. }
  202. },
  203. files: {
  204. 'dest/output.min.js': ['src/input.js']
  205. }
  206. }
  207. }
  208. });
  209. ```
  210. #### Banner comments
  211. 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.
  212. _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._
  213. ```js
  214. // Project configuration.
  215. grunt.initConfig({
  216. pkg: grunt.file.readJSON('package.json'),
  217. uglify: {
  218. options: {
  219. banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
  220. '<%= grunt.template.today("yyyy-mm-dd") %> */'
  221. },
  222. my_target: {
  223. files: {
  224. 'dest/output.min.js': ['src/input.js']
  225. }
  226. }
  227. }
  228. });
  229. ```
  230. ## Release History
  231. * 2013-03-13   v0.2.0   No longer report gzip results by default. Support `report` option.
  232. * 2013-01-29   v0.1.2   Added better error reporting Support for dynamic names of multiple sourcemaps
  233. * 2013-02-14   v0.1.1   First official release for Grunt 0.4.0.
  234. * 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.
  235. * 2013-01-08   v0.1.1rc5   Updating to work with grunt v0.4.0rc5. Switching back to this.files api.
  236. * 2012-11-27   v0.1.0   Work in progress, not yet officially released.
  237. ---
  238. Task submitted by ["Cowboy" Ben Alman](http://benalman.com)
  239. *This file was generated on Thu Mar 14 2013 08:47:41.*