Ensure that the javaOptions are set correctly, as exec is asynchronous.

Previously, the `java` command would execute on the next tick and update the
`javaOptions` array, which could occur prior to the invocation of the Closure
Compiler on the current tick. This has now been refactored into a separate
`getJavaOptions` function, which passes the `javaOptions` array to a callback.

`getJavaOptions` is defined lazily; after the first invocation, the options
are cached and passed to all subsequent callbacks. The callbacks are invoked
on the next tick for compatibility with `exec`.


Former-commit-id: 89ca63c9edb3df3d4fcbbaa64e06075495febfd0
This commit is contained in:
Kit Cambridge
2013-05-19 14:14:33 -07:00
parent a2088fa500
commit 14a447b3d8

View File

@@ -60,15 +60,30 @@
}()); }());
/** /**
* Java command-line options used for faster minification. * Retrieves the Java command-line options used for faster minification by
* the Closure Compiler, invoking the `callback` when finished. Subsequent
* invocations will lazily return the original options. The callback is
* invoked with one argument: `(javaOptions)`.
*
* See https://code.google.com/p/closure-compiler/wiki/FAQ#What_are_the_recommended_Java_VM_command-line_options?. * See https://code.google.com/p/closure-compiler/wiki/FAQ#What_are_the_recommended_Java_VM_command-line_options?.
*
* @param {Function} callback The function called once the options have
* been retrieved.
*/ */
function getJavaOptions(callback) {
var javaOptions = []; var javaOptions = [];
cp.exec('java -version -client -d32', function(error) { cp.exec('java -version -client -d32', function(error) {
if (!error && process.platform != 'win32') { if (!error && process.platform != 'win32') {
javaOptions.push('-client', '-d32'); javaOptions.push('-client', '-d32');
} }
getJavaOptions = function getJavaOptions(callback) {
process.nextTick(function () {
callback(javaOptions);
}); });
};
callback(javaOptions);
});
}
/** The Closure Compiler optimization modes */ /** The Closure Compiler optimization modes */
var optimizationModes = { var optimizationModes = {
@@ -386,6 +401,8 @@
if (isMapped) { if (isMapped) {
options.push('--create_source_map=' + mapPath, '--source_map_format=V3'); options.push('--create_source_map=' + mapPath, '--source_map_format=V3');
} }
getJavaOptions(function onJavaOptions(javaOptions) {
var compiler = cp.spawn('java', javaOptions.concat('-jar', closurePath, options)); var compiler = cp.spawn('java', javaOptions.concat('-jar', closurePath, options));
if (!this.isSilent) { if (!this.isSilent) {
console.log('Compressing ' + path.basename(outputPath, '.js') + ' using the Closure Compiler (' + mode + ')...'); console.log('Compressing ' + path.basename(outputPath, '.js') + ' using the Closure Compiler (' + mode + ')...');
@@ -437,6 +454,7 @@
// proxy the standard input to the Closure Compiler // proxy the standard input to the Closure Compiler
compiler.stdin.end(source); compiler.stdin.end(source);
}.bind(this));
} }
/** /**