-
Notifications
You must be signed in to change notification settings - Fork 421
/
minify.js
48 lines (42 loc) · 1.48 KB
/
minify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var path = require('path');
var fs = require('fs');
module.exports = function( grunt ) {
grunt.registerMultiTask("minify", "Minifies CanJS, then outputs filesize information", function() {
var done = this.async();
var minPostfix = '.min';
var tasks = [];
grunt.file.expandFiles(this.file.src).forEach(function (file) {
if(new RegExp(minPostfix).test(file)) {
console.log('Ignoring already minified file ' + file);
return;
}
tasks.push(function(callback) {
var minFile = path.join(path.dirname(file), path.basename(file, '.js') + minPostfix + '.js');
var gzFile = minFile + '.gz';
fs.stat(file, function(err, stats) {
if(err) return callback(err);
var originalSize = (stats.size / 1024).toFixed(2);
console.log('Minifying ' + file + ' original size is ' + originalSize + 'Kb');
grunt.utils.exec({
cmd : 'closure',
args : [file, '--js_output_file', minFile]
}, function(error, result, code) {
fs.stat(minFile, function(err, stats) {
if(err) return callback(err);
var minifiedSize = (stats.size / 1024).toFixed(2);
var saved = 100 - Math.round((minifiedSize/originalSize) * 100);
console.log('Created ' + minFile + ' minified size is ' +
minifiedSize + 'Kb, saved ' + saved + '%')
return callback(null, result);
});
});
});
});
});
console.log(tasks.length);
grunt.utils.async.series(tasks, function(error, results) {
console.log(results);
done();
});
});
};