Clean up build.js. The build process now delays writing the compressed and gzip-ped versions to disk until all build stages have finished successfully.

Former-commit-id: 498165a725e8b1023b05d2a737fdac6d6a8c46ea
This commit is contained in:
Kit Cambridge
2012-04-23 16:41:04 -06:00
parent bd70d31a72
commit 2f91764f80

183
build.js
View File

@@ -26,85 +26,52 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/** /**
* Compresses a `source` string using the Closure Compiler. Yields the * Invokes a process with the given `name`, `parameters`, and `source` (used as
* minified result, and any exceptions encountered, to a `callback` function. * the standard input). Yields the result to a `callback` function. The optional
* `encoding` argument specifies the output stream encoding.
* *
* @private * @private
* @param {String} source The JavaScript source to minify. * @param {String} name The name of the process.
* @param {Function} callback The function called when minifying is complete. * @param {Array} parameters An array of arguments to proxy to the process.
* @param {String} source The standard input to proxy to the process.
* @param {String} [encoding] The expected encoding of the output stream.
* @param {Function} callback The function to call once the process completes.
*/ */
function compile(source, callback) { function invoke(name, parameters, source, encoding, callback) {
var stderr = '', // the process instance and its standard output and error streams
stdout = ''; var process = spawn(name, parameters),
results = '', error = '';
var compiler = spawn('java', [ if (typeof encoding == 'string' && callback != null) {
// load the Closure Compiler and set the compression options // explicitly set the encoding of the output stream if one is specified
'-jar', path.join(__dirname, 'vendor', 'closure-compiler', 'compiler.jar'), process.stdout.setEncoding(encoding);
'--compilation_level=ADVANCED_OPTIMIZATIONS', } else {
'--language_in=ECMASCRIPT5_STRICT', callback = encoding;
'--warning_level=QUIET' encoding = null;
]); }
// explicitly set the encoding of the output and error streams process.stdout.on('data', function onData(data) {
compiler.stdout.setEncoding('utf8'); // append the compiled source to the output stream
compiler.stderr.setEncoding('utf8'); results += data;
compiler.stdout.on('data', function(data) {
stdout += data;
}); });
compiler.stderr.on('data', function(data) { process.stderr.on('data', function onError(data) {
stderr += data; // append the error message to the error stream
error += data;
}); });
compiler.on('exit', function(status) { process.on('exit', function onExit(status) {
var exception = null; var exception = null;
// `status` contains the process exit code
if (status) { if (status) {
exception = new Error(stderr); exception = new Error(error);
exception.status = status; exception.status = status;
} }
callback(exception, stdout); callback(exception, results);
}); });
// proxy the source string to Closure Compiler // proxy the standard input to the process
compiler.stdin.end(source); process.stdin.end(source);
}
/**
* Compresses a `source` string using the Unix `gzip` commands. Yields the
* result, and any exceptions encountered, to a `callback` function.
*
* @private
* @param {String} source The JavaScript source to gzip.
* @param {Function} callback The function called when gzipping is complete.
*/
function gzip(source, callback) {
var compressor = spawn('gzip', ['-9f', '-c']),
stderr = '',
stdout = '';
compressor.stdout.setEncoding('binary');
compressor.stderr.setEncoding('utf8');
compressor.stdout.on('data', function(data) {
stdout += data;
});
compressor.stderr.on('data', function(data) {
stderr += data;
});
compressor.on('exit', function(status) {
var exception = null;
if (status) {
exception = new Error(stderr);
exception.status = status;
}
callback(exception, stdout);
});
// proxy the source string to the `gzip` executable
compressor.stdin.end(source);
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -115,70 +82,68 @@
} }
// compress and `gzip` Lo-Dash using the Closure Compiler // compress and `gzip` Lo-Dash using the Closure Compiler
compile(source, function(exception, compiledSource) { console.log('Compressing Lodash using the Closure Compiler...');
invoke('java', ['-jar', path.join(__dirname, 'vendor', 'closure-compiler', 'compiler.jar'), '--compilation_level=ADVANCED_OPTIMIZATIONS', '--language_in=ECMASCRIPT5_STRICT', '--warning_level=QUIET'], source, function onClosureCompile(exception, compiledSource) {
if (exception) { if (exception) {
throw exception; throw exception;
} }
console.log("Compressing Lodash using the Closure Compiler..."); // post-process and `gzip` the compiled distribution
// post-process the compiled source
compiledSource = postprocess(compiledSource); compiledSource = postprocess(compiledSource);
invoke('gzip', ['-9f', '-c'], compiledSource, 'binary', function onClosureCompress(exception, compiledGzippedSource) {
// save the final compiled version var compiledSize, ugly, uglifiedSource;
fs.writeFileSync(path.join(distPath, 'lodash.compiler.js'), compiledSource);
// `gzip` the compiled version
gzip(compiledSource, function gzipCompiled(exception, result) {
if (exception) { if (exception) {
throw exception; throw exception;
} }
// record the size of the compiled version // store and print the `gzip`-ped size of the compiled distribution
var compiledSize = result.length; compiledSize = compiledGzippedSource.length;
console.log('Done. Size: %d KB.', compiledSize);
// explicit `binary` encoding is necessary to ensure that the stream is written correctly
fs.writeFileSync(path.join(distPath, 'lodash.compiler.js.gz'), result, 'binary');
console.log("Done. Size: %d KB.", compiledSize);
// compress Lo-Dash using UglifyJS // compress Lo-Dash using UglifyJS
var ugly = uglifyJS.uglify, console.log('Compressing Lodash using UglifyJS...');
uglifiedSource = ugly.gen_code( ugly = uglifyJS.uglify;
// enable unsafe transformations
ugly.ast_squeeze_more(
ugly.ast_squeeze(
// munge variable and function names, excluding the special `define`
// function exposed by AMD loaders
ugly.ast_mangle(uglifyJS.parser.parse(source), {
'except': ['define']
}
))), {
'ascii_only': true
});
console.log("Compressing Lodash using UglifyJS..."); uglifiedSource = ugly.gen_code(
// enable unsafe transformations
ugly.ast_squeeze_more(
ugly.ast_squeeze(
// munge variable and function names, excluding the special `define`
// function exposed by AMD loaders
ugly.ast_mangle(uglifyJS.parser.parse(source), {
'except': ['define']
}
))), {
'ascii_only': true
});
// post-process the uglified source and split lines at 500 characters for // post-process and `gzip` the uglified distribution. Lines are
// consistency with Closure Compiler // restricted to 500 characters for consistency with Closure Compiler.
uglifiedSource = postprocess(ugly.split_lines(uglifiedSource, 500)); uglifiedSource = postprocess(ugly.split_lines(uglifiedSource, 500));
invoke('gzip', ['-9f', '-c'], uglifiedSource, 'binary', function onUglifyCompress(exception, uglifiedGzippedSource) {
// save the uglified version var uglifiedSize;
fs.writeFileSync(path.join(distPath, 'lodash.uglify.js'), uglifiedSource);
// `gzip` the uglified version
gzip(uglifiedSource, function gzipUglified(exception, result) {
if (exception) { if (exception) {
throw exception; throw exception;
} }
var uglifiedSize = result.length;
fs.writeFileSync(path.join(distPath, 'lodash.uglify.js.gz'), result, 'binary');
console.log("Done. Size: %d KB.", uglifiedSize); // store and print the `gzip`-ped size of the uglified distribution
uglifiedSize = uglifiedGzippedSource.length;
console.log('Done. Size: %d KB.', uglifiedSize);
// select the smallest minified distribution and use it as the official // save the compiled version to disk. The explicit `binary`
// minified release // encoding for the `gzip`-ped version is necessary to ensure that
fs.writeFileSync(path.join(__dirname, "lodash.min.js"), compiledSize < uglifiedSize ? compiledSource : uglifiedSource); // the stream is written correctly.
fs.writeFileSync(path.join(distPath, 'lodash.compiler.js'), compiledSource);
fs.writeFileSync(path.join(distPath, 'lodash.compiler.js.gz'), compiledGzippedSource, 'binary');
// save the uglified version to disk.
fs.writeFileSync(path.join(distPath, 'lodash.uglify.js'), uglifiedSource);
fs.writeFileSync(path.join(distPath, 'lodash.uglify.js.gz'), uglifiedGzippedSource, 'binary');
// select the smallest minified distribution and use it as the
// official minified release. If they are equivalent, the compiled
// distribution is used.
fs.writeFileSync(path.join(__dirname, 'lodash.min.js'), compiledSize < uglifiedSize ? compiledSource : uglifiedSource);
}); });
}); });
}); });