Skip to content

Commit 58cf94d

Browse files
author
Jarrod Overson
committedNov 18, 2012
Updated docs, added author, clarified comment
1 parent adde3fd commit 58cf94d

File tree

5 files changed

+106
-153
lines changed

5 files changed

+106
-153
lines changed
 

‎AUTHORS

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
"Cowboy" Ben Alman (http://benalman.com)
2-
Tyler Kellen (http://goingslowly.com)
2+
Tyler Kellen (http://goingslowly.com)
3+
Jarrod Overson (http://jarrodoverson.com)

‎docs/uglify-examples.md

+65-100
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,68 @@
1-
## Usage examples
2-
3-
### Minifying individual files
4-
5-
In this example, running `grunt min:dist` (or `grunt min` because `min` is a [multi task](types_of_tasks.md)) will minify the specified source file, writing the output to `dist/built.min.js`.
6-
7-
_Note that UglifyJS strips all comments from the source, including banner comments. See the "Banner comments" example for instructions on how to add a banner to the generated source._
8-
9-
```javascript
10-
// Project configuration.
11-
grunt.initConfig({
12-
min: {
13-
dist: {
14-
src: ['dist/built.js'],
15-
dest: 'dist/built.min.js'
16-
}
17-
}
18-
});
19-
```
20-
21-
### Minifying while concatenating files
22-
23-
In this example, running `grunt min:dist` (or `grunt min` because `min` is a [multi task](types_of_tasks.md)) will first concatenate the three specified source files, in order, minifying the result and writing the output to `dist/built.min.js`.
24-
25-
_Note that UglifyJS strips all comments from the source, including banner comments. See the "Banner comments" example for instructions on how to add a banner to the generated source._
26-
27-
```javascript
28-
// Project configuration.
29-
grunt.initConfig({
30-
min: {
31-
dist: {
32-
src: ['src/intro.js', 'src/project.js', 'src/outro.js'],
33-
dest: 'dist/built.min.js'
34-
}
35-
}
36-
});
37-
```
38-
39-
With a slight modification, running `grunt min` will join the specified source files using `;` instead of the default newline character before minification.
40-
41-
```javascript
42-
// Project configuration.
43-
grunt.initConfig({
44-
min: {
45-
dist: {
46-
src: ['src/intro.js', 'src/project.js', 'src/outro.js'],
47-
dest: 'dist/built.min.js',
48-
separator: ';'
49-
}
50-
}
51-
});
52-
```
53-
54-
### Minifying and concatenating separately
55-
56-
Often, it's desirable to create both unminified and minified distribution files. In these cases, the [concat task](task_concat.md) should be run first, followed by the `min` task.
57-
58-
In this example, running `grunt concat:dist min:dist` (or `grunt concat min` because both `concat` and `min` are [multi tasks](types_of_tasks.md)) will first concatenate the three specified source files, in order, writing the output to `dist/built.js`. After that, grunt will minify the newly-created file, writing the output to `dist/built.min.js`.
59-
60-
_Note that UglifyJS strips all comments from the source, including banner comments. See the "Banner comments" example for instructions on how to add a banner to the generated source._
61-
62-
```javascript
63-
// Project configuration.
64-
grunt.initConfig({
65-
concat: {
66-
dist: {
67-
src: ['src/intro.js', 'src/project.js', 'src/outro.js'],
68-
dest: 'dist/built.js'
69-
}
70-
},
71-
min: {
72-
dist: {
73-
src: ['dist/built.js'],
74-
dest: 'dist/built.min.js'
75-
}
76-
}
77-
});
78-
```
1+
# Usage examples
2+
3+
### All tasks are specified in an `uglify` block
4+
5+
uglify: {
6+
7+
### This is a multitask and options specified at the root level will be merged with each task
8+
9+
options: {
10+
mangle : {
11+
except : ['jQuery', 'Backbone']
12+
}
13+
},
14+
15+
### Just use default options to compress your source
16+
17+
default: {
18+
files: {
19+
'source.min.js': ['source.js']
20+
}
21+
},
22+
23+
### Compress your source only, no mangling
24+
25+
no_mangle: {
26+
files: {
27+
'source.min.js': ['source.js']
28+
},
29+
options : {
30+
mangle : false
31+
}
32+
},
33+
34+
### Compress, mangle, and output source map
35+
36+
sourcemap: {
37+
files: {
38+
'source.min.js': ['source.js']
39+
},
40+
options : {
41+
source_map : 'sourcemap.js'
42+
}
43+
},
44+
45+
### Beautify your compressed and mangled source
46+
47+
beautified: {
48+
files: {
49+
'source.min.js': ['source.js']
50+
},
51+
options : {
52+
beautify : {
53+
max_line_len : 120
54+
}
55+
}
56+
}
7957

8058
### Banner comments
8159

82-
In this example, running `grunt min:dist` (or `grunt min` because `min` is a [multi task](types_of_tasks.md)) will first strip any preexisting comments from the `src/project.js` file (because that's how UglifyJS works), then concatenate the result with a newly-generated banner comment, writing the output to `dist/built.js`.
83-
84-
This generated banner will be the contents of the `min.options.banner` underscore template string interpolated with the config object. In this case, those properties are the values imported from the `package.json` file (which are available via the `pkg` config property) plus today's date.
85-
86-
_Note: you don't have to use an external JSON file. It's completely 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.
87-
88-
```javascript
89-
// Project configuration.
90-
grunt.initConfig({
91-
pkg: grunt.file.readJSON('package.json'),
92-
min: {
93-
options: {
94-
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
95-
'<%= grunt.template.today("yyyy-mm-dd") %> */'
96-
},
97-
dist: {
98-
src: ['dist/built.js'],
99-
dest: 'dist/built.min.js'
100-
}
101-
}
102-
});
103-
```
60+
banner: {
61+
files: {
62+
'source.min.js': ['source.js']
63+
},
64+
options : {
65+
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
66+
' * <%= grunt.template.today("yyyy-mm-dd") %> */'
67+
}
68+
}

‎docs/uglify-options.md

+35-26
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
### Specifying UglifyJS options
2-
3-
In this example, custom UglifyJS `mangle`, `squeeze` and `codegen` options are specified. The listed methods and their expected options are explained in the API section of the [UglifyJS documentation][uglify]:
4-
5-
* The `mangle` object is passed into the `pro.ast_mangle` method.
6-
* The `squeeze` object is passed into the `pro.ast_squeeze` method.
7-
* The `codegen` object is passed into the `pro.gen_code` method.
8-
9-
```javascript
10-
// Project configuration.
11-
grunt.initConfig({
12-
min: {
13-
dist: {
14-
src: ['dist/built.js'],
15-
dest: 'dist/built.min.js'
16-
}
17-
},
18-
uglify: {
19-
mangle: {toplevel: true},
20-
squeeze: {dead_code: false},
21-
codegen: {quote_keys: true}
22-
}
23-
});
24-
```
25-
26-
See the [min task source](../tasks/min.js) for more information.
1+
# UglifyJS options
2+
3+
## API Note:
4+
5+
When in doubt, the options mimic the [UglifyJS2 api](http://lisperator.net/uglifyjs/) *except* where the command line API is found to be simpler (e.g. reusing options passed to `mangle_names` and `compute_char_frequency`.
6+
7+
## mangle
8+
Type: `Boolean`, `Object`
9+
Default: `{}`
10+
11+
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).
12+
13+
## compress
14+
Type: `Boolean`, `Object`
15+
Default: `{}`
16+
17+
Turn on or off source compression with default options. If an `Object` is specified, it is passed directly to `UglifyJS2.Compressor()`.
18+
19+
## beautify
20+
Type: `Boolean`, `Object`
21+
Default: `false`
22+
23+
Turns on beautification of the generated source code. Any extra options passed are merged with the options sent to `UglifyJS2.OutputStream()`.
24+
25+
## source_map
26+
Type: `string`
27+
Default: `undefined`
28+
29+
Specify the sourcemap location to output.
30+
31+
## banner
32+
Type: `string`
33+
Default: `undefined`
34+
35+
Specify a banner to prepend to the output source, e.g. license comments.

‎docs/uglify-overview.md

+3-24
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
1-
## About
1+
## Overview
22

3-
This task is a [multi task](types_of_tasks.md), meaning that grunt will automatically iterate over all `min` targets if a target is not specified.
3+
This task is a [multi task](types_of_tasks.md), meaning that grunt will automatically iterate over all `uglify` targets if a target is not specified.
44

5-
_Need some help getting started with grunt? Visit the [getting started](getting_started.md) page. And if you're creating your own tasks, be sure to check out the [types of tasks](types_of_tasks.md) page as well as the [API documentation](api.md)._
6-
7-
## A Very Important Note
8-
Your Gruntfile **must** contain this code, once and **only** once. If it doesn't, grunt won't work. For the sake of brevity, this "wrapper" code has been omitted from all examples on this page, but it needs to be there.
9-
10-
```javascript
11-
module.exports = function(grunt) {
12-
// Your grunt code goes in here.
13-
};
14-
```
15-
16-
## Project configuration
17-
18-
This example shows a brief overview of the [config](api_config.md) properties used by the `min` task. For a more in-depth explanation, see the usage examples.
19-
20-
```javascript
21-
// Project configuration.
22-
grunt.initConfig({
23-
// Lists of files to be minified with UglifyJS.
24-
min: {}
25-
});
26-
```
5+
grunt-contrib-uglify primarily delegates to [UglifyJS2](https://github.com/mishoo/UglifyJS2), so please consider the [UglifyJS documentation](http://lisperator.net/uglifyjs/) as required reading for advanced configuration.

‎tasks/lib/uglify.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ exports.init = function(grunt) {
6565
}
6666

6767
if (options.mangle !== false ) {
68-
// Optimize for gzip compression.
69-
// compute_char_frequency creates larger source but compresses better
68+
// compute_char_frequency optimizes names for compression
7069
ast.compute_char_frequency(options.mangle);
7170

7271
// Requires previous call to figure_out_scope

0 commit comments

Comments
 (0)
Please sign in to comment.