-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrunt-closure-compiler.js
117 lines (91 loc) · 3.51 KB
/
grunt-closure-compiler.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
module.exports = function(grunt) {
'use strict';
var exec = require('child_process').exec,
fs = require('fs'),
path = require('path');
// ==========================================================================
// TASKS
// ==========================================================================
grunt.registerMultiTask('closure-compiler', '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 || './';
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]) + '"';
}
}
data.js = grunt.file.expand({
cwd: data.cwd
}, data.js)
// because closure compiler does not create dirs.
grunt.file.write(data.jsOutputFile, '');
// 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);
}
// OK
done();
});
});
};