Simplify getMethodAssignments, add removeMethodAssignments, and avoid some cleanup when the --no-dep flag is passed in build.js.

Former-commit-id: aebfc41fdde98df243c4620ee729977775cba52b
This commit is contained in:
John-David Dalton
2013-06-19 08:39:12 -07:00
parent 2bc343b524
commit b4adc127a4
3 changed files with 116 additions and 91 deletions

199
build.js
View File

@@ -1002,12 +1002,7 @@
* @returns {String} Returns the method assignments snippet.
*/
function getMethodAssignments(source) {
var result = source.match(RegExp(
'/\\*-+\\*/\\n' +
'(?:\\s*//.*)*\\s*lodash\\.\\w+ *=[\\s\\S]+?\\n' +
'(?=\\s*/\\*-+\\*/\\n\\s*' + multilineComment + ' *lodash\\.VERSION *=)'
));
var result = source.match(/\n\n(?:\s*\/\/.*)*\s*lodash\.\w+ *=[\s\S]+?\n *lodash\.VERSION *=.+\n/);
return result ? result[0] : '';
}
@@ -1433,6 +1428,17 @@
return source;
}
/**
* Removes the Lo-Dash method assignments snippet from `source`.
*
* @private
* @param {String} source The source to inspect.
* @returns {String} Returns the modified source.
*/
function removeMethodAssignments(source) {
return source.replace(getMethodAssignments(source), '');
}
/**
* Removes the specified pseudo private property from `source`. If a `propName`
* is not specified, all pseudo private properties are removed.
@@ -3473,41 +3479,12 @@
source = removeVar(source, 'cloneableClasses');
source = removeVar(source, 'ctorByClass');
}
if (isRemoved(source, 'clone', 'isEqual', 'isPlainObject')) {
source = removeSupportNodeClass(source);
}
if (isRemoved(source, 'createIterator')) {
source = removeSupportNonEnumShadows(source);
}
if (isRemoved(source, 'createIterator', 'bind', 'keys')) {
source = removeSupportProp(source, 'fastBind');
}
if (isRemoved(source, 'createIterator', 'keys')) {
source = removeKeysOptimization(source);
source = removeSupportNonEnumArgs(source);
}
if (isRemoved(source, 'invert')) {
source = replaceVar(source, 'htmlUnescapes', "{'&amp;':'&','&lt;':'<','&gt;':'>','&quot;':'\"','&#x27;':\"'\"}");
}
if (isRemoved(source, 'isArguments')) {
source = replaceSupportProp(source, 'argsClass', 'true');
source = removeIsArgumentsFallback(source);
}
if (isRemoved(source, 'isArguments', 'isEmpty')) {
source = removeSupportArgsClass(source);
}
if (isRemoved(source, 'isArray')) {
source = removeIsArrayFallback(source);
}
if (isRemoved(source, 'isPlainObject')) {
source = removeSupportOwnLast(source);
}
if (isRemoved(source, 'keys')) {
source = removeFunction(source, 'shimKeys');
}
if (isRemoved(source, 'mixin')) {
// if possible, inline the `_.mixin` call to ensure proper chaining behavior
source = source.replace(/^( *)mixin\(lodash\).+/m, function(match, indent) {
source = source.replace(/^( *)mixin\(lodash\).*/m, function(match, indent) {
if (isRemoved(source, 'forOwn')) {
return '';
}
@@ -3529,27 +3506,17 @@
].join('\n' + indent);
});
}
if (isRemoved(source, 'sortBy')) {
_.each([removeFromGetObject, removeFromReleaseObject], function(func) {
source = func(source, 'criteria');
source = func(source, 'index');
source = func(source, 'value');
});
if (!_.contains(buildMethods, 'shimKeys') && isRemoved(source, 'keys')) {
source = removeFunction(source, 'shimKeys');
}
if (isRemoved(source, 'template')) {
// remove `templateSettings` assignment
source = source.replace(/(?:\n +\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)?\n *lodash\.templateSettings[\s\S]+?};\n/, '');
}
if (isRemoved(source, 'throttle')) {
_.each(['leading', 'maxWait', 'trailing'], function(prop) {
source = removeFromGetObject(source, prop);
});
}
if (isRemoved(source, 'value')) {
source = removeFunction(source, 'chain');
source = removeFunction(source, 'wrapperToString');
source = removeFunction(source, 'wrapperValueOf');
source = removeSupportSpliceObjects(source);
source = removeLodashWrapper(source);
// simplify the `lodash` function
@@ -3574,52 +3541,104 @@
.replace(/(?:\s*\/\/.*)*\n( *)(?:basicEach|forEach)\(\['[\s\S]+?\n\1}.+/g, '')
.replace(/(?:\s*\/\/.*)*\n *lodash\.prototype.[\s\S]+?;/g, '');
}
if (isNoDep) {
_.each(buildMethods, function(methodName) {
_.each(getAliases(methodName), function(alias) {
source = removeFunction(source, alias);
});
// remove forks of removed methods
_.each(['createObject', 'defer', 'isArguments', 'isArray', 'isFunction'], function(methodName) {
if (_.size(source.match(RegExp(methodName + '\\(', 'g'))) < 2) {
source = eval('remove' + capitalize(methodName) + 'Fallback')(source);
_.each(propDependencyMap[methodName], function(varName) {
source = removeVar(source, varName);
});
});
source = source.replace(/^ *\/\*-+\*\/\n/gm, '');
source = removeSupport(source);
}
else {
if (isRemoved(source, 'bind')) {
source = removeSupportProp(source, 'fastBind');
}
});
if (isRemoved(source, 'clone', 'isEqual', 'isPlainObject')) {
source = removeSupportNodeClass(source);
}
if (isRemoved(source, 'createIterator')) {
source = removeSupportNonEnumShadows(source);
}
if (isRemoved(source, 'isArguments')) {
source = replaceSupportProp(source, 'argsClass', 'true');
}
if (isRemoved(source, 'isArguments', 'isEmpty')) {
source = removeSupportArgsClass(source);
}
if (isRemoved(source, 'isPlainObject')) {
source = removeSupportOwnLast(source);
}
if (isRemoved(source, 'keys')) {
source = removeKeysOptimization(source);
source = removeSupportNonEnumArgs(source);
}
if (isRemoved(source, 'sortBy')) {
_.each([removeFromGetObject, removeFromReleaseObject], function(func) {
source = func(source, 'criteria');
source = func(source, 'index');
source = func(source, 'value');
});
}
if (isRemoved(source, 'throttle')) {
_.each(['leading', 'maxWait', 'trailing'], function(prop) {
source = removeFromGetObject(source, prop);
});
}
if (isRemoved(source, 'value')) {
source = removeSupportSpliceObjects(source);
}
if (!/^ *support\.(?:enumErrorProps|nonEnumShadows) *=/m.test(source)) {
source = removeFromCreateIterator(source, 'errorClass');
source = removeFromCreateIterator(source, 'errorProto');
if (!/^ *support\.(?:enumErrorProps|nonEnumShadows) *=/m.test(source)) {
source = removeFromCreateIterator(source, 'errorClass');
source = removeFromCreateIterator(source, 'errorProto');
// remove 'Error' from the `contextProps` array
source = source.replace(/^ *var contextProps *=[\s\S]+?;/m, function(match) {
return match.replace(/'Error', */, '');
});
}
// remove code used to resolve unneeded `support` properties
source = source.replace(getSupport(source), function(match) {
return match.replace(/^ *\(function[\s\S]+?\n(( *)var ctor *=[\s\S]+?(?:\n *for.+)+\n)([\s\S]+?)}\(1\)\);\n/m, function(match, setup, indent, body) {
var modified = setup;
// remove 'Error' from the `contextProps` array
source = source.replace(/^ *var contextProps *=[\s\S]+?;/m, function(match) {
return match.replace(/'Error', */, '');
if (!/support\.spliceObjects *=(?! *(?:false|true))/.test(body)) {
modified = modified.replace(/^ *object *=.+\n/m, '');
}
if (!/support\.enumPrototypes *=(?! *(?:false|true))/.test(body) &&
!/support\.nonEnumShadows *=(?! *(?:false|true))/.test(body) &&
!/support\.ownLast *=(?! *(?:false|true))/.test(body)) {
modified = modified
.replace(/\bctor *=.+\s+/, '')
.replace(/^ *ctor\.prototype.+\s+.+\n/m, '')
.replace(/(?:,\n)? *props *=[^;=]+/, '')
.replace(/^ *for *\((?=prop)/, '$&var ')
}
if (!/support\.nonEnumArgs *=(?! *(?:false|true))/.test(body)) {
modified = modified.replace(/^ *for *\(.+? arguments.+\n/m, '');
}
// cleanup the empty var statement
modified = modified.replace(/^ *var;\n/m, '');
// if no setup then remove IIFE
return /^\s*$/.test(modified)
? body.replace(RegExp('^' + indent, 'gm'), indent.slice(0, -2))
: match.replace(setup, modified);
});
});
}
// remove code used to resolve unneeded `support` properties
source = source.replace(getSupport(source), function(match) {
return match.replace(/^ *\(function[\s\S]+?\n(( *)var ctor *=[\s\S]+?(?:\n *for.+)+\n)([\s\S]+?)}\(1\)\);\n/m, function(match, setup, indent, body) {
var modified = setup;
if (!/support\.spliceObjects *=(?! *(?:false|true))/.test(body)) {
modified = modified.replace(/^ *object *=.+\n/m, '');
}
if (!/support\.enumPrototypes *=(?! *(?:false|true))/.test(body) &&
!/support\.nonEnumShadows *=(?! *(?:false|true))/.test(body) &&
!/support\.ownLast *=(?! *(?:false|true))/.test(body)) {
modified = modified
.replace(/\bctor *=.+\s+/, '')
.replace(/^ *ctor\.prototype.+\s+.+\n/m, '')
.replace(/(?:,\n)? *props *=[^;=]+/, '')
.replace(/^ *for *\((?=prop)/, '$&var ')
}
if (!/support\.nonEnumArgs *=(?! *(?:false|true))/.test(body)) {
modified = modified.replace(/^ *for *\(.+? arguments.+\n/m, '');
}
// cleanup the empty var statement
modified = modified.replace(/^ *var;\n/m, '');
// if no setup then remove IIFE
return /^\s*$/.test(modified)
? body.replace(RegExp('^' + indent, 'gm'), indent.slice(0, -2))
: match.replace(setup, modified);
});
// remove forks of removed methods
_.each(['createObject', 'defer', 'isArguments', 'isArray', 'isFunction'], function(methodName) {
if (isRemoved(source, methodName)) {
source = eval('remove' + capitalize(methodName) + 'Fork')(source);
}
});
// remove unused variables
@@ -3653,6 +3672,12 @@
if (_.size(source.match(/\bfreeExports\b/g)) < 2) {
source = removeVar(source, 'freeExports');
}
if (!isAMD && !isCommonJS && !isGlobal && !isNode) {
source = removeFunction(source, 'lodash');
source = removeLodashWrapper(source);
source = removePseudoPrivate(source);
source = removeMethodAssignments(source);
}
debugSource = cleanupSource(source);
source = debugSource;

View File

@@ -4384,9 +4384,6 @@
lodash.take = first;
lodash.head = first;
// add functions to `lodash.prototype`
mixin(lodash);
/*--------------------------------------------------------------------------*/
/**
@@ -4398,6 +4395,9 @@
*/
lodash.VERSION = '1.3.1';
// add functions to `lodash.prototype`
mixin(lodash);
// add "Chaining" functions to the wrapper
lodash.prototype.chain = wrapperChain;
lodash.prototype.value = wrapperValueOf;

View File

@@ -32,5 +32,5 @@ return e},f.collect=F,f.drop=V,f.each=B,f.extend=y,f.methods=_,f.object=function
for(typeof r=="number"&&(e=(0>r?Rt(0,e+r):Dt(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},f.mixin=L,f.noConflict=function(){return n._=bt,this},f.random=function(n,t){n==X&&t==X&&(t=1),n=+n||0,t==X?(t=n,n=0):t=+t||0;var r=Mt();return n%1||t%1?n+Dt(r*(t-n+parseFloat("1e-"+((r+"").length-1))),t):n+xt(r*(t-n+1))},f.reduce=D,f.reduceRight=M,f.result=function(n,t){var r=n?n[t]:X;return w(r)?n[t]():r},f.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:Pt(n).length},f.some=$,f.sortedIndex=W,f.template=function(n,t,r){var e=f.templateSettings;
n||(n=""),r=m({},r,e);var u=0,o="__p+='",e=r.variable;n.replace(RegExp((r.escape||et).source+"|"+(r.interpolate||et).source+"|"+(r.evaluate||et).source+"|$","g"),function(t,r,e,a,f){return o+=n.slice(u,f).replace(ot,i),r&&(o+="'+_['escape']("+r+")+'"),a&&(o+="';"+a+";__p+='"),e&&(o+="'+((__t=("+e+"))==null?'':__t)+'"),u=f+t.length,t}),o+="';\n",e||(e="obj",o="with("+e+"||{}){"+o+"}"),o="function("+e+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+o+"return __p}";
try{var a=Function("_","return "+o)(f)}catch(c){throw c.source=o,c}return t?a(t):(a.source=o,a)},f.unescape=function(n){return n==X?"":(n+"").replace(rt,g)},f.uniqueId=function(n){var t=++Z+"";return n?n+t:t},f.all=T,f.any=$,f.detect=k,f.findWhere=function(n,t){return I(n,t,Q)},f.foldl=D,f.foldr=M,f.include=S,f.inject=D,f.first=C,f.last=function(n,t,r){if(n){var e=0,u=n.length;if(typeof t!="number"&&t!=X){var o=u;for(t=J(t,r);o--&&t(n[o],o,n);)e++}else if(e=t,e==X||r)return n[u-1];return $t.call(n,Rt(0,u-e))
}},f.take=C,f.head=C,L(f),f.VERSION="1.3.1",f.prototype.chain=function(){return this.__chain__=Q,this},f.prototype.value=function(){return this.__wrapped__},B("pop push reverse shift sort splice unshift".split(" "),function(n){var t=dt[n];f.prototype[n]=function(){var n=this.__wrapped__;return t.apply(n,arguments),!zt.spliceObjects&&0===n.length&&delete n[0],this}}),B(["concat","join","slice"],function(n){var t=dt[n];f.prototype[n]=function(){var n=t.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new c(n),n.__chain__=Q),n
}},f.take=C,f.head=C,f.VERSION="1.3.1",L(f),f.prototype.chain=function(){return this.__chain__=Q,this},f.prototype.value=function(){return this.__wrapped__},B("pop push reverse shift sort splice unshift".split(" "),function(n){var t=dt[n];f.prototype[n]=function(){var n=this.__wrapped__;return t.apply(n,arguments),!zt.spliceObjects&&0===n.length&&delete n[0],this}}),B(["concat","join","slice"],function(n){var t=dt[n];f.prototype[n]=function(){var n=t.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new c(n),n.__chain__=Q),n
}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=f, define(function(){return f})):yt&&!yt.nodeType?mt?(mt.exports=f)._=f:yt._=f:n._=f}(this);