Reduce underscore build size.

Former-commit-id: 207d4ab49063483245dc951d4646413d6d4a1903
This commit is contained in:
John-David Dalton
2012-09-29 12:04:40 -07:00
parent f31598f916
commit 82bc52b909
4 changed files with 38 additions and 17 deletions

View File

@@ -630,7 +630,7 @@
return source; return source;
} }
// remove function // remove function
source = source.replace(matchFunction(source, funcName), ''); source = source.replace(snippet, '');
// grab the method assignments snippet // grab the method assignments snippet
snippet = getMethodAssignments(source); snippet = getMethodAssignments(source);
@@ -1054,6 +1054,11 @@
// replace `arrayLikeClasses` in `_.isEqual` // replace `arrayLikeClasses` in `_.isEqual`
source = source.replace(/(?: *\/\/.*\n)*( +)var isArr *= *arrayLikeClasses[^}]+}/, '$1var isArr = isArray(a);'); 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 // remove `deep` clone functionality
source = source.replace(/( +)function clone[\s\S]+?\n\1}/, [ source = source.replace(/( +)function clone[\s\S]+?\n\1}/, [
' function clone(value) {', ' function clone(value) {',
@@ -1094,6 +1099,15 @@
source = buildTemplate(templatePattern, templateSettings); source = buildTemplate(templatePattern, templateSettings);
} }
else { 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 // remove methods from the build
allMethods.forEach(function(otherName) { allMethods.forEach(function(otherName) {
if (!_.contains(buildMethods, otherName)) { if (!_.contains(buildMethods, otherName)) {

View File

@@ -274,7 +274,7 @@
// remove whitespace from string literals // remove whitespace from string literals
source = source.replace(/'(?:(?=(\\?))\1.)*?'/g, function(string) { source = source.replace(/'(?:(?=(\\?))\1.)*?'/g, function(string) {
// avoids removing the '\n' of the `stringEscapes` object // 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; return match == false || match == '\\n' ? '' : match;
}); });
}); });

View File

@@ -667,11 +667,13 @@
function createCallback(func, thisArg) { function createCallback(func, thisArg) {
if (!func) { if (!func) {
return identity; return identity;
} else if (typeof func != 'function') { }
if (typeof func != 'function') {
return function(object) { return function(object) {
return object[func]; return object[func];
} };
} else if (thisArg !== undefined) { }
if (thisArg !== undefined) {
return function(value, index, object) { return function(value, index, object) {
return func.call(thisArg, value, index, object); return func.call(thisArg, value, index, object);
}; };
@@ -1693,13 +1695,10 @@
* // => ['one', 'two', 'three'] (order is not guaranteed) * // => ['one', 'two', 'three'] (order is not guaranteed)
*/ */
var keys = !nativeKeys ? shimKeys : function(object) { var keys = !nativeKeys ? shimKeys : function(object) {
var type = typeof object;
// avoid iterating over the `prototype` property // avoid iterating over the `prototype` property
if (type == 'function' && propertyIsEnumerable.call(object, 'prototype')) { return typeof object == 'function' && propertyIsEnumerable.call(object, 'prototype')
return shimKeys(object); ? shimKeys(object)
} : nativeKeys(object);
return nativeKeys(object);
}; };
/** /**

View File

@@ -436,7 +436,7 @@
console.log(e); console.log(e);
pass = false; pass = false;
} }
equal(pass, true, methodName + ': ' + message); equal(pass, true, '_.' + methodName + ': ' + message);
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -545,17 +545,25 @@
var start = _.after(2, _.once(QUnit.start)); var start = _.after(2, _.once(QUnit.start));
build(['-s', 'underscore'], function(source, filepath) { build(['-s', 'underscore'], function(source, filepath) {
var array = [{ 'a': 1 }], var last,
array = [{ 'value': 1 }, { 'value': 2 }],
basename = path.basename(filepath, '.js'), basename = path.basename(filepath, '.js'),
context = createContext(); context = createContext();
vm.runInContext(source, context); vm.runInContext(source, context);
var lodash = context._; var lodash = context._;
var object = { 'fn': lodash.bind(function(x) { return this.x + x; }, { 'x': 1 }, 1) }; lodash.each(array, function(value) {
equal(object.fn(), 2, 'bind: ' + basename); 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(); start();
}); });
}); });
@@ -570,7 +578,7 @@
vm.runInContext(source, context); vm.runInContext(source, context);
var lodash = context._; var lodash = context._;
equal(lodash.partial(_.identity, 2)(), 2, 'partial: ' + basename); equal(lodash.partial(_.identity, 2)(), 2, '_.partial: ' + basename);
start(); start();
}); });
}); });