8000 Adjust how "mobile" build is created and add first pass at "legacy" b… · lodash/lodash@d493185 · GitHub
[go: up one dir, main page]

Skip to content

Commit d493185

Browse files
committed
Adjust how "mobile" build is created and add first pass at "legacy" build.
Former-commit-id: 740cc40c21d33353f34796ae6da3bc8ce015ab6c
1 parent 83e3f83 commit d493185

File tree

1 file changed

+97
-29
lines changed

1 file changed

+97
-29
lines changed

build.js

Lines changed: 97 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,43 @@
22
;(function() {
33
'use strict';
44

5-
/** The Node filesystem and path modules */
5+
/** Load modules */
66
var fs = require('fs'),
7-
path = require('path');
8-
9-
/** Load other modules */
10-
var lodash = require(path.join(__dirname, 'lodash')),
7+
path = require('path'),
8+
vm = require('vm'),
119
minify = require(path.join(__dirname, 'build', 'minify'));
1210

11+
/** Flag used to specify a backbone build */
12+
var isBackbone = process.argv.indexOf('backbone') > -1;
13+
14+
/** Flag used to specify a legacy build */
15+
var isLegacy = process.argv.indexOf('legacy') > -1;
16+
17+
/** Flag used to specify a mobile build */
18+
var isMobile = !isLegacy && process.argv.indexOf('mobile') > -1;
19+
1320
/** Shortcut used to convert array-like objects to arrays */
1421
var slice = [].slice;
1522

1623
/** The lodash.js source */
1724
var source = fs.readFileSync(path.join(__dirname, 'lodash.js'), 'utf8');
1825

26+
/** Load customized Lo-Dash module */
27+
var lodash = (function() {
28+
var sandbox = {};
29+
30+
if (isLegacy) {
31+
['isBindFast', 'isKeysFast', 'nativeBind', 'nativeIsArray', 'nativeKeys'].forEach(function(varName) {
32+
source = replaceVar(source, varName, 'false');
33+
});
34+
}
35+
else if (isMobile) {
36+
source = replaceVar(source, 'isKeysFast', 'false');
37+
}
38+
vm.runInNewContext(source, sandbox);
39+
return sandbox._;
40+
}());
41+
1942
/** Used to associate aliases with their real names */
2043
var aliasToRealMap = {
2144
'all': 'every',
@@ -198,6 +221,7 @@
198221
'hasDontEnumBug',
199222
'inLoop',
200223
'init',
224+
'isKeysFast',
201225
'iteratedObject',
202226
'loopExp',
203227
'object',
@@ -233,12 +257,6 @@
233257
return pair[1];
234258
}, '');
235259

236-
/** Flag used to specify a backbone build */
237-
var isBackbone = process.argv.indexOf('backbone') > -1;
238-
239-
/** Flag used to specify a mobile build */
240-
var isMobile = process.argv.indexOf('mobile') > -1;
241-
242260
/*--------------------------------------------------------------------------*/
243261

244262
/**
@@ -318,7 +336,7 @@
318336
* @returns {String} Returns the `isArguments` fallback snippet.
319337
*/
320338
function getIsArgumentsFallback(source) {
321-
return (source.match(/(?: *\/\/.*)*\s*if *\(!(?:lodash\.)?isArguments[^)]+\)[\s\S]+?};?\s*}\n/) || [''])[0];
339+
return (source.match(/(?: *\/\/.*)*\s*if *\(!(?:lodash\.)?isArguments[^)]+\)[\s\S]+?};\s*}\n/) || [''])[0];
322340
}
323341

324342
/**
@@ -465,6 +483,31 @@
465483
return removeFromCreateIterator(source, varName);
466484
}
467485

486+
/**
487+
* Searches `source` for a `varName` variable declaration and replaces its
488+
* assigned value with `varValue`.
489+
*
490+
* @private
491+
* @param {String} source The source to inspect.
492+
* @param {String} varName The name of the variable to replace.
493+
* @returns {String} Returns the source with the variable replaced.
494+
*/
495+
function replaceVar(source, varName, varValue) {
496+
// replace a variable that's not part of a declaration list
497+
source = source.replace(RegExp(
498+
'(( +)var ' + varName + ' *= *)' +
499+
'(?:.*?;|(?:Function\\(.+?|.*?[^,])\\n[\\s\\S]+?\\n\\2.+?;)\\n'
500+
), '$1' + varValue + ';\n');
501+
502+
// replace a varaible at the start of middle of a declaration list
503+
source = source.replace(RegExp('((?:var|\\n) +' + varName + ' *=).+?,'), '$1 ' + varValue + ',');
504+
505+
// replace a variable at the end of a variable declaration list
506+
source = source.replace(RegExp('(,\\s*' + varName + ' *=).*?;'), '$1 ' + varValue + ';');
507+
508+
return source;
509+
}
510+
468511
/*--------------------------------------------------------------------------*/
469512

470513
// Backbone build
@@ -574,13 +617,12 @@
574617
}
575618
if (isRemoved(source, 'bind')) {
576619
source = removeVar(source, 'nativeBind');
577-
source = removeVar(source, 'useNativeBind');
620+
source = removeVar(source, 'isBindFast');
578621
}
579622
if (isRemoved(source, 'isArray')) {
580623
source = removeVar(source, 'nativeIsArray');
581624
}
582625
if (isRemoved(source, 'keys')) {
583-
source = removeVar(source, 'nativeKeys');
584626
source = removeFunction(source, 'shimKeys');
585627
}
586628
if (isRemoved(source, 'clone', 'isObject', 'keys')) {
@@ -637,6 +679,10 @@
637679
'RegExp': 'regexpClass',
638680
'String': 'stringClass'
639681
}, function(value, key) {
682+
// if legacy build skip `isArguments`
683+
if (isLegacy && key == 'Arguments') {
684+
return;
685+
}
640686
var funcName = 'is' + key,
641687
funcCode = matchFunction(source, funcName);
642688

@@ -673,7 +719,7 @@
673719
);
674720

675721
// tweak `isArguments` fallback
676-
snippet = getIsArgumentsFallback(source);
722+
snippet = !isLegacy && getIsArgumentsFallback(source);
677723
if (snippet) {
678724
result = '\n' + snippet.replace(/isArguments/g, 'lodash.$&');
679725
source = source.replace(snippet, result);
@@ -682,6 +728,29 @@
682728

683729
/*--------------------------------------------------------------------------*/
684730

731+
if (isLegacy) {
732+
// replace `_.keys` with `shimKeys`
733+
if (!isRemoved(source, 'keys')) {
734+
source = source.replace(
735+
matchFunction(source, 'keys').replace(/[\s\S]+?var keys *=/, ''),
736+
matchFunction(source, 'shimKeys').replace(/[\s\S]+?var shimKeys *=/, '')
737+
);
738+
739+
source = removeFunction(source, 'shimKeys');
740+
}
741+
// replace `_.isArguments` with fallback
742+
if (!isRemoved(source, 'isArguments')) {
743+
source = source.replace(
744+
matchFunction(source, 'isArguments').replace(/[\s\S]+?var isArguments *=/, ''),
745+
getIsArgumentsFallback(source).match(/isArguments *=([\s\S]+?) *};/)[1] + ' };\n'
746+
);
747+
748+
source = removeIsArgumentsFallback(source);
749+
}
750+
751+
source = removeFromCreateIterator(source, 'nativeKeys');
752+
}
753+
685754
if (isMobile) {
686755
// inline functions defined with `createIterator`
687756
lodash.functions(lodash).forEach(function(funcName) {
@@ -696,31 +765,27 @@
696765
source = source.replace(reFunc, '$1' + getFunctionSource(lodash[funcName]) + ';\n');
697766
});
698767

699-
source = removeIsArgumentsFallback(source);
700-
701-
source = removeVar(source, 'iteratorTemplate');
702-
703768
// remove JScript [[DontEnum]] fix from `isEqual`
704769
source = source.replace(/(?:\s*\/\/.*\n)*( +)if *\(result *&& *hasDontEnumBug[\s\S]+?\n\1}/, '');
705770

706771
// remove IE `shift` and `splice` fix
707772
source = source.replace(/(?:\s*\/\/.*\n)*( +)if *\(value.length *=== *0[\s\S]+?\n\1}/, '');
708773

709-
// cleanup code
710-
source = source.replace(/^ *;\n/gm, '');
774+
source = removeVar(source, 'iteratorTemplate');
775+
source = removeIsArgumentsFallback(source);
711776
}
712777
else {
713778
// inline `iteratorTemplate` template
714779
source = source.replace(/(( +)var iteratorTemplate *= *)[\s\S]+?\n\2.+?;\n/, (function() {
715-
var code = getFunctionSource(lodash._iteratorTemplate);
780+
var snippet = getFunctionSource(lodash._iteratorTemplate);
716781

717782
// expand properties to avoid having to use a with-statement
718783
iteratorOptions.forEach(function(property) {
719-
code = code.replace(RegExp('([^\\w.])\\b' + property + '\\b', 'g'), '$1obj.' + property);
784+
snippet = snippet.replace(RegExp('([^\\w.])\\b' + property + '\\b', 'g'), '$1obj.' + property);
720785
});
721786

722787
// remove unnecessary code
723-
code = code
788+
snippet = snippet
724789
.replace(/, *__t,[^;]+|function print[^}]+}/g, '')
725790
.replace(/'(?:\\n|\s)+'/g, "''")
726791
.replace(/__p *\+= *' *';/g, '')
@@ -729,17 +794,17 @@
729794
.replace(/\(\(__w?t *= *\( *([^)]+) *\)\) *== *null *\? *'' *: *__w?t\)/g, '$1');
730795

731796
// remove the with-statement
732-
code = code.replace(/ *with *\([^)]+\) *{/, '\n').replace(/}}\s*;?\s*}/, '}}\n');
797+
snippet = snippet.replace(/ *with *\([^)]+\) *{/, '\n').replace(/}}\s*;?\s*}/, '}}\n');
733798

734799
// minor cleanup
735-
code = code
800+
snippet = snippet
736801
.replace(/obj *\|\| *\(obj *= *\{}\);/, '')
737802
.replace(/var __p;\s*__p/, 'var __p');
738803

739804
// remove comments, including sourceURLs
740-
code = code.replace(/\s*\/\/.*(?:\n|$)/g, '');
805+
snippet = snippet.replace(/\s*\/\/.*(?:\n|$)/g, '');
741806

742-
return '$1' + code + ';\n';
807+
return '$1' + snippet + ';\n';
743808
}()));
744809
}
745810

@@ -748,8 +813,11 @@
748813
// remove pseudo private properties
749814
source = source.replace(/(?:(?:\s*\/\/.*)*\s*lodash\._[^=]+=.+\n)+/g, '\n');
750815

816+
// cleanup code
817+
source = source.replace(/^ *;\n/gm, '');
818+
751819
// begin the minification process
752-
if (filterType || isBackbone || isMobile) {
820+
if (filterType || isBackbone || isLegacy || isMobile) {
753821
fs.writeFileSync(path.join(__dirname, 'lodash.custom.js'), source);
754822
minify(source, 'lodash.custom.min', function(result) {
755823
fs.writeFileSync(path.join(__dirname, 'lodash.custom.min.js'), result);

0 commit comments

Comments
 (0)
0