Merge pull request #2 from bestiejs/autominification

lodash: Make build.js automatically choose the best minification, Uglify or Closure Compiler, and use it for lodash.min.js. [jddalton, kitcambridge]
Former-commit-id: fb6fd67dc97d8ac379a580e15cd430593d88966c
This commit is contained in:
John-David Dalton
2012-04-23 20:18:46 -07:00
3 changed files with 90 additions and 118 deletions

200
build.js
View File

@@ -26,114 +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);
}
/**
* Compresses a `source` string using UglifyJS. Yields the result to a
* `callback` function. This function is synchronous; the `callback` is used
* for symmetry.
*
* @private
* @param {String} source The JavaScript source to minify.
* @param {Function} callback The function called when minifying is complete.
*/
function uglify(source, callback) {
var ugly = uglifyJS.uglify;
var result = 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
});
// split lines at 500 characters to be consistent with Closure Compiler
callback(ugly.split_lines(result, 500));
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -144,35 +82,69 @@
} }
// compress and `gzip` Lo-Dash using the Closure Compiler // compress and `gzip` Lo-Dash using the Closure Compiler
compile(source, function(exception, result) { 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;
} }
// post-process the minified source
var source = postprocess(result);
// save the final minified version // post-process and `gzip` the compiled distribution
fs.writeFileSync(path.join(distPath, 'lodash.compiler.js'), source); compiledSource = postprocess(compiledSource);
invoke('gzip', ['-9f', '-c'], compiledSource, 'binary', function onClosureCompress(exception, compiledGzippedSource) {
// save the `gzip`-ed version var compiledSize, ugly, uglifiedSource;
gzip(source, function(exception, result) {
if (exception) { if (exception) {
throw exception; throw exception;
} }
// explicit `binary` encoding is necessary to ensure that the stream is written correctly
fs.writeFileSync(path.join(distPath, 'lodash.compiler.js.gz'), result, 'binary');
});
});
// compress and `gzip` Lo-Dash using UglifyJS // store and print the `gzip`-ped size of the compiled distribution
uglify(source, function(result) { compiledSize = compiledGzippedSource.length;
var source = postprocess(result); console.log('Done. Size: %d KB.', compiledSize);
fs.writeFileSync(path.join(distPath, 'lodash.uglify.js'), source);
gzip(source, function(exception, result) { // compress Lo-Dash using UglifyJS
if (exception) { console.log('Compressing Lodash using UglifyJS...');
throw exception; ugly = uglifyJS.uglify;
}
fs.writeFileSync(path.join(distPath, 'lodash.uglify.js.gz'), result, 'binary'); 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 and `gzip` the uglified distribution. Lines are
// restricted to 500 characters for consistency with Closure Compiler
uglifiedSource = postprocess(ugly.split_lines(uglifiedSource, 500));
invoke('gzip', ['-9f', '-c'], uglifiedSource, 'binary', function onUglifyCompress(exception, uglifiedGzippedSource) {
var uglifiedSize;
if (exception) {
throw exception;
}
// store and print the `gzip`-ped size of the uglified distribution
uglifiedSize = uglifiedGzippedSource.length;
console.log('Done. Size: %d KB.', uglifiedSize);
// save the compiled version to disk. The explicit `binary`
// encoding for the `gzip`-ped version is necessary to ensure that
// 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);
});
}); });
}); });
}()); }());

View File

@@ -39,9 +39,9 @@
if (module != require.main) { if (module != require.main) {
module.exports = postprocess; module.exports = postprocess;
} else { } else {
// Read the JavaScript source file from the first argument if the script // read the JavaScript source file from the first argument if the script
// was invoked directly (e.g. `node post-compile.js source.js`) and write to // was invoked directly (e.g. `node post-compile.js source.js`) and write to
// the same file. // the same file
(function() { (function() {
var source = fs.readFileSync(process.argv[2], 'utf8'); var source = fs.readFileSync(process.argv[2], 'utf8');
fs.writeFileSync(process.argv[2], postprocess(source), 'utf8'); fs.writeFileSync(process.argv[2], postprocess(source), 'utf8');

View File

@@ -199,9 +199,9 @@
if (module != require.main) { if (module != require.main) {
module.exports = preprocess; module.exports = preprocess;
} else { } else {
// Read the JavaScript source file from the first argument if the script // read the JavaScript source file from the first argument if the script
// was invoked directly (e.g. `node pre-compile.js source.js`) and write to // was invoked directly (e.g. `node pre-compile.js source.js`) and write to
// the same file. // the same file
(function() { (function() {
var source = fs.readFileSync(process.argv[2], 'utf8'); var source = fs.readFileSync(process.argv[2], 'utf8');
fs.writeFileSync(process.argv[2], preprocess(source), 'utf8'); fs.writeFileSync(process.argv[2], preprocess(source), 'utf8');