This commit is contained in:
namniak
2014-06-27 00:31:51 -04:00
parent e9951d2934
commit 6ddb18a127
1442 changed files with 274017 additions and 0 deletions
+212
View File
@@ -0,0 +1,212 @@
# 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
Configure basic source map output by specifying a file path for the `sourceMap` option.
```js
// 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](http://lisperator.net/uglifyjs/codegen#source-map) for more information.
```js
// 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](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.
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'
}]
}
}
});
```
+122
View File
@@ -0,0 +1,122 @@
# Options
This task primarily delegates to [UglifyJS2][], so please consider the [UglifyJS documentation][] as required reading for advanced configuration.
[UglifyJS2]: https://github.com/mishoo/UglifyJS2
[UglifyJS documentation]: http://lisperator.net/uglifyjs/
## 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()`
## 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: `String` `Function`
Default: `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: `String`
Default: `undefined`
The location where your source files can be found. This sets the sourceRoot field in the source map.
## sourceMapIn
Type: `String` `Function`
Default: `undefined`
The location of an input source map from an earlier compilation, e.g. from CoffeeScript. If a function is provided, the
uglify source is passed as the argument and the return value will be used as the sourceMap name. This only makes sense
when there's one source file.
## sourceMappingURL
Type: `String` `Function`
Default: `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: `Number`
Default: `undefined`
The number of directories to drop from the path prefix when declaring files in the source map.
#### enclose
Type: `Object`
Default: `undefined`
Wrap all of the code in a closure with a configurable arguments/parameters list.
Each key-value pair in the `enclose` object is effectively an argument-parameter pair.
## wrap
Type: `String`
Default: `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 `exports` and `global` variables are made available.
The value of wrap is the global variable exports will be available as.
## exportAll
Type: `Boolean`
Default: `false`
When using `wrap` this will make all global functions and variables available via the export variable.
## 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`
## banner
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.
## footer
Type: `String`
Default: empty string
This string will be append to the end 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)_
[grunt.template.process]: https://github.com/gruntjs/grunt/wiki/grunt.template#wiki-grunt-template-process
+1
View File
@@ -0,0 +1 @@
Task targets, files and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide.