1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
# Usage examples
## Basic compression
This configuration will compress and mangle the input files using the default options.
```js
// 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.
```js
// 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.
```js
// Project configuration.
grunt.initConfig({
uglify: {
options: {
mangle: {
except: ['jQuery', 'Backbone']
}
},
my_target: {
files: {
'dest/output.min.js': ['src/input.js']
}
}
}
});
```
## Source maps
Generate a source map by setting the `sourceMap` option to `true`. The generated
source map will be in the same directory as the destination file. Its name will be the
basename of the destination file with a `.map` extension. Override these
defaults with the `sourceMapName` attribute.
```js
// Project configuration.
grunt.initConfig({
uglify: {
my_target: {
options: {
sourceMap: true,
sourceMapName: 'path/to/sourcemap.map'
},
files: {
'dest/output.min.js': ['src/input.js']
}
}
}
});
```
## Advanced source maps
Set the `sourceMapIncludeSources` option to `true` to embed your sources directly into the map. To include
a source map from a previous compilation pass it as the value of the `sourceMapIn` option.
```js
// Project configuration.
grunt.initConfig({
uglify: {
my_target: {
options: {
sourceMap: true,
sourceMapIncludeSources: true,
sourceMapIn: 'example/coffeescript-sourcemap.js', // input sourcemap from a previous compilation
},
files: {
'dest/output.min.js': ['src/input.js'],
},
},
},
});
```
Refer to the [UglifyJS SourceMap Documentation](http://lisperator.net/uglifyjs/codegen#source-map) for more information.
## Turn off console warnings
Specify `drop_console: true` as part of the `compress` options to discard calls to `console.*` functions.
This will suppress warning messages in the console.
```js
// Project configuration.
grunt.initConfig({
uglify: {
options: {
compress: {
drop_console: true
}
},
my_target: {
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](http://lisperator.net/uglifyjs/codegen) for more information.
_Note that manual configuration will require you to explicitly set `beautify: true` if you want traditional, beautified output._
```js
// 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_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._
```js
// 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. This is equivalent to the command line [`--define` option](https://github.com/mishoo/UglifyJS#use-as-a-code-pre-processor).
See [UglifyJS global definitions documentation](http://lisperator.net/uglifyjs/compress#global-defs) for more information.
```js
// Project configuration.
grunt.initConfig({
uglify: {
options: {
compress: {
global_defs: {
'DEBUG': false
},
dead_code: true
}
},
my_target: {
files: {
'dest/output.min.js': ['src/input.js']
}
}
}
});
```
## Compiling all files in a folder dynamically
This configuration will compress and mangle the files dynamically.
```js
// Project configuration.
grunt.initConfig({
uglify: {
my_target: {
files: [{
expand: true,
cwd: 'src/js',
src: '**/*.js',
dest: 'dest/js'
}]
}
}
});
```
## Turn on object property name mangling
This configuration will turn on object property name mangling, but not mangle built-in browser object properties.
Additionally, variables and object properties listed in the `myExceptionsFile.json` will be mangled. For more info,
on the format of the exception file format please see the [UglifyJS docs](https://www.npmjs.com/package/uglify-js).
```js
// Project configuration.
grunt.initConfig({
uglify: {
options: {
mangleProperties: true,
reserveDOMCache: true,
exceptionsFiles: [ 'myExceptionsFile.json' ]
},
my_target: {
files: {
'dest/output.min.js': ['src/input.js']
}
}
}
});
```
## Turn on use of name mangling cache
Turn on use of name mangling cache to coordinate mangled symbols between outputted uglify files. uglify will the
generate a JSON cache file with the name provided in the options. Note: this generated file uses the same JSON format
as the `exceptionsFiles` files.
```js
// Project configuration.
grunt.initConfig({
uglify: {
options: {
nameCache: '.tmp/grunt-uglify-cache.json',
},
my_target: {
files: {
'dest/output1.min.js': ['src/input1.js'],
'dest/output2.min.js': ['src/input2.js']
}
}
}
});
```
|