Cleanup build/minify.js.

Former-commit-id: 08896dd9407afdbaa98a7d6011121e785e62ede9
This commit is contained in:
John-David Dalton
2012-11-09 04:46:01 -08:00
parent 6782bc8ca2
commit 14fb3bde60

View File

@@ -23,7 +23,7 @@
var closureOptions = ['--warning_level=QUIET']; var closureOptions = ['--warning_level=QUIET'];
/** The Closure Compiler optimization modes */ /** The Closure Compiler optimization modes */
var OPTIMIZATION_MODES = { var optimizationModes = {
'simple': 'SIMPLE_OPTIMIZATIONS', 'simple': 'SIMPLE_OPTIMIZATIONS',
'advanced': 'ADVANCED_OPTIMIZATIONS' 'advanced': 'ADVANCED_OPTIMIZATIONS'
}; };
@@ -100,15 +100,10 @@
options = source || options; options = source || options;
source = options.source || ''; source = options.source || '';
} }
this.compiled = { this.compiled = { 'simple': {}, 'advanced': {} };
'simple': {}, this.hybrid = { 'simple': {}, 'advanced': {} };
'advanced': {}
};
this.hybrid = {
'simple': {},
'advanced': {}
};
this.uglified = {}; this.uglified = {};
this.isSilent = !!options.isSilent; this.isSilent = !!options.isSilent;
this.isTemplate = !!options.isTemplate; this.isTemplate = !!options.isTemplate;
this.outputPath = options.outputPath; this.outputPath = options.outputPath;
@@ -132,25 +127,13 @@
* *
* @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 {String} mode The optimization mode.
* @param {String} [mode] The optimization mode.
* @param {Function} callback The function called once the process has completed. * @param {Function} callback The function called once the process has completed.
*/ */
function closureCompile(source, mode, message, callback) { function closureCompile(source, mode, callback) {
// use simple optimizations when minifying template files
var options = closureOptions.slice(); var options = closureOptions.slice();
options.push('--compilation_level=' + optimizationModes[this.isTemplate ? 'simple' : mode]);
// juggle arguments
if (typeof mode == 'function') {
callback = mode;
mode = null;
} else if (typeof message == 'function') {
callback = message;
message = null;
}
// use simple optimizations by default when minifying template files
mode = OPTIMIZATION_MODES[mode] || OPTIMIZATION_MODES[this.isTemplate ? 'simple' : 'advanced'];
options.push('--compilation_level=' + mode);
// the standard error stream, standard output stream, and the Closure Compiler process // the standard error stream, standard output stream, and the Closure Compiler process
var error = '', var error = '',
@@ -158,10 +141,7 @@
compiler = spawn('java', ['-jar', closurePath].concat(options)); compiler = spawn('java', ['-jar', closurePath].concat(options));
if (!this.isSilent) { if (!this.isSilent) {
console.log(message == null console.log('Compressing ' + path.basename(this.outputPath, '.js') + ' using the Closure Compiler (' + mode + ')...');
? 'Compressing ' + path.basename(this.outputPath, '.js') + ' using the Closure Compiler, `' + mode + '`...'
: 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
@@ -174,9 +154,8 @@
}); });
compiler.on('exit', function(status) { compiler.on('exit', function(status) {
var exception = null;
// `status` contains the process exit code // `status` contains the process exit code
var exception = null;
if (status) { if (status) {
exception = new Error(error); exception = new Error(error);
exception.status = status; exception.status = status;
@@ -195,24 +174,16 @@
* *
* @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 {String} label The label to log.
* @param {Function} callback The function called once the process has completed. * @param {Function} callback The function called once the process has completed.
*/ */
function uglify(source, message, callback) { function uglify(source, label, callback) {
var exception, var exception,
result, result,
ugly = uglifyJS.uglify; ugly = uglifyJS.uglify;
// juggle arguments
if (typeof message == 'function') {
callback = message;
message = null;
}
if (!this.isSilent) { if (!this.isSilent) {
console.log(message == null console.log('Compressing ' + path.basename(this.outputPath, '.js') + ' using ' + label + '...');
? 'Compressing ' + path.basename(this.outputPath, '.js') + ' using UglifyJS...'
: message
);
} }
try { try {
result = ugly.gen_code( result = ugly.gen_code(
@@ -237,7 +208,7 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/** /**
* The `closureCompile()` callback. * The Closure Compiler callback for simple optimizations.
* *
* @private * @private
* @param {Object|Undefined} exception The error object. * @param {Object|Undefined} exception The error object.
@@ -247,13 +218,13 @@
if (exception) { if (exception) {
throw exception; throw exception;
} }
// store the post-processed Closure Compiler result and gzip it result = postprocess(result);
this.compiled.simple.source = result = postprocess(result); this.compiled.simple.source = result;
gzip(result, onClosureSimpleGzip.bind(this)); gzip(result, onClosureSimpleGzip.bind(this));
} }
/** /**
* The Closure Compiler `gzip` callback. * The Closure Compiler `gzip` callback for simple optimizations.
* *
* @private * @private
* @param {Object|Undefined} exception The error object. * @param {Object|Undefined} exception The error object.
@@ -266,27 +237,35 @@
if (!this.isSilent) { if (!this.isSilent) {
console.log('Done. Size: %d bytes.', result.length); console.log('Done. Size: %d bytes.', result.length);
} }
// store the gzipped result and report the size
this.compiled.simple.gzip = result; this.compiled.simple.gzip = result;
// next, compile the source using advanced optimizations // next, compile the source using advanced optimizations
if (this.isTemplate) { closureCompile.call(this, this.source, 'advanced', onClosureAdvancedCompile.bind(this));
// jump directly to UglifyJS for templates.
uglify.call(this, this.source, onUglify.bind(this));
} else {
// otherwise, compress using advanced optimizations
closureCompile.call(this, this.source, 'advanced', onClosureAdvancedCompile.bind(this));
}
} }
/**
* The Closure Compiler callback for advanced optimizations.
*
* @private
* @param {Object|Undefined} exception The error object.
* @param {String} result The resulting minified source.
*/
function onClosureAdvancedCompile(exception, result) { function onClosureAdvancedCompile(exception, result) {
if (exception) { if (exception) {
throw exception; throw exception;
} }
this.compiled.advanced.source = result = postprocess(result); result = postprocess(result);
this.compiled.advanced.source = result;
gzip(result, onClosureAdvancedGzip.bind(this)); gzip(result, onClosureAdvancedGzip.bind(this));
} }
/**
* The Closure Compiler `gzip` callback for advanced optimizations.
*
* @private
* @param {Object|Undefined} exception The error object.
* @param {Buffer} result The resulting gzipped source.
*/
function onClosureAdvancedGzip(exception, result) { function onClosureAdvancedGzip(exception, result) {
if (exception) { if (exception) {
throw exception; throw exception;
@@ -294,15 +273,14 @@
if (!this.isSilent) { if (!this.isSilent) {
console.log('Done. Size: %d bytes.', result.length); console.log('Done. Size: %d bytes.', result.length);
} }
// store the gzipped result and report the size
this.compiled.advanced.gzip = result; this.compiled.advanced.gzip = result;
// next, minify the source using only UglifyJS // next, minify the source using only UglifyJS
uglify.call(this, this.source, onUglify.bind(this)); uglify.call(this, this.source, 'UglifyJS', onUglify.bind(this));
} }
/** /**
* The `uglify()` callback. * The UglifyJS callback.
* *
* @private * @private
* @param {Object|Undefined} exception The error object. * @param {Object|Undefined} exception The error object.
@@ -312,8 +290,8 @@
if (exception) { if (exception) {
throw exception; throw exception;
} }
// store the post-processed Uglified result and gzip it result = postprocess(result);
this.uglified.source = result = postprocess(result); this.uglified.source = result;
gzip(result, onUglifyGzip.bind(this)); gzip(result, onUglifyGzip.bind(this));
} }
@@ -331,17 +309,14 @@
if (!this.isSilent) { if (!this.isSilent) {
console.log('Done. Size: %d bytes.', result.length); console.log('Done. Size: %d bytes.', result.length);
} }
var message = 'Compressing ' + path.basename(this.outputPath, '.js') + ' using hybrid minification; `SIMPLE_OPTIMIZATIONS`...';
// store the gzipped result and report the size
this.uglified.gzip = result; this.uglified.gzip = result;
// next, minify the Closure Compiler simple minified source using UglifyJS // next, minify the already Closure Compiler simple optimized source using UglifyJS
uglify.call(this, this.compiled.simple.source, message, onSimpleHybrid.bind(this)); uglify.call(this, this.compiled.simple.source, 'hybrid (simple)', onSimpleHybrid.bind(this));
} }
/** /**
* The hybrid `uglify()` callback. * The hybrid callback for simple optimizations.
* *
* @private * @private
* @param {Object|Undefined} exception The error object. * @param {Object|Undefined} exception The error object.
@@ -351,19 +326,54 @@
if (exception) { if (exception) {
throw exception; throw exception;
} }
// store the post-processed Uglified result and gzip it result = postprocess(result);
this.hybrid.simple.source = result = postprocess(result); this.hybrid.simple.source = result;
gzip(result, onSimpleHybridGzip.bind(this)); gzip(result, onSimpleHybridGzip.bind(this));
} }
/**
* The hybrid `gzip` callback for simple optimizations.
*
* @private
* @param {Object|Undefined} exception The error object.
* @param {Buffer} result The resulting gzipped source.
*/
function onSimpleHybridGzip(exception, result) {
if (exception) {
throw exception;
}
if (!this.isSilent) {
console.log('Done. Size: %d bytes.', result.length);
}
this.hybrid.simple.gzip = result;
// next, minify the already Closure Compiler advance optimized source using UglifyJS
uglify.call(this, this.compiled.advanced.source, 'hybrid (advanced)', onAdvancedHybrid.bind(this));
}
/**
* The hybrid callback for advanced optimizations.
*
* @private
* @param {Object|Undefined} exception The error object.
* @param {String} result The resulting minified source.
*/
function onAdvancedHybrid(exception, result) { function onAdvancedHybrid(exception, result) {
if (exception) { if (exception) {
throw exception; throw exception;
} }
this.hybrid.advanced.source = result = postprocess(result); result = postprocess(result);
this.hybrid.advanced.source = result;
gzip(result, onAdvancedHybridGzip.bind(this)); gzip(result, onAdvancedHybridGzip.bind(this));
} }
/**
* The hybrid `gzip` callback for advanced optimizations.
*
* @private
* @param {Object|Undefined} exception The error object.
* @param {Buffer} result The resulting gzipped source.
*/
function onAdvancedHybridGzip(exception, result) { function onAdvancedHybridGzip(exception, result) {
if (exception) { if (exception) {
throw exception; throw exception;
@@ -378,37 +388,7 @@
} }
/** /**
* The hybrid `gzip` callback. * The callback executed after the source is minified and gzipped.
*
* @private
* @param {Object|Undefined} exception The error object.
* @param {Buffer} result The resulting gzipped source.
*/
function onSimpleHybridGzip(exception, result) {
if (exception) {
throw exception;
}
if (!this.isSilent) {
console.log('Done. Size: %d bytes.', result.length);
}
// store the gzipped result and report the size
this.hybrid.simple.gzip = result;
var message = 'Compressing ' + path.basename(this.outputPath, '.js') + ' using hybrid minification; `ADVANCED_OPTIMIZATIONS`...';
if (this.isTemplate) {
this.compiled.advanced = this.hybrid.advanced = {
'gzip': {
'length': Infinity
}
};
onComplete.call(this);
} else {
uglify.call(this, this.compiled.advanced.source, message, onAdvancedHybrid.bind(this));
}
}
/**
* The callback executed after JavaScript source is minified and gzipped.
* *
* @private * @private
*/ */
@@ -429,18 +409,12 @@
hybridAdvanced.gzip.length hybridAdvanced.gzip.length
); );
// pass the minified source to the minify instances "onComplete" callback // pass the minified source to the "onComplete" callback
this.onComplete( [compiledSimple, compiledAdvanced, uglified, hybridSimple, hybridAdvanced].some(function(data) {
compiledSimple.gzip.length == min if (data.gzip.length == min) {
? compiledSimple.source this.onComplete(data.source);
: compiledAdvanced.gzip.length == min }
? compiledAdvanced.source }, this);
: uglified.gzip.length == min
? uglified.source
: hybridSimple.gzip.length == min
? hybridSimple.source
: hybridAdvanced.source
);
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/