mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 11:57:49 +00:00
Add Backbone build and correct dependencies for _.bindAll, _.mixin, and _.sortBy.
Former-commit-id: 3d4413d6ab3b41471be6336d138a4b4bfa97fde7
This commit is contained in:
18
README.md
18
README.md
@@ -46,8 +46,11 @@ Lo-Dash has been tested in at least Chrome 5-19, Firefox 1.5-13, IE 6-9, Opera 9
|
|||||||
Custom builds make it easy to create lightweight versions of Lo-Dash containing only the methods you need.
|
Custom builds make it easy to create lightweight versions of Lo-Dash containing only the methods you need.
|
||||||
We handle all the method dependency and alias mapping for you.
|
We handle all the method dependency and alias mapping for you.
|
||||||
|
|
||||||
Mobile builds, with IE bug fixes and method compilation removed, may be created by using the `mobile` argument.
|
* Backbone builds, containing all methods required by Backbone, may be created by using the `backbone` modifier argument.
|
||||||
|
~~~ bash
|
||||||
|
node build backbone
|
||||||
|
~~~
|
||||||
|
* Mobile builds, with IE bug fixes and method compilation removed, may be created by using the `mobile` modifier argument.
|
||||||
~~~ bash
|
~~~ bash
|
||||||
node build mobile
|
node build mobile
|
||||||
~~~
|
~~~
|
||||||
@@ -58,22 +61,25 @@ Custom builds may be created in three ways:
|
|||||||
Valid categories are *"arrays"*, *"chaining"*, *"collections"*, *"functions"*, *"objects"*, and *"utilities"*.
|
Valid categories are *"arrays"*, *"chaining"*, *"collections"*, *"functions"*, *"objects"*, and *"utilities"*.
|
||||||
~~~ bash
|
~~~ bash
|
||||||
node build category=collections,functions
|
node build category=collections,functions
|
||||||
node build category="collections, functions"
|
|
||||||
node build mobile category=collections,functions
|
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
2. Use the `include` argument to pass the names of the methods to include in the build.
|
2. Use the `include` argument to pass the names of the methods to include in the build.
|
||||||
~~~ bash
|
~~~ bash
|
||||||
node build include=each,filter,map
|
node build include=each,filter,map
|
||||||
node build include="each, filter, map"
|
node build include="each, filter, map"
|
||||||
node build mobile include=each,filter,map
|
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
3. Use the `exclude` argument to pass the names of the methods to exclude from the build.
|
3. Use the `exclude` argument to pass the names of the methods to exclude from the build.
|
||||||
~~~ bash
|
~~~ bash
|
||||||
node build exclude=union,uniq,zip
|
node build exclude=union,uniq,zip
|
||||||
node build exclude="union, uniq, zip"
|
node build exclude="union, uniq, zip"
|
||||||
node build mobile exclude=union,uniq,zip
|
~~~
|
||||||
|
|
||||||
|
All arguments, except `include` and `exlcude`, may be combined.
|
||||||
|
|
||||||
|
~~~ bash
|
||||||
|
node build backbone mobile category=functions include=pick,uniq
|
||||||
|
node build backbone mobile category=utilities exclude=first,last
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
Custom builds are saved to `lodash.custom.js` and `lodash.custom.min.js`.
|
Custom builds are saved to `lodash.custom.js` and `lodash.custom.min.js`.
|
||||||
|
|||||||
199
build.js
199
build.js
@@ -10,9 +10,6 @@
|
|||||||
var lodash = require(path.join(__dirname, 'lodash')),
|
var lodash = require(path.join(__dirname, 'lodash')),
|
||||||
minify = require(path.join(__dirname, 'build', 'minify'));
|
minify = require(path.join(__dirname, 'build', 'minify'));
|
||||||
|
|
||||||
/** Flag used to specify a mobile build */
|
|
||||||
var isMobile = process.argv.indexOf('mobile') > -1;
|
|
||||||
|
|
||||||
/** Shortcut used to convert array-like objects to arrays */
|
/** Shortcut used to convert array-like objects to arrays */
|
||||||
var slice = [].slice;
|
var slice = [].slice;
|
||||||
|
|
||||||
@@ -55,11 +52,56 @@
|
|||||||
'uniq': ['unique']
|
'uniq': ['unique']
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Used to track Backbone's Lo-Dash dependencies */
|
||||||
|
var backboneDependencies = [
|
||||||
|
'bind',
|
||||||
|
'bindAll',
|
||||||
|
'clone',
|
||||||
|
'contains',
|
||||||
|
'escape',
|
||||||
|
'every',
|
||||||
|
'extend',
|
||||||
|
'filter',
|
||||||
|
'find',
|
||||||
|
'first',
|
||||||
|
'forEach',
|
||||||
|
'groupBy',
|
||||||
|
'has',
|
||||||
|
'indexOf',
|
||||||
|
'initial',
|
||||||
|
'invoke',
|
||||||
|
'isArray',
|
||||||
|
'isEmpty',
|
||||||
|
'isEqual',
|
||||||
|
'isFunction',
|
||||||
|
'isObject',
|
||||||
|
'isRegExp',
|
||||||
|
'keys',
|
||||||
|
'last',
|
||||||
|
'lastIndexOf',
|
||||||
|
'map',
|
||||||
|
'max',
|
||||||
|
'min',
|
||||||
|
'mixin',
|
||||||
|
'reduce',
|
||||||
|
'reduceRight',
|
||||||
|
'reject',
|
||||||
|
'rest',
|
||||||
|
'shuffle',
|
||||||
|
'size',
|
||||||
|
'some',
|
||||||
|
'sortBy',
|
||||||
|
'sortedIndex',
|
||||||
|
'toArray',
|
||||||
|
'uniqueId',
|
||||||
|
'without'
|
||||||
|
];
|
||||||
|
|
||||||
/** Used to track function dependencies */
|
/** Used to track function dependencies */
|
||||||
var dependencyMap = {
|
var dependencyMap = {
|
||||||
'after': [],
|
'after': [],
|
||||||
'bind': [],
|
'bind': [],
|
||||||
'bindAll': ['bind'],
|
'bindAll': ['bind', 'functions'],
|
||||||
'chain': ['mixin'],
|
'chain': ['mixin'],
|
||||||
'clone': ['extend', 'isArray'],
|
'clone': ['extend', 'isArray'],
|
||||||
'compact': [],
|
'compact': [],
|
||||||
@@ -112,7 +154,7 @@
|
|||||||
'max': [],
|
'max': [],
|
||||||
'memoize': [],
|
'memoize': [],
|
||||||
'min': [],
|
'min': [],
|
||||||
'mixin': ['forEach'],
|
'mixin': ['forEach', 'functions'],
|
||||||
'noConflict': [],
|
'noConflict': [],
|
||||||
'once': [],
|
'once': [],
|
||||||
'partial': [],
|
'partial': [],
|
||||||
@@ -127,7 +169,7 @@
|
|||||||
'shuffle': [],
|
'shuffle': [],
|
||||||
'size': ['keys'],
|
'size': ['keys'],
|
||||||
'some': ['createIterator', 'identity'],
|
'some': ['createIterator', 'identity'],
|
||||||
'sortBy': ['map', 'pluck'],
|
'sortBy': [],
|
||||||
'sortedIndex': ['identity'],
|
'sortedIndex': ['identity'],
|
||||||
'tap': [],
|
'tap': [],
|
||||||
'template': ['escape'],
|
'template': ['escape'],
|
||||||
@@ -143,40 +185,41 @@
|
|||||||
'zip': ['max', 'pluck']
|
'zip': ['max', 'pluck']
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Names of all methods */
|
/** Collections of method names */
|
||||||
var allMethods = Object.keys(dependencyMap);
|
var excludeMethods,
|
||||||
|
includeMethods,
|
||||||
|
allMethods = Object.keys(dependencyMap);
|
||||||
|
|
||||||
/** Names of methods to filter for the build */
|
/** Used to specify whether filtering is for exclusion or inclusion */
|
||||||
var filterMethods = allMethods;
|
|
||||||
|
|
||||||
/** Used to specify whether `filterMethods` is used for exclusion or inclusion */
|
|
||||||
var filterType = process.argv.reduce(function(result, value) {
|
var filterType = process.argv.reduce(function(result, value) {
|
||||||
if (result) {
|
if (result) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
var pair = value.match(/^(category|exclude|include)=(.*)$/);
|
var pair = value.match(/^(exclude|include)=(.*)$/);
|
||||||
if (!pair) {
|
if (!pair) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
// remove nonexistent method names
|
||||||
|
var methodNames = lodash.intersection(allMethods, pair[2].split(/, */).map(getRealName));
|
||||||
|
|
||||||
result = pair[1];
|
if (pair[1] == 'exclude') {
|
||||||
filterMethods = pair[2].split(/, */).map(getRealName);
|
excludeMethods = methodNames;
|
||||||
|
} else {
|
||||||
if (result == 'category') {
|
includeMethods = methodNames;
|
||||||
// resolve method names belonging to each category
|
|
||||||
filterMethods = filterMethods.reduce(function(result, category) {
|
|
||||||
return result.concat(allMethods.filter(function(funcName) {
|
|
||||||
return RegExp('@category ' + category + '\\b', 'i').test(matchFunction(source, funcName));
|
|
||||||
}));
|
|
||||||
}, []);
|
|
||||||
}
|
}
|
||||||
else {
|
// return `filterType`
|
||||||
// remove nonexistent method names
|
return pair[1];
|
||||||
filterMethods = lodash.intersection(allMethods, filterMethods);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}, '');
|
}, '');
|
||||||
|
|
||||||
|
/** Flag used to specify a backbone build */
|
||||||
|
var isBackbone = process.argv.indexOf('backbone') > -1;
|
||||||
|
|
||||||
|
/** Flag used to specify a mobile build */
|
||||||
|
var isMobile = process.argv.indexOf('mobile') > -1;
|
||||||
|
|
||||||
|
/** Flag used to specify a custom build */
|
||||||
|
var isCustom = filterType || isBackbone || isMobile;
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -201,7 +244,7 @@
|
|||||||
// iterate over `dependencyMap`, adding the names of functions that
|
// iterate over `dependencyMap`, adding the names of functions that
|
||||||
// have `funcName` as a dependency
|
// have `funcName` as a dependency
|
||||||
return lodash.reduce(dependencyMap, function(result, dependencies, otherName) {
|
return lodash.reduce(dependencyMap, function(result, dependencies, otherName) {
|
||||||
if (dependencies.indexOf(funcName) > -1) {
|
if (lodash.contains(dependencies, funcName)) {
|
||||||
result.push(otherName);
|
result.push(otherName);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -209,25 +252,26 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets an array of dependencies for a function of the given `funcName`.
|
* Gets an array of dependencies for a given function name. If passed an array
|
||||||
|
* of dependencies it will return an array containing the given dependencies
|
||||||
|
* plus any additional detected sub-dependencies.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {String} funcName The name of the function to query.
|
* @param {Array|String} funcName A single function name or array of
|
||||||
|
* dependencies to query.
|
||||||
* @returns {Array} Returns an array of function dependencies.
|
* @returns {Array} Returns an array of function dependencies.
|
||||||
*/
|
*/
|
||||||
function getDependencies(funcName) {
|
function getDependencies(funcName) {
|
||||||
var dependencies = dependencyMap[funcName],
|
var dependencies = Array.isArray(funcName) ? funcName : dependencyMap[funcName];
|
||||||
result = [];
|
|
||||||
|
|
||||||
if (!dependencies) {
|
if (!dependencies) {
|
||||||
return result;
|
return [];
|
||||||
}
|
}
|
||||||
// recursively accumulate the dependencies of the `funcName` function, and
|
// recursively accumulate the dependencies of the `funcName` function, and
|
||||||
// the dependencies of its dependencies, and so on.
|
// the dependencies of its dependencies, and so on.
|
||||||
return dependencies.reduce(function(result, otherName) {
|
return lodash.uniq(dependencies.reduce(function(result, otherName) {
|
||||||
result.push.apply(result, getDependencies(otherName).concat(otherName));
|
result.push.apply(result, getDependencies(otherName).concat(otherName));
|
||||||
return result;
|
return result;
|
||||||
}, result);
|
}, []));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -292,7 +336,7 @@
|
|||||||
* @returns {String} Returns the modified source.
|
* @returns {String} Returns the modified source.
|
||||||
*/
|
*/
|
||||||
function removeFromCreateIterator(source, refName) {
|
function removeFromCreateIterator(source, refName) {
|
||||||
var snippet = matchFunction(source, 'createIterator'),
|
var snippet = matchFunction(source, 'createIterator').match(/Function\([\s\S]+$/)[0],
|
||||||
modified = snippet.replace(RegExp('\\b' + refName + '\\b,? *', 'g'), '');
|
modified = snippet.replace(RegExp('\\b' + refName + '\\b,? *', 'g'), '');
|
||||||
|
|
||||||
return source.replace(snippet, modified);
|
return source.replace(snippet, modified);
|
||||||
@@ -389,30 +433,81 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
// Backbone build
|
||||||
|
if (isBackbone) {
|
||||||
|
// add any additional dependencies
|
||||||
|
backboneDependencies = getDependencies(backboneDependencies);
|
||||||
|
|
||||||
|
if (filterType == 'exclude') {
|
||||||
|
// remove excluded methods from `backboneDependencies`
|
||||||
|
includeMethods = lodash.without.apply(lodash, [backboneDependencies].concat(excludeMethods));
|
||||||
|
}
|
||||||
|
else if (filterType) {
|
||||||
|
// merge backbone dependencies into `includeMethods`
|
||||||
|
includeMethods = lodash.union(includeMethods, backboneDependencies);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// include only the Backbone dependencies
|
||||||
|
includeMethods = backboneDependencies;
|
||||||
|
}
|
||||||
|
filterType = 'include';
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
// add category methods
|
||||||
|
process.argv.some(function(value) {
|
||||||
|
var categories = value.match(/^category=(.*)$/);
|
||||||
|
if (!categories) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// resolve method names belonging to each category
|
||||||
|
var categoryMethods = categories.reduce(function(result, category) {
|
||||||
|
return result.concat(allMethods.filter(function(funcName) {
|
||||||
|
return RegExp('@category ' + category + '\\b', 'i').test(matchFunction(source, funcName));
|
||||||
|
}));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (filterType == 'exclude') {
|
||||||
|
// remove excluded methods from `categoryMethods`
|
||||||
|
includeMethods = lodash.without.apply(lodash, [categoryMethods].concat(excludeMethods));
|
||||||
|
}
|
||||||
|
else if (filterType) {
|
||||||
|
// merge backbone dependencies into `includeMethods`
|
||||||
|
includeMethods = lodash.union(includeMethods, categoryMethods);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// include only the Backbone dependencies
|
||||||
|
includeMethods = categoryMethods;
|
||||||
|
}
|
||||||
|
|
||||||
|
filterType = 'include';
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
// custom build
|
// custom build
|
||||||
(function() {
|
(function() {
|
||||||
// exit early if "category", "exclude", or "include" options aren't specified
|
// exit early if "exclude" or "include" options aren't specified
|
||||||
if (!filterType) {
|
if (!filterType) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (filterType == 'exclude') {
|
if (filterType == 'exclude') {
|
||||||
// remove methods that are named in `filterMethods` and their dependants
|
// remove methods that are named in `excludeMethods` and their dependants
|
||||||
filterMethods.forEach(function(funcName) {
|
excludeMethods.forEach(function(funcName) {
|
||||||
getDependants(funcName).concat(funcName).forEach(function(otherName) {
|
getDependants(funcName).concat(funcName).forEach(function(otherName) {
|
||||||
source = removeFunction(source, otherName);
|
source = removeFunction(source, otherName);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// add dependencies to `filterMethods`
|
// add dependencies to `includeMethods`
|
||||||
filterMethods = lodash.uniq(filterMethods.reduce(function(result, funcName) {
|
includeMethods = getDependencies(includeMethods);
|
||||||
result.push.apply(result, getDependencies(funcName).concat(funcName));
|
|
||||||
return result;
|
|
||||||
}, []));
|
|
||||||
|
|
||||||
// remove methods that aren't named in `filterMethods`
|
// remove methods that aren't named in `includeMethods`
|
||||||
lodash.each(dependencyMap, function(dependencies, otherName) {
|
lodash.each(allMethods, function(otherName) {
|
||||||
if (filterMethods.indexOf(otherName) < 0) {
|
if (!lodash.contains(includeMethods, otherName)) {
|
||||||
source = removeFunction(source, otherName);
|
source = removeFunction(source, otherName);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -517,13 +612,13 @@
|
|||||||
}()));
|
}()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
// remove pseudo private properties
|
// remove pseudo private properties
|
||||||
source = source.replace(/(?:(?:\s*\/\/.*)*\s*lodash\._[^=]+=.+\n)+/g, '\n');
|
source = source.replace(/(?:(?:\s*\/\/.*)*\s*lodash\._[^=]+=.+\n)+/g, '\n');
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
// begin the minification process
|
// begin the minification process
|
||||||
if (filterType || isMobile) {
|
if (isCustom) {
|
||||||
fs.writeFileSync(path.join(__dirname, 'lodash.custom.js'), source);
|
fs.writeFileSync(path.join(__dirname, 'lodash.custom.js'), source);
|
||||||
minify(source, 'lodash.custom.min', function(result) {
|
minify(source, 'lodash.custom.min', function(result) {
|
||||||
fs.writeFileSync(path.join(__dirname, 'lodash.custom.min.js'), result);
|
fs.writeFileSync(path.join(__dirname, 'lodash.custom.min.js'), result);
|
||||||
|
|||||||
Reference in New Issue
Block a user