diff --git a/build.js b/build.js index a03000201..2393a9352 100755 --- a/build.js +++ b/build.js @@ -285,10 +285,14 @@ ' lodash mobile Build with IE < 9 bug fixes & method compilation removed', ' lodash strict Build with `_.bindAll`, `_.defaults`, & `_.extend` in strict mode', ' lodash underscore Build with only methods included in Underscore without iteration fixes', - ' lodash category=... Comma separated categories of methods to include in the build', ' lodash exclude=... Comma separated names of methods to exclude from the build', - ' lodash exports=... Comma separated names of ways to export the `LoDash` function', ' lodash include=... Comma separated names of methods to include in the build', + ' lodash category=... Comma separated categories of methods to include in the build', + ' (i.e. “arrays”, “chaining”, “collections”, “functions”, “objects”, and “utilities”)', + ' lodash exports=... Comma separated names of ways to export the `LoDash` function', + ' (i.e. “amd”, “commonjs”, “global”, “node”, and “none”)', + ' lodash iife=... Code to replace the immediately-invoked function expression that wraps Lo-Dash', + ' (e.g. “!function(window,undefined){%output%}(this)”)', '', ' All arguments, except `exclude` with `include` & `legacy` with `csp`/`mobile`,', ' may be combined.', @@ -674,7 +678,7 @@ // used to report invalid command-line arguments var invalidArgs = _.reject(options.slice(options[0] == 'node' ? 2 : 0), function(value, index, options) { if (/^(?:-o|--output)$/.test(options[index - 1]) || - /^(?:category|exclude|exports|include)=.*$/.test(value)) { + /^(?:category|exclude|exports|iife|include)=.*$/.test(value)) { return true; } return [ @@ -786,6 +790,11 @@ return pair[1]; }, ''); + // used to specify a custom IIFE to wrap Lo-Dash + var iife = options.reduce(function(result, value) { + return result || (result = value.match(/^iife=(.*)$/)) && result[1]; + }, ''); + // load customized Lo-Dash module var lodash = (function() { var context = vm.createContext({ @@ -1161,7 +1170,7 @@ /*------------------------------------------------------------------------*/ - // customize how the `LoDash` function is exported + // customize Lo-Dash's export bootstrap if (exportsOptions.indexOf('amd') == -1) { source = source.replace(/(?: *\/\/.*\n)*( +)if *\(typeof +define[\s\S]+?else /, '$1'); } @@ -1180,6 +1189,21 @@ /*------------------------------------------------------------------------*/ + // customize Lo-Dash's IIFE + (function() { + if (iife) { + var token = '%output%', + index = iife.indexOf(token); + + source = source.match(/\/\*![\s\S]+?\*\/\n/) + + iife.slice(0, index) + + source.replace(/^[^(]+?\(function[^{]+?{|}\(this\)\)[;\s]*$/g, '') + + iife.slice(index + token.length); + } + }()); + + /*------------------------------------------------------------------------*/ + // modify/remove references to removed methods/variables if (isRemoved(source, 'isArguments')) { source = replaceVar(source, 'noArgsClass', 'false'); @@ -1269,7 +1293,7 @@ /*------------------------------------------------------------------------*/ // used to specify creating a custom build - var isCustom = !_.isEqual(exportsOptions, exportsAll) || filterType || isBackbone || isLegacy || isMobile || isStrict || isUnderscore; + var isCustom = !_.isEqual(exportsOptions, exportsAll) || filterType || iife || isBackbone || isLegacy || isMobile || isStrict || isUnderscore; // used to specify the output path for builds var outputPath = options.reduce(function(result, value, index) { diff --git a/test/test-build.js b/test/test-build.js index 96ccbf165..4ff47b471 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -585,7 +585,8 @@ 'exports=amd', 'exports=commonjs', 'exports=global', - 'exports=node' + 'exports=node', + 'exports=none' ]; commands.forEach(function(command, index) { @@ -626,6 +627,11 @@ vm.runInContext(source, context); ok(context._ === undefined, basename); ok(_.isFunction(context.module.exports), basename); + break; + + case 4: + vm.runInContext(source, context); + ok(context._ === undefined, basename); } start(); }); @@ -635,6 +641,30 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('iife command'); + + (function() { + var start = _.after(2, _.once(QUnit.start)); + + asyncTest('`lodash iife=...`', function() { + build(['-s', 'iife=!function(window,undefined){%output%}(this)'], function(source, filepath) { + var basename = path.basename(filepath, '.js'), + context = createContext(); + + try { + vm.runInContext(source, context); + } catch(e) { } + + var lodash = context._ || {}; + ok(_.isString(lodash.VERSION), basename); + ok(/!function/.test(source), basename); + start(); + }); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('output options'); (function() { @@ -676,7 +706,7 @@ (function() { var start = _.once(QUnit.start); - asyncTest('`minify underscore.js`', function() { + asyncTest('`node minify underscore.js`', function() { var source = fs.readFileSync(path.join(__dirname, '..', 'vendor', 'underscore', 'underscore.js'), 'utf8'); minify(source, { 'silent': true,