diff --git a/build.js b/build.js index cf0166832..30efecff0 100755 --- a/build.js +++ b/build.js @@ -3333,14 +3333,16 @@ // inline all functions defined with `createIterator` _.functions(lodash).forEach(function(methodName) { - // strip leading underscores to match pseudo private functions - var reFunc = RegExp('^( *)(var ' + methodName.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[methodName], indent)) + ';\n'; - }); + if (!(isUnderscore && isLodashMethod(methodName))) { + // strip leading underscores to match pseudo private functions + var reFunc = RegExp('^( *)(var ' + methodName.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[methodName], indent)) + ';\n'; + }); + } } }); @@ -3423,46 +3425,48 @@ }()); } } - else { - source = removeFromCreateIterator(source, 'support'); - // inline `iteratorTemplate` template - source = source.replace(getIteratorTemplate(source), function(match) { - var indent = getIndent(match), - snippet = cleanupCompiled(getFunctionSource(lodash._iteratorTemplate, indent)); + source = removeFromCreateIterator(source, 'support'); - // prepend data object references to property names to avoid having to - // use a with-statement - iteratorOptions.forEach(function(prop) { - if (prop !== 'support') { - snippet = snippet.replace(RegExp('([^\\w.])' + prop + '\\b', 'g'), '$1obj.' + prop); - } - }); + // inline `iteratorTemplate` template + source = source.replace(getIteratorTemplate(source), function(match) { + var indent = getIndent(match), + snippet = cleanupCompiled(getFunctionSource(lodash._iteratorTemplate, indent)); - // remove unnecessary code - snippet = snippet - .replace(/var __t.+/, "var __p = '';") - .replace(/function print[^}]+}/, '') - .replace(/'(?:\\n|\s)+'/g, "''") - .replace(/__p *\+= *' *';/g, '') - .replace(/\s*\+\s*'';/g, ';') - .replace(/(__p *\+= *)' *' *\+/g, '$1') - .replace(/\(\(__t *= *\( *([\s\S]+?) *\)\) *== *null *\? *'' *: *__t\)/g, '($1)'); - - // remove the with-statement - snippet = snippet.replace(/ *with *\(.+?\) *{/, '\n').replace(/}([^}]*}[^}]*$)/, '$1'); - - // minor cleanup - snippet = snippet - .replace(/obj\s*\|\|\s*\(obj *= *{}\);/, '') - .replace(/var __p = '';\s*__p \+=/, 'var __p ='); - - // remove comments, including sourceURLs - snippet = snippet.replace(/\s*\/\/.*(?:\n|$)/g, ''); - - return indent + 'var iteratorTemplate = ' + snippet + ';\n'; + // prepend data object references to property names to avoid having to + // use a with-statement + iteratorOptions.forEach(function(prop) { + if (prop !== 'support') { + snippet = snippet.replace(RegExp('([^\\w.])' + prop + '\\b', 'g'), '$1obj.' + prop); + } }); - } + + // remove unnecessary code + snippet = snippet + .replace(/var __t.+/, "var __p = '';") + .replace(/function print[^}]+}/, '') + .replace(/'(?:\\n|\s)+'/g, "''") + .replace(/__p *\+= *' *';/g, '') + .replace(/\s*\+\s*'';/g, ';') + .replace(/(__p *\+= *)' *' *\+/g, '$1') + .replace(/\(\(__t *= *\( *([\s\S]+?) *\)\) *== *null *\? *'' *: *__t\)/g, '($1)'); + + // remove the with-statement + snippet = snippet.replace(/ *with *\(.+?\) *{/, '\n').replace(/}([^}]*}[^}]*$)/, '$1'); + + // minor cleanup + snippet = snippet + .replace(/obj\s*\|\|\s*\(obj *= *{}\);/, '') + .replace(/var __p = '';\s*__p \+=/, 'var __p ='); + + // remove comments, including sourceURLs + snippet = snippet.replace(/\s*\/\/.*(?:\n|$)/g, ''); + + // replace `iteratorTemplate` assignment + snippet = indent + 'var iteratorTemplate = ' + snippet + ';\n'; + + return snippet; + }); // remove methods from the build allMethods.forEach(function(otherName) { @@ -3597,6 +3601,9 @@ source = source.replace(/\blodash\.(createCallback\()\b/g, '$1'); } if (isNoDep) { + // remove all horizontal rule comment separators + source = source.replace(/^ *\/\*-+\*\/\n/gm, ''); + _.each(buildMethods, function(methodName) { _.each(getAliases(methodName), function(alias) { source = removeFunction(source, alias); @@ -3607,8 +3614,12 @@ source = removeVar(source, varName); }); - // remove all horizontal rule comment separators - source = source.replace(/^ *\/\*-+\*\/\n/gm, ''); + if (!isAMD && !isCommonJS && !isGlobal && !isNode) { + source = removeFunction(source, 'lodash'); + source = removeLodashWrapper(source); + source = removePseudoPrivates(source); + source = removeMethodAssignments(source); + } } else { if (isExcluded('bind')) { @@ -3728,12 +3739,6 @@ 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 = removePseudoPrivates(source); - source = removeMethodAssignments(source); - } debugSource = cleanupSource(source); source = debugSource; diff --git a/test/test-build.js b/test/test-build.js index c170e3d4c..905773f71 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -1363,16 +1363,11 @@ commands.forEach(function(command) { asyncTest('`lodash modern include=each ' + command +'`', function() { - var start = _.after(2, _.once(QUnit.start)); + var start = _.once(QUnit.start); build(['-s', 'modern', 'include=each', command], function(data) { - var basename = path.basename(data.outputPath, '.js'), - context = createContext(); - - vm.runInContext(data.source, context); - var lodash = context._; - - deepEqual(_.functions(lodash), ['each', 'forEach'], basename); + var basename = path.basename(data.outputPath, '.js'); + strictEqual(/function createCallback\b/.test(data.source), false, basename); start(); }); }); @@ -1582,12 +1577,11 @@ 'include=each,filter,map', 'include=once plus=bind,Chaining', 'category=collections,functions', - 'backbone legacy category=utilities minus=first,last', + 'backbone category=utilities minus=first,last', 'legacy include=defer', - 'legacy underscore', 'modern strict include=isArguments,isArray,isFunction,isPlainObject,key', 'underscore include=debounce,throttle plus=after minus=throttle', - 'underscore mobile strict category=functions exports=amd,global plus=pick,uniq', + 'underscore strict category=functions exports=amd,global plus=pick,uniq', ] .concat( allMethods.map(function(methodName) { @@ -1595,27 +1589,14 @@ }) ); - commands.forEach(function(origCommand) { - _.times(4, function(index) { - var command = origCommand; + var reNonCombinable = /\b(?:backbone|legacy|mobile|modern|underscore)\b/; - if (index == 1) { - if (/\b(?:legacy|mobile)\b/.test(command)) { - return; - } - command = 'mobile ' + command; - } - else if (index == 2) { - if (/\b(?:legacy|modern)\b/.test(command)) { - return; - } - command = 'modern ' + command; - } - else if (index == 3) { - if (/\b(?:category|legacy|underscore)\b/.test(command)) { - return; - } - command = 'underscore ' + command; + commands.forEach(function(origCommand) { + _.each(['', 'mobile', 'modern', 'underscore'], function(otherCommand) { + var command = (otherCommand + ' ' + origCommand).trim(); + if ((otherCommand && reNonCombinable.test(origCommand)) || + (otherCommand == 'underscore' && /\bcategory\b/.test(origCommand))) { + return; } asyncTest('`lodash ' + command +'`', function() { var start = _.after(2, _.once(QUnit.start));