-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclosure-compiler-modules.js
executable file
·225 lines (180 loc) · 7.85 KB
/
closure-compiler-modules.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
module.exports = function(grunt) {
'use strict';
var exec = require('child_process').exec,
fs = require('fs'),
path = require('path'),
gzip = require('zlib').gzip;
// ==========================================================================
// TASKS
// ==========================================================================
grunt.registerMultiTask('closure-compiler', 'Minify JS files using Closure Compiler.', function() {
var closurePath = '',
reportFile = '',
data = this.data,
done = this.async();
// Check for closure path.
if (data.closurePath) {
closurePath = data.closurePath;
} else if (process.env.CLOSURE_PATH) {
closurePath = process.env.CLOSURE_PATH;
} else {
grunt.log.error('' +
'/!\\'.red +
' Set an environment variable called ' +
'CLOSURE_PATH'.red + ' or the build parameter' + 'closurePath'.red +
' and\nmake it point to your root install of Closure Compiler.' +
'\n');
return false;
}
var command = 'java -jar "' + closurePath + '/build/compiler.jar"';
data.cwd = data.cwd || './';
var output_mode;
// --module mode
if ("modules" in data) {
output_mode = 'modules';
} else { // --js mode
output_mode = 'js';
data.js = grunt.file.expand({
cwd: data.cwd
}, data.js);
// Sanitize options passed.
if (!data.js.length) {
// This task requires a minima an input file.
grunt.warn('Missing js property.');
return false;
}
// Build command line.
command += ' --js "' + data.js.join('" --js "') + '"';
if (data.jsOutputFile) {
if (!grunt.file.isPathAbsolute(data.jsOutputFile)) {
data.jsOutputFile = path.resolve('./') + '/' + data.jsOutputFile;
}
command += ' --js_output_file "' + data.jsOutputFile + '"';
reportFile = data.reportFile || data.jsOutputFile + '.report.txt';
}
}
if (data.externs) {
data.externs = grunt.file.expand(data.externs);
command += ' --externs ' + data.externs.join(' --externs ');
if (!data.externs.length) {
delete data.externs;
}
}
if (data.options.externs) {
data.options.externs = grunt.file.expand(data.options.externs);
if (!data.options.externs.length) {
delete data.options.externs;
}
}
for (var directive in data.options) {
if (Array.isArray(data.options[directive])) {
command += ' --' + directive + ' ' + data.options[directive].join(' --' + directive + ' ');
} else if (data.options[directive] === undefined || data.options[directive] === null) {
command += ' --' + directive;
} else {
command += ' --' + directive + ' "' + String(data.options[directive]) + '"';
}
}
if (output_mode === 'js') {
// because closure compiler does not create dirs.
grunt.file.write(data.jsOutputFile, '');
} else { // modules
for (var module in data.modules) {
data.modules[module].src.forEach(function(src) {
command += ' --js "' + src + '"';
});
command += ' --chunk "' + module + ':' + data.modules[module].src.length + ':';
// add dependency
if (data.modules[module].dep) {
command += data.modules[module].dep.join(',');
command += ':';
}
command += '"';
// @todo add --define option for modules
if (data.modules[module].define) {
if (typeof data.modules[module].define === 'object') {
var value;
for (var key in data.modules[module].define) {
if (data.modules[module].define.hasOwnProperty(key)) {
value = data.modules[module].define[key];
command += ' --define="' + String(key) + '=';
if (typeof value === 'boolean') {
command += data.modules[module].define[key];
} else if (!isNaN(parseFloat(data.modules[module].define[key]))) {
command += String(data.modules[module].define[key]);
} else {
// escape quotes
command += '\'' + String(data.modules[module].define[key]).replace(/'/g, '\\\'').replace(/"/g, '\\"') + '\'';
}
command += '"';
}
}
}
}
command += ' --assume_function_wrapper';
command += ' --isolation_mode IIFE';
}
// module output directory
command += ' --chunk_output_path_prefix ' + data.moduleOutputPath;
}
// Minify WebGraph class.
exec(command, {
maxBuffer: data.maxBuffer * 1024,
cwd: data.cwd
}, function(err, stdout, stderr) {
if (err) {
grunt.warn(err);
done(false);
}
if (stdout) {
grunt.log.writeln(stdout);
}
var p = [];
for (var module in data.modules) {
// If OK, calculate gzipped file size.
//if (reportFile.length) {
(function(module) {
p.push(new Promise(function(resolve, reject) {
var min = fs.readFileSync(data.moduleOutputPath + module + '.js', 'utf8');
min_info(module + '.js', min, function(err) {
if (err) {
grunt.warn(err);
done(false);
}
//if (data.noreport) {
/*} else {
// Write compile report to a file.
fs.writeFile(reportFile, stderr, function(err) {
if (err) {
grunt.warn(err);
done(false);
}
grunt.log.writeln('A report is saved in ' + reportFile + '.');
done();
});
}*/
});
}));
})(module);
}
Promise.all(p).then(done);
/*} else {
if (data.report) {
grunt.log.error(stderr);
}
done();
}*/
});
});
// Output some size info about a file.
function min_info(name, min, onComplete) {
gzip(min, function(err, buffer) {
if (err) {
onComplete.call(this, err);
}
var gzipSize = buffer.toString().length;
grunt.log.writeln(name, 'Size: ' + String((min.length / 1024).toFixed(2)).green + ' kb (' + String(min.length).green + ' bytes) Gzip: ' + String((gzipSize / 1024).toFixed(2)).green + ' kb (' + String(gzipSize).green + ' bytes).');
onComplete.call(this, null);
});
}
};