From eccf92ebaf684e95cc5fc9957d88d9fa9cb297c2 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 23 Oct 2012 23:54:53 -0700 Subject: [PATCH] Cleanup build script comments and fix Closure Compiler bugs. Former-commit-id: 84771ac79d6cfd7ec3b0d29586edf58d617d5577 --- build.js | 20 ++++++++------------ build/minify.js | 6 +++--- build/post-compile.js | 10 +++++++++- build/pre-compile.js | 11 ++++++++--- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/build.js b/build.js index c73793cda..2bfe33dbd 100755 --- a/build.js +++ b/build.js @@ -12,14 +12,14 @@ /** The current working directory */ var cwd = process.cwd(); - /** Shortcut to native `Array.prototype` */ - var ArrayProto = Array.prototype; + /** Used for array method references */ + var arrayRef = []; /** 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 */ - var slice = ArrayProto.slice; + var slice = arrayRef.slice; /** Shortcut to the `stdout` object */ 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). * * Note: For a list of commands see `displayHelp()` or run `lodash --help`. @@ -1065,7 +1065,7 @@ ' function difference(array) {', ' var index = -1,', ' length = array.length,', - ' flattened = concat.apply(ArrayProto, arguments),', + ' flattened = concat.apply(arrayRef, arguments),', ' result = [];', '', ' while (++index < length) {', @@ -1121,7 +1121,7 @@ // replace `_.omit` source = source.replace(/^( +)function omit[\s\S]+?\n\1}/m, [ ' function omit(object) {', - ' var props = concat.apply(ArrayProto, arguments),', + ' var props = concat.apply(arrayRef, arguments),', ' result = {};', '', ' forIn(object, function(value, key) {', @@ -1137,7 +1137,7 @@ source = source.replace(/^( +)function pick[\s\S]+?\n\1}/m, [ ' function pick(object) {', ' var index = 0,', - ' props = concat.apply(ArrayProto, arguments),', + ' props = concat.apply(arrayRef, arguments),', ' length = props.length,', ' result = {};', '', @@ -1647,10 +1647,6 @@ 'isTemplate': isTemplate, 'outputPath': outputPath, '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 if (isStrict) { source = source.replace(/^([\s\S]*?function[^{]+{)([^'"])/, '$1"use strict";$2'); diff --git a/build/minify.js b/build/minify.js index 8018390fc..9fabb7821 100755 --- a/build/minify.js +++ b/build/minify.js @@ -19,7 +19,7 @@ postprocess = require('./post-compile'), uglifyJS = require('../vendor/uglifyjs/uglify-js'); - /** Closure Compiler command-line options */ + /** The Closure Compiler command-line options */ var closureOptions = [ '--compilation_level=ADVANCED_OPTIMIZATIONS', '--warning_level=QUIET' @@ -135,7 +135,7 @@ 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 = '', output = '', compiler = spawn('java', ['-jar', closurePath].concat(options)); @@ -348,7 +348,7 @@ uglified = this.uglified; // 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); // pass the minified source to the minify instances "onComplete" callback diff --git a/build/post-compile.js b/build/post-compile.js index 5d616c77b..63ed8b7f2 100644 --- a/build/post-compile.js +++ b/build/post-compile.js @@ -26,9 +26,17 @@ * @returns {String} Returns the processed 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'); + // 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) source = source.replace(/(\w)\["([^."]+)"\]/g, function(match, left, right) { return /\W/.test(right) ? match : (left + '.' + right); diff --git a/build/pre-compile.js b/build/pre-compile.js index db9c1012c..89b63f4ad 100644 --- a/build/pre-compile.js +++ b/build/pre-compile.js @@ -206,7 +206,7 @@ source || (source = ''); 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, ''); if (options.isTemplate) { @@ -216,7 +216,12 @@ // remove copyright to add later in post-compile.js 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 source = source.replace(RegExp('\\.(' + propWhitelist.join('|') + ')\\b', 'g'), "['$1']"); @@ -314,7 +319,7 @@ isIteratorTemplate = /var iteratorTemplate\b/.test(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']"); if (isCreateIterator) {