UNPKG

3.02 kBJavaScriptView Raw
1/*
2 * grunt
3 * https://github.com/cowboy/grunt
4 *
5 * Copyright (c) 2012 "Cowboy" Ben Alman
6 * Licensed under the MIT license.
7 * http://benalman.com/about/license/
8 */
9
10var uglifyjs = require('uglify-js');
11var gzip = require('gzip-js');
12
13// ============================================================================
14// TASKS
15// ============================================================================
16
17task.registerBasicTask('min', 'Minify files with UglifyJS.', function(data, name) {
18 var files = file.expand(data);
19 // Get banner, if specified. It would be nice if UglifyJS supported ignoring
20 // all comments matching a certain pattern, like /*!...*/, but it doesn't.
21 var banner = task.directive(files[0], function() { return null; });
22 if (banner === null) {
23 banner = '';
24 } else {
25 files.shift();
26 }
27 // Concat specified files. This should really be a single, pre-built (and
28 // linted) file, but it supports any number of files.
29 var max = task.helper('concat', files);
30
31 // Concat banner + minified source.
32 var min = banner + task.helper('uglify', max, config('uglify'));
33 file.write(name, min);
34
35 // Fail task if errors were logged.
36 if (task.hadErrors()) { return false; }
37
38 // Otherwise, print a success message....
39 log.writeln('File "' + name + '" created.');
40 // ...and report some size information.
41 task.helper('min_max_info', min, max);
42});
43
44// ============================================================================
45// HELPERS
46// ============================================================================
47
48// Minify with UglifyJS.
49// From https://github.com/mishoo/UglifyJS
50task.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 // Success!
63 verbose.ok();
64 return src;
65 } catch(e) {
66 // Something went wrong.
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// Return gzipped source.
75task.registerHelper('gzip', function(src) {
76 return src ? gzip.zip(src, {}) : '';
77});
78
79// Output some size info about a file.
80task.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// Strip /*...*/ comments from source.
87task.registerHelper('strip_comments', function(src) {
88 return src.replace(/\/\*[\s\S]*?\*\/\n*/g, '\n');
89});