Cleanup build script comments and fix Closure Compiler bugs.

Former-commit-id: 84771ac79d6cfd7ec3b0d29586edf58d617d5577
This commit is contained in:
John-David Dalton
2012-10-23 23:54:53 -07:00
parent ad9ddc5621
commit eccf92ebaf
4 changed files with 28 additions and 19 deletions

View File

@@ -12,14 +12,14 @@
/** The current working directory */ /** The current working directory */
var cwd = process.cwd(); var cwd = process.cwd();
/** Shortcut to native `Array.prototype` */ /** Used for array method references */
var ArrayProto = Array.prototype; var arrayRef = [];
/** Shortcut used to push arrays of values to an array */ /** Shortcut used to push arrays of values to an array */
var push = ArrayProto.push; var push = arrayRef.push;
/** Shortcut used to convert array-like objects to arrays */ /** Shortcut used to convert array-like objects to arrays */
var slice = ArrayProto.slice; var slice = arrayRef.slice;
/** Shortcut to the `stdout` object */ /** Shortcut to the `stdout` object */
var stdout = process.stdout; var stdout = process.stdout;
@@ -792,7 +792,7 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/** /**
* Creates a debug and minified build, executing the `callback` for each. * Creates a debug and/or minified build, executing the `callback` for each.
* The `callback` is invoked with two arguments; (filePath, outputSource). * The `callback` is invoked with two arguments; (filePath, outputSource).
* *
* Note: For a list of commands see `displayHelp()` or run `lodash --help`. * Note: For a list of commands see `displayHelp()` or run `lodash --help`.
@@ -1065,7 +1065,7 @@
' function difference(array) {', ' function difference(array) {',
' var index = -1,', ' var index = -1,',
' length = array.length,', ' length = array.length,',
' flattened = concat.apply(ArrayProto, arguments),', ' flattened = concat.apply(arrayRef, arguments),',
' result = [];', ' result = [];',
'', '',
' while (++index < length) {', ' while (++index < length) {',
@@ -1121,7 +1121,7 @@
// replace `_.omit` // replace `_.omit`
source = source.replace(/^( +)function omit[\s\S]+?\n\1}/m, [ source = source.replace(/^( +)function omit[\s\S]+?\n\1}/m, [
' function omit(object) {', ' function omit(object) {',
' var props = concat.apply(ArrayProto, arguments),', ' var props = concat.apply(arrayRef, arguments),',
' result = {};', ' result = {};',
'', '',
' forIn(object, function(value, key) {', ' forIn(object, function(value, key) {',
@@ -1137,7 +1137,7 @@
source = source.replace(/^( +)function pick[\s\S]+?\n\1}/m, [ source = source.replace(/^( +)function pick[\s\S]+?\n\1}/m, [
' function pick(object) {', ' function pick(object) {',
' var index = 0,', ' var index = 0,',
' props = concat.apply(ArrayProto, arguments),', ' props = concat.apply(arrayRef, arguments),',
' length = props.length,', ' length = props.length,',
' result = {};', ' result = {};',
'', '',
@@ -1647,10 +1647,6 @@
'isTemplate': isTemplate, 'isTemplate': isTemplate,
'outputPath': outputPath, 'outputPath': outputPath,
'onComplete': function(source) { 'onComplete': function(source) {
// correct overly aggressive Closure Compiler minification
if (!isTemplate) {
source = source.replace(/prototype\s*=\s*{\s*valueOf\s*:\s*1\s*}/, 'prototype={valueOf:1,y:1}');
}
// inject "use strict" directive // inject "use strict" directive
if (isStrict) { if (isStrict) {
source = source.replace(/^([\s\S]*?function[^{]+{)([^'"])/, '$1"use strict";$2'); source = source.replace(/^([\s\S]*?function[^{]+{)([^'"])/, '$1"use strict";$2');

View File

@@ -19,7 +19,7 @@
postprocess = require('./post-compile'), postprocess = require('./post-compile'),
uglifyJS = require('../vendor/uglifyjs/uglify-js'); uglifyJS = require('../vendor/uglifyjs/uglify-js');
/** Closure Compiler command-line options */ /** The Closure Compiler command-line options */
var closureOptions = [ var closureOptions = [
'--compilation_level=ADVANCED_OPTIMIZATIONS', '--compilation_level=ADVANCED_OPTIMIZATIONS',
'--warning_level=QUIET' '--warning_level=QUIET'
@@ -135,7 +135,7 @@
return value.replace(/^(--compilation_level)=.+$/, '$1=SIMPLE_OPTIMIZATIONS'); return value.replace(/^(--compilation_level)=.+$/, '$1=SIMPLE_OPTIMIZATIONS');
}); });
} }
// the standard error stream, standard output stream, and Closure Compiler process // the standard error stream, standard output stream, and the Closure Compiler process
var error = '', var error = '',
output = '', output = '',
compiler = spawn('java', ['-jar', closurePath].concat(options)); compiler = spawn('java', ['-jar', closurePath].concat(options));
@@ -348,7 +348,7 @@
uglified = this.uglified; uglified = this.uglified;
// 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 the Closure Compiler)
var min = Math.min(compiled.gzip.length, hybrid.gzip.length, uglified.gzip.length); var min = Math.min(compiled.gzip.length, hybrid.gzip.length, uglified.gzip.length);
// pass the minified source to the minify instances "onComplete" callback // pass the minified source to the minify instances "onComplete" callback

View File

@@ -26,9 +26,17 @@
* @returns {String} Returns the processed source. * @returns {String} Returns the processed source.
*/ */
function postprocess(source) { function postprocess(source) {
// move vars exposed by Closure Compiler into the IIFE // move vars exposed by the Closure Compiler into the IIFE
source = source.replace(/^((?:(['"])use strict\2;)?(?:var (?:[a-z]+=(?:!0|!1|null)[,;])+)?)([\s\S]*?function[^)]+\){)/, '$3$1'); source = source.replace(/^((?:(['"])use strict\2;)?(?:var (?:[a-z]+=(?:!0|!1|null)[,;])+)?)([\s\S]*?function[^)]+\){)/, '$3$1');
// avoid bugs with the Closure Compiler
source = source
// correct overly aggressive Closure Compiler minification
.replace(/prototype\s*=\s*{\s*valueOf\s*:\s*1\s*}/, 'prototype={valueOf:1,y:1}')
// restore `arrayRef` and `objectRef` values modified by pre-compile.js
.replace(/= *Array.prototype/, '=[]')
.replace(/= *Object.prototype/, '={}');
// unescape properties (i.e. foo["bar"] => foo.bar) // unescape properties (i.e. foo["bar"] => foo.bar)
source = source.replace(/(\w)\["([^."]+)"\]/g, function(match, left, right) { source = source.replace(/(\w)\["([^."]+)"\]/g, function(match, left, right) {
return /\W/.test(right) ? match : (left + '.' + right); return /\W/.test(right) ? match : (left + '.' + right);

View File

@@ -206,7 +206,7 @@
source || (source = ''); source || (source = '');
options || (options = {}); options || (options = {});
// remove unrecognized JSDoc tags so Closure Compiler won't complain // remove unrecognized JSDoc tags so the Closure Compiler won't complain
source = source.replace(/@(?:alias|category)\b.*/g, ''); source = source.replace(/@(?:alias|category)\b.*/g, '');
if (options.isTemplate) { if (options.isTemplate) {
@@ -216,7 +216,12 @@
// remove copyright to add later in post-compile.js // remove copyright to add later in post-compile.js
source = source.replace(/\/\*![\s\S]+?\*\//, ''); source = source.replace(/\/\*![\s\S]+?\*\//, '');
// add brackets to whitelisted properties so Closure Compiler won't mung them // replace `arrayRef` and `objectRef` values to avoid a bug in the Closure Compiler
source = source
.replace(/(arrayRef *= *)\[\]/, '$1Array.prototype')
.replace(/(objectRef *= *)\{\}/, '$1Object.prototype');
// add brackets to whitelisted properties so the Closure Compiler won't mung them
// http://code.google.com/closure/compiler/docs/api-tutorial3.html#export // http://code.google.com/closure/compiler/docs/api-tutorial3.html#export
source = source.replace(RegExp('\\.(' + propWhitelist.join('|') + ')\\b', 'g'), "['$1']"); source = source.replace(RegExp('\\.(' + propWhitelist.join('|') + ')\\b', 'g'), "['$1']");
@@ -314,7 +319,7 @@
isIteratorTemplate = /var iteratorTemplate\b/.test(snippet), isIteratorTemplate = /var iteratorTemplate\b/.test(snippet),
modified = snippet; modified = snippet;
// add brackets to whitelisted properties so Closure Compiler won't mung them // add brackets to whitelisted properties so the Closure Compiler won't mung them
modified = modified.replace(RegExp('\\.(' + iteratorOptions.join('|') + ')\\b', 'g'), "['$1']"); modified = modified.replace(RegExp('\\.(' + iteratorOptions.join('|') + ')\\b', 'g'), "['$1']");
if (isCreateIterator) { if (isCreateIterator) {