-
Notifications
You must be signed in to change notification settings - Fork 0
/
minify.js
124 lines (107 loc) · 3.06 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var minify = (function(undefined) {
var exec = require('child_process').exec,
_fs = require('fs');
/**
* minify.js
* minify javascript using GCC | Uglify | YUI (CSS too)
* @param {object} options see below
* @return {void}
*/
var minify = function(options) {
this.type = options.type
//prepare input file format
if (options.type == 'gcc' && typeof options.fileIn === 'string') {
this.fileIn = [options.fileIn];
} else if(typeof options.fileIn === 'string') {
this.fileIn = options.fileIn;
}
//read the file contents
if (typeof options.fileIn === 'object' && options.fileIn instanceof Array && options.type != 'gcc') {
this.fileIn = options.fileIn.map(function(path) {
return _fs.readFileSync(path, 'utf8');
});
} else if(typeof options.fileIn === 'object' && options.fileIn instanceof Array && options.type == 'gcc') {
this.fileIn = options.fileIn;
}
//set options
this.fileOut = options.fileOut;
this.options = options.options || [];
//with larger files you will need a bigger buffer for closure compiler
this.buffer = options.buffer || 1000 * 1024;
if (typeof options.callback !== 'undefined') {
this.callback = options.callback;
}
//compress file
this.compress();
};
minify.prototype = minify.fn = {
type : null,
fileIn : null,
fileOut : null,
callback : null,
buffer : null,
compress : function () {
var minify = this, command, fileInCommand;
switch (this.type) {
case 'yui':
case 'yui-css':
command = 'java -jar -Xss2048k "' +
__dirname +
'/yuicompressor-2.4.7.jar" "' +
this.fileIn +
'" -o "' +
this.fileOut +
'" --type css ' +
this.options.join(' ');
break;
case 'yui-js':
command = 'java -jar -Xss2048k "' +
__dirname +
'/yuicompressor-2.4.7.jar" "' +
this.fileIn + '" -o "' +
this.fileOut +
'" --type js '
+ this.options.join(' ');
break;
case 'gcc':
fileInCommand = this.fileIn.map(function(file) {
return '--js="' + file + '"';
});
command = 'java -jar "' +
__dirname +
'/google_closure_compiler-r1918.jar" ' +
fileInCommand.join(' ') +
' --js_output_file="' +
this.fileOut +
'" ' +
this.options.join(' ');
break;
case 'uglifyjs':
command = '"' + __dirname +
'/node_modules/uglify-js/bin/uglifyjs" --output "' +
this.fileOut +
'" --no-copyright "' +
this.fileIn + '" ' +
this.options.join(' ');
break;
}
exec(command, { maxBuffer: this.buffer }, function (err, stdout, stderr) {
if (minify.callback) {
minify.callback(err || null);
}
});
}
};
return minify;
}());
exports.minify = minify;
// EXAMPLE USAGE
// var compressor = require('minify');
// new compressor.minify({
// type: 'gcc',
// fileIn: 'example.js',
// fileOut: 'gcc.js',
// callback: function (err) {
// console.log(err ? 'Error: ' + err : 'minification successful using google closure compiler');
// }
// });