1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var uglifyjs = require('uglify-js');
|
11 | var gzip = require('gzip-js');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | task.registerBasicTask('min', 'Minify files with UglifyJS.', function(data, name) {
|
18 | var files = file.expand(data);
|
19 |
|
20 |
|
21 | var banner = task.directive(files[0], function() { return null; });
|
22 | if (banner === null) {
|
23 | banner = '';
|
24 | } else {
|
25 | files.shift();
|
26 | }
|
27 |
|
28 |
|
29 | var max = task.helper('concat', files);
|
30 |
|
31 |
|
32 | var min = banner + task.helper('uglify', max, config('uglify'));
|
33 | file.write(name, min);
|
34 |
|
35 |
|
36 | if (task.hadErrors()) { return false; }
|
37 |
|
38 |
|
39 | log.writeln('File "' + name + '" created.');
|
40 |
|
41 | task.helper('min_max_info', min, max);
|
42 | });
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | task.registerHelper('uglify', function(src, options) {
|
51 | if (!options) { options = {}; }
|
52 | var jsp = uglifyjs.parser;
|
53 | var pro = uglifyjs.uglify;
|
54 | var ast, pos;
|
55 | var msg = 'Minifying with UglifyJS...';
|
56 | verbose.write(msg);
|
57 | try {
|
58 | ast = jsp.parse(src);
|
59 | ast = pro.ast_mangle(ast, options.mangle || {});
|
60 | ast = pro.ast_squeeze(ast, options.squeeze || {});
|
61 | src = pro.gen_code(ast, options.codegen || {});
|
62 |
|
63 | verbose.ok();
|
64 | return src;
|
65 | } catch(e) {
|
66 |
|
67 | verbose.or.write(msg);
|
68 | pos = '['.red + ('L' + e.line).yellow + ':'.red + ('C' + e.col).yellow + ']'.red;
|
69 | log.error().writeln(pos + ' ' + (e.message + ' (position: ' + e.pos + ')').yellow);
|
70 | fail.warn('UglifyJS found errors.', 10);
|
71 | }
|
72 | });
|
73 |
|
74 |
|
75 | task.registerHelper('gzip', function(src) {
|
76 | return src ? gzip.zip(src, {}) : '';
|
77 | });
|
78 |
|
79 |
|
80 | task.registerHelper('min_max_info', function(min, max) {
|
81 | var gzipSize = String(task.helper('gzip', min).length);
|
82 | log.writeln('Uncompressed size: ' + String(max.length).green + ' bytes.');
|
83 | log.writeln('Compressed size: ' + gzipSize.green + ' bytes gzipped (' + String(min.length).green + ' bytes minified).');
|
84 | });
|
85 |
|
86 |
|
87 | task.registerHelper('strip_comments', function(src) {
|
88 | return src.replace(/\/\*[\s\S]*?\*\/\n*/g, '\n');
|
89 | });
|