lodash: Add a combined minified mode. [jddalton]

Former-commit-id: ba37c06ce553a2e8f366952432ad7fb9903ca577
This commit is contained in:
John-David Dalton
2012-05-07 13:50:18 -04:00
parent 98942c5e1a
commit 21b86980a7

View File

@@ -24,6 +24,7 @@
/** Used to shares values between multiple callbacks */ /** Used to shares values between multiple callbacks */
var accumulator = { var accumulator = {
'combined': {},
'compiled': {}, 'compiled': {},
'uglified': {} 'uglified': {}
}; };
@@ -46,16 +47,23 @@
* *
* @private * @private
* @param {String} source The JavaScript source to minify. * @param {String} source The JavaScript source to minify.
* @param {String} [message] The message to log.
* @param {Function} callback The function to call once the process completes. * @param {Function} callback The function to call once the process completes.
*/ */
function closureCompile(source, callback) { function closureCompile(source, message, callback) {
console.log('Compressing lodash.js using the Closure Compiler...');
// the standard error stream, standard output stream, and Closure Compiler process // the standard error stream, standard output stream, and Closure Compiler process
var error = '', var error = '',
output = '', output = '',
compiler = spawn('java', ['-jar', closurePath].concat(closureOptions)); compiler = spawn('java', ['-jar', closurePath].concat(closureOptions));
// juggle arguments
if (typeof message == 'function') {
callback = message;
message = null;
}
console.log(message == null ? 'Compressing lodash.js using the Closure Compiler...' : message);
compiler.stdout.on('data', function(data) { compiler.stdout.on('data', function(data) {
// append the data to the output stream // append the data to the output stream
output += data; output += data;
@@ -88,14 +96,21 @@
* *
* @private * @private
* @param {String} source The JavaScript source to minify. * @param {String} source The JavaScript source to minify.
* @param {String} [message] The message to log.
* @param {Function} callback The function to call once the process completes. * @param {Function} callback The function to call once the process completes.
*/ */
function uglify(source, callback) { function uglify(source, message, callback) {
var exception, var exception,
result, result,
ugly = uglifyJS.uglify; ugly = uglifyJS.uglify;
console.log('Compressing lodash.js using UglifyJS...'); // juggle arguments
if (typeof message == 'function') {
callback = message;
message = null;
}
console.log(message == null ? 'Compressing lodash.js using UglifyJS...' : message);
try { try {
result = ugly.gen_code( result = ugly.gen_code(
@@ -150,7 +165,7 @@
accumulator.compiled.gzip = result; accumulator.compiled.gzip = result;
console.log('Done. Size: %d KB.', result.length); console.log('Done. Size: %d KB.', result.length);
// next, minify using UglifyJS // next, minify the source using only UglifyJS
uglify(source, onUglify); uglify(source, onUglify);
} }
@@ -181,10 +196,47 @@
if (exception) { if (exception) {
throw exception; throw exception;
} }
var message = 'Compressing lodash.js combining Closure Compiler and UglifyJS...';
// store the gzipped result and report the size // store the gzipped result and report the size
accumulator.uglified.gzip = result; accumulator.uglified.gzip = result;
console.log('Done. Size: %d KB.', result.length); console.log('Done. Size: %d KB.', result.length);
// next, minify the Closure Compiler minified source using UglifyJS
uglify(accumulator.compiled.source, message, onCombine);
}
/**
* The combined `uglify()` callback.
*
* @private
* @param {Object|Undefined} exception The error object.
* @param {String} result The resulting minified source.
*/
function onCombine(exception, result) {
if (exception) {
throw exception;
}
// store the post-processed Uglified result and gzip it
accumulator.combined.source = result = postprocess(result);
gzip(result, onCombineGzip);
}
/**
* The combined `gzip` callback.
*
* @private
* @param {Object|Undefined} exception The error object.
* @param {Buffer} result The resulting gzipped source.
*/
function onCombineGzip(exception, result) {
if (exception) {
throw exception;
}
// store the gzipped result and report the size
accumulator.combined.gzip = result;
console.log('Done. Size: %d KB.', result.length);
// finish by choosing the smallest compressed file // finish by choosing the smallest compressed file
onComplete(); onComplete();
} }
@@ -195,7 +247,8 @@
* @private * @private
*/ */
function onComplete() { function onComplete() {
var compiled = accumulator.compiled, var combined = accumulator.combined,
compiled = accumulator.compiled,
uglified = accumulator.uglified; uglified = accumulator.uglified;
// save the Closure Compiled version to disk // save the Closure Compiled version to disk
@@ -206,12 +259,20 @@
fs.writeFileSync(path.join(distPath, 'lodash.uglify.js'), uglified.source); fs.writeFileSync(path.join(distPath, 'lodash.uglify.js'), uglified.source);
fs.writeFileSync(path.join(distPath, 'lodash.uglify.js.gz'), uglified.gzip); fs.writeFileSync(path.join(distPath, 'lodash.uglify.js.gz'), uglified.gzip);
// save the Combined minified version to disk
fs.writeFileSync(path.join(distPath, 'lodash.combined.js'), combined.source);
fs.writeFileSync(path.join(distPath, 'lodash.combined.js.gz'), combined.gzip);
// select the smallest gzipped file and use its minified counterpart as the // select the smallest gzipped file and use its minified counterpart as the
// official minified release (ties go to Closure Compiler) // official minified release (ties go to Closure Compiler)
var min = Math.min(combined.gzip.length, compiled.gzip.length, uglified.gzip.length);
fs.writeFileSync(path.join(__dirname, 'lodash.min.js'), fs.writeFileSync(path.join(__dirname, 'lodash.min.js'),
uglified.gzip.length < compiled.gzip.length compiled.gzip.length == min
? uglified.source ? compiled.source
: compiled.source : uglified.gzip.length == min
? uglified.source
: combined.source
); );
} }