Fix modularize legacy builds.

Former-commit-id: 17b09cce67bdf0ec2fd7373beb5b58240b08877b
This commit is contained in:
John-David Dalton
2013-08-03 21:33:29 -07:00
parent 6aa1361b1a
commit 76dcd7d96e

208
build.js
View File

@@ -265,13 +265,13 @@
/** Used to track Lo-Dash property dependencies of identifiers */ /** Used to track Lo-Dash property dependencies of identifiers */
var propDependencyMap = { var propDependencyMap = {
'at': ['support'], 'at': ['support'],
'baseClone': ['support'],
'baseIsEqual': ['support'],
'bind': ['support'], 'bind': ['support'],
'clone': ['support'],
'createBound': ['support'], 'createBound': ['support'],
'forEachRight': ['support'], 'forEachRight': ['support'],
'isArguments': ['support'], 'isArguments': ['support'],
'isEmpty': ['support'], 'isEmpty': ['support'],
'isEqual': ['support'],
'isPlainObject': ['support'], 'isPlainObject': ['support'],
'iteratorTemplate': ['support'], 'iteratorTemplate': ['support'],
'keys': ['support'], 'keys': ['support'],
@@ -1439,22 +1439,6 @@
return result ? result[0] : ''; return result ? result[0] : '';
} }
/**
* Gets the `templateSettings` assignment from `source`.
*
* @private
* @param {String} source The source to inspect.
* @returns {String} Returns the assignment.
*/
function getTemplateSettings(source) {
var result = source.match(RegExp(
multilineComment +
'( *)(?:var +|lodash\\.)templateSettings *=[\\s\\S]+?\\n\\1};\\n'
));
return result ? result[0] : '';
}
/** /**
* Creates a sorted array of all variables defined outside of Lo-Dash functions. * Creates a sorted array of all variables defined outside of Lo-Dash functions.
* *
@@ -3942,108 +3926,109 @@
.replace(/iteratorTemplate *&& */g, '') .replace(/iteratorTemplate *&& */g, '')
.replace(/iteratorTemplate\s*\?\s*([^:]+?)\s*:[^,;]+/g, '$1'); .replace(/iteratorTemplate\s*\?\s*([^:]+?)\s*:[^,;]+/g, '$1');
}); });
}
if (isModern || isUnderscore) {
iteratorOptions.forEach(function(prop) {
if (prop != 'array') {
source = removeFromGetObject(source, prop);
}
});
// inline all functions defined with `createIterator` if (isModern || isUnderscore) {
_.functions(lodash).forEach(function(funcName) { iteratorOptions.forEach(function(prop) {
if (!(isUnderscore && isLodash(funcName))) { if (prop != 'array') {
// strip leading underscores to match pseudo private functions source = removeFromGetObject(source, prop);
var reFunc = RegExp('^( *)(var ' + funcName.replace(/^_/, '') + ' *= *)createIterator\\(((?:{|[a-zA-Z])[\\s\\S]+?)\\);\\n', 'm');
if (reFunc.test(source)) {
// extract, format, and inject the compiled function's source code
source = source.replace(reFunc, function(match, indent, left) {
return (indent + left) +
cleanupCompiled(getFunctionSource(lodash[funcName], indent)) + ';\n';
});
}
}
});
if (isUnderscore) {
// unexpose "exit early" feature of `baseEach`, `_.forEach`, `_.forIn`, and `_.forOwn`
_.each(['baseEach', 'forEach', 'forIn', 'forOwn'], function(funcName) {
if (funcName == 'baseEach' || !isLodash(funcName)) {
source = source.replace(matchFunction(source, funcName), function(match) {
return match.replace(/=== *false\)/g, '=== indicatorObject)');
});
} }
}); });
// remove `thisArg` from unexposed `forIn` and `forOwn` // inline all functions defined with `createIterator`
_.each(['forIn', 'forOwn'], function(funcName) { _.functions(lodash).forEach(function(funcName) {
if (!isLodash(funcName)) { if (!(isUnderscore && isLodash(funcName))) {
source = source.replace(matchFunction(source, funcName), function(match) { // strip leading underscores to match pseudo private functions
return match var reFunc = RegExp('^( *)(var ' + funcName.replace(/^_/, '') + ' *= *)createIterator\\(((?:{|[a-zA-Z])[\\s\\S]+?)\\);\\n', 'm');
.replace(/(callback), *thisArg/g, '$1') if (reFunc.test(source)) {
.replace(/^ *callback *=.+\n/m, ''); // extract, format, and inject the compiled function's source code
}); source = source.replace(reFunc, function(match, indent, left) {
return (indent + left) +
cleanupCompiled(getFunctionSource(lodash[funcName], indent)) + ';\n';
});
}
} }
}); });
// replace complex lodash wrapper checks with simpler ones if (isUnderscore) {
if (!isModularize) { // unexpose "exit early" feature of `baseEach`, `_.forEach`, `_.forIn`, and `_.forOwn`
source = source.replace(matchFunction(source, 'baseIsEqual'), function(match) { _.each(['baseEach', 'forEach', 'forIn', 'forOwn'], function(funcName) {
return match.replace(/hasOwnProperty\.call\((\w+), *'__wrapped__'\)/g, '$1 instanceof lodash') if (funcName == 'baseEach' || !isLodash(funcName)) {
}); source = source.replace(matchFunction(source, funcName), function(match) {
} return match.replace(/=== *false\)/g, '=== indicatorObject)');
// modify `_.contains`, `_.every`, `_.find`, `_.some`, and `_.transform` to use the private `indicatorObject` });
if (!isLodash('forOwn')) { }
source = source.replace(matchFunction(source, 'every'), function(match) {
return match.replace(/\(result *= *(.+?)\);/g, '!(result = $1) && indicatorObject;');
}); });
source = source.replace(matchFunction(source, 'find'), function(match) { // remove `thisArg` from unexposed `forIn` and `forOwn`
return match.replace(/return false/, 'return indicatorObject'); _.each(['forIn', 'forOwn'], function(funcName) {
if (!isLodash(funcName)) {
source = source.replace(matchFunction(source, funcName), function(match) {
return match
.replace(/(callback), *thisArg/g, '$1')
.replace(/^ *callback *=.+\n/m, '');
});
}
}); });
source = source.replace(matchFunction(source, 'transform'), function(match) { // replace complex lodash wrapper checks with simpler ones
return match.replace(/return callback[^)]+\)/, '$& && indicatorObject'); if (!isModularize) {
}); source = source.replace(matchFunction(source, 'baseIsEqual'), function(match) {
return match.replace(/hasOwnProperty\.call\((\w+), *'__wrapped__'\)/g, '$1 instanceof lodash')
_.each(['contains', 'some'], function(funcName) {
source = source.replace(matchFunction(source, funcName), function(match) {
return match.replace(/!\(result *= *(.+?)\);/, '(result = $1) && indicatorObject;');
}); });
}); }
} // modify `_.contains`, `_.every`, `_.find`, `_.some`, and `_.transform` to use the private `indicatorObject`
// modify `baseEqual` and `shimIsPlainObject` to use the private `indicatorObject` if (!isLodash('forOwn')) {
if (!isLodash('forIn')) { source = source.replace(matchFunction(source, 'every'), function(match) {
source = source.replace(matchFunction(source, 'baseIsEqual'), function(match) { return match.replace(/\(result *= *(.+?)\);/g, '!(result = $1) && indicatorObject;');
return match.replace(/\(result *= *(.+?)\);/g, '!(result = $1) && indicatorObject;');
});
source = source.replace(matchFunction(source, 'shimIsPlainObject'), function(match) {
return match.replace(/return false/, 'return indicatorObject');
});
}
// replace `lodash.createCallback` references with `createCallback`
if (!isLodash('createCallback')) {
source = source.replace(/\blodash\.(createCallback\()\b/g, '$1');
}
// remove chainability from `baseEach` and `_.forEach`
if (!isLodash('forEach')) {
_.each(['baseEach', 'forEach'], function(funcName) {
source = source.replace(matchFunction(source, funcName), function(match) {
return match
.replace(/\n *return .+?([};\s]+)$/, '$1')
.replace(/\b(return) +result\b/, '$1')
}); });
source = source.replace(matchFunction(source, 'find'), function(match) {
return match.replace(/return false/, 'return indicatorObject');
});
source = source.replace(matchFunction(source, 'transform'), function(match) {
return match.replace(/return callback[^)]+\)/, '$& && indicatorObject');
});
_.each(['contains', 'some'], function(funcName) {
source = source.replace(matchFunction(source, funcName), function(match) {
return match.replace(/!\(result *= *(.+?)\);/, '(result = $1) && indicatorObject;');
});
});
}
// modify `baseEqual` and `shimIsPlainObject` to use the private `indicatorObject`
if (!isLodash('forIn')) {
source = source.replace(matchFunction(source, 'baseIsEqual'), function(match) {
return match.replace(/\(result *= *(.+?)\);/g, '!(result = $1) && indicatorObject;');
});
source = source.replace(matchFunction(source, 'shimIsPlainObject'), function(match) {
return match.replace(/return false/, 'return indicatorObject');
});
}
// replace `lodash.createCallback` references with `createCallback`
if (!isLodash('createCallback')) {
source = source.replace(/\blodash\.(createCallback\()\b/g, '$1');
}
// remove chainability from `baseEach` and `_.forEach`
if (!isLodash('forEach')) {
_.each(['baseEach', 'forEach'], function(funcName) {
source = source.replace(matchFunction(source, funcName), function(match) {
return match
.replace(/\n *return .+?([};\s]+)$/, '$1')
.replace(/\b(return) +result\b/, '$1')
});
});
}
// remove `_.assign`, `_.forEachRight`, `_.forIn`, `_.forOwn`, `_.isPlainObject`, `_.unzip`, and `_.zipObject` assignments
source = source.replace(getMethodAssignments(source), function(match) {
return _.reduce(['assign', 'createCallback', 'forEachRight', 'forIn', 'forOwn', 'isPlainObject', 'unzip', 'zipObject'], function(result, funcName) {
return isLodash(funcName)
? result
: result.replace(RegExp('^(?: *//.*\\s*)* *lodash\\.' + funcName + ' *=[\\s\\S]+?;\\n', 'm'), '');
}, match);
}); });
} }
// remove `_.assign`, `_.forEachRight`, `_.forIn`, `_.forOwn`, `_.isPlainObject`, `_.unzip`, and `_.zipObject` assignments
source = source.replace(getMethodAssignments(source), function(match) {
return _.reduce(['assign', 'createCallback', 'forEachRight', 'forIn', 'forOwn', 'isPlainObject', 'unzip', 'zipObject'], function(result, funcName) {
return isLodash(funcName)
? result
: result.replace(RegExp('^(?: *//.*\\s*)* *lodash\\.' + funcName + ' *=[\\s\\S]+?;\\n', 'm'), '');
}, match);
});
} }
if (isModularize) { if (isModularize) {
source = removeGetIndexOf(source); source = removeGetIndexOf(source);
@@ -4062,15 +4047,18 @@
return match.replace(/(?: *\/\/.*\n)*( *)if *\(!source[\s\S]+?\n\1}/, ''); return match.replace(/(?: *\/\/.*\n)*( *)if *\(!source[\s\S]+?\n\1}/, '');
}); });
// remove debug sourceURL use in `_.template`
source = source.replace(matchFunction(source, 'template'), function(match) {
return match.replace(/(?:\s*\/\/.*\n)* *var sourceURL[^;]+;|\+ *sourceURL/g, '');
});
// replace `lodash` use in `_.templateSettings.imports` // replace `lodash` use in `_.templateSettings.imports`
source = source.replace(matchProp(source, 'templateSettings'), function(match) { source = source.replace(matchProp(source, 'templateSettings'), function(match) {
return match.replace(/(:\s*)lodash\b/, "$1{ 'escape': escape }"); return match.replace(/(:\s*)lodash\b/, "$1{ 'escape': escape }");
}); });
source = source.replace(matchFunction(source, 'template'), function(match) {
return match
// assign `settings` variable from the reassigned `templateSettings.imports`
.replace(/= *templateSettings\b/, '$&.imports._.templateSettings')
// remove debug sourceURL use in `_.template`
.replace(/(?:\s*\/\/.*\n)* *var sourceURL[^;]+;|\+ *sourceURL/g, '');
});
} }
} }