|
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 | + } |
79 | 57 |
|
80 | 58 | ### Banner comments
|
81 | 59 |
|
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 | + } |
0 commit comments