From 82bc52b909cd4707d530c8ba2d9a2e690cc0b2b9 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 29 Sep 2012 12:04:40 -0700 Subject: [PATCH] Reduce underscore build size. Former-commit-id: 207d4ab49063483245dc951d4646413d6d4a1903 --- build.js | 16 +++++++++++++++- build/pre-compile.js | 2 +- lodash.js | 17 ++++++++--------- test/test-build.js | 20 ++++++++++++++------ 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/build.js b/build.js index 774bc3633..9728c9646 100755 --- a/build.js +++ b/build.js @@ -630,7 +630,7 @@ return source; } // remove function - source = source.replace(matchFunction(source, funcName), ''); + source = source.replace(snippet, ''); // grab the method assignments snippet snippet = getMethodAssignments(source); @@ -1054,6 +1054,11 @@ // replace `arrayLikeClasses` in `_.isEqual` source = source.replace(/(?: *\/\/.*\n)*( +)var isArr *= *arrayLikeClasses[^}]+}/, '$1var isArr = isArray(a);'); + // remove "exit early" feature from `_.each` + source = source.replace(/( )+var baseIteratorOptions *=[\s\S]+?\n\1.+?;/, function(match) { + return match.replace(/if *\(callback[^']+/, 'callback(value, index, collection)'); + }); + // remove `deep` clone functionality source = source.replace(/( +)function clone[\s\S]+?\n\1}/, [ ' function clone(value) {', @@ -1094,6 +1099,15 @@ source = buildTemplate(templatePattern, templateSettings); } else { + // simplify template snippets by removing unnecessary brackets + source = source.replace( + RegExp("{(\\\\n' *\\+\\s*.*?\\+\\n\\s*' *)}(?:\\\\n)?' *([,\\n])", 'g'), "$1'$2" + ); + + source = source.replace( + RegExp("{(\\\\n' *\\+\\s*.*?\\+\\n\\s*' *)}(?:\\\\n)?' *\\+", 'g'), "$1;\\n'+" + ); + // remove methods from the build allMethods.forEach(function(otherName) { if (!_.contains(buildMethods, otherName)) { diff --git a/build/pre-compile.js b/build/pre-compile.js index 879e9149a..dfe28763c 100644 --- a/build/pre-compile.js +++ b/build/pre-compile.js @@ -274,7 +274,7 @@ // remove whitespace from string literals source = source.replace(/'(?:(?=(\\?))\1.)*?'/g, function(string) { // avoids removing the '\n' of the `stringEscapes` object - return string.replace(/\[object |delete |else if|else var |function | in |return\s+[\w']|throw |typeof |use strict|var |@ |'\\n'|\\\\n|\\n|\s+/g, function(match) { + return string.replace(/\[object |delete |else |function | in |return\s+[\w']|throw |typeof |use strict|var |@ |'\\n'|\\\\n|\\n|\s+/g, function(match) { return match == false || match == '\\n' ? '' : match; }); }); diff --git a/lodash.js b/lodash.js index e1468cb09..f8c337329 100644 --- a/lodash.js +++ b/lodash.js @@ -667,11 +667,13 @@ function createCallback(func, thisArg) { if (!func) { return identity; - } else if (typeof func != 'function') { + } + if (typeof func != 'function') { return function(object) { return object[func]; - } - } else if (thisArg !== undefined) { + }; + } + if (thisArg !== undefined) { return function(value, index, object) { return func.call(thisArg, value, index, object); }; @@ -1693,13 +1695,10 @@ * // => ['one', 'two', 'three'] (order is not guaranteed) */ var keys = !nativeKeys ? shimKeys : function(object) { - var type = typeof object; - // avoid iterating over the `prototype` property - if (type == 'function' && propertyIsEnumerable.call(object, 'prototype')) { - return shimKeys(object); - } - return nativeKeys(object); + return typeof object == 'function' && propertyIsEnumerable.call(object, 'prototype') + ? shimKeys(object) + : nativeKeys(object); }; /** diff --git a/test/test-build.js b/test/test-build.js index 14109d276..2f536af71 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -436,7 +436,7 @@ console.log(e); pass = false; } - equal(pass, true, methodName + ': ' + message); + equal(pass, true, '_.' + methodName + ': ' + message); } /*--------------------------------------------------------------------------*/ @@ -545,17 +545,25 @@ var start = _.after(2, _.once(QUnit.start)); build(['-s', 'underscore'], function(source, filepath) { - var array = [{ 'a': 1 }], + var last, + array = [{ 'value': 1 }, { 'value': 2 }], basename = path.basename(filepath, '.js'), context = createContext(); vm.runInContext(source, context); var lodash = context._; - var object = { 'fn': lodash.bind(function(x) { return this.x + x; }, { 'x': 1 }, 1) }; - equal(object.fn(), 2, 'bind: ' + basename); + lodash.each(array, function(value) { + last = value; + return false; + }); - ok(lodash.clone(array, true)[0] === array[0], 'clone: ' + basename); + equal(last.value, 2, '_.each: ' + basename); + + var object = { 'fn': lodash.bind(function(x) { return this.x + x; }, { 'x': 1 }, 1) }; + equal(object.fn(), 2, '_.bind: ' + basename); + + ok(lodash.clone(array, true)[0] === array[0], '_.clone: ' + basename); start(); }); }); @@ -570,7 +578,7 @@ vm.runInContext(source, context); var lodash = context._; - equal(lodash.partial(_.identity, 2)(), 2, 'partial: ' + basename); + equal(lodash.partial(_.identity, 2)(), 2, '_.partial: ' + basename); start(); }); });