From 756587d080c45fd45d829d95f58f1da12a210fdb Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 23 Feb 2013 14:08:48 -0800 Subject: [PATCH] Remove `undefined` from the IIFE to allow easier customizations with the `iife` build option. Former-commit-id: a496b1fba74fdab3b2819d27b769371f73364684 --- build.js | 20 ++++++++++---------- build/minify.js | 2 +- lodash.js | 9 ++++++--- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/build.js b/build.js index c37d4b359..106c510ab 100755 --- a/build.js +++ b/build.js @@ -588,7 +588,7 @@ ' 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. `lodash iife="!function(window,undefined){%output%}(this)"`)', + ' (e.g. `lodash iife="!function(window){%output%}(this)"`)', '', ' lodash template=... File path pattern used to match template files to precompile', ' (e.g. `lodash template=./*.jst`)', @@ -964,8 +964,8 @@ source = source .replace(/context/g, 'window') - .replace(/(?:\n +\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)?\n *var Array *=[\s\S]+?;\n/m, '') - .replace(/(?: *\/\/.*\n)* *var lodash *= *runInContext.+\n/, ''); + .replace(/(?:\n +\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/)?\n *var Array *=[\s\S]+?;\n/, '') + .replace(/^ *var lodash *= *runInContext.+\n+/m, ''); } else { source = source.replace(snippet, ''); @@ -1136,7 +1136,7 @@ // remove `noCharByIndex` from `_.at` source = source.replace(matchFunction(source, 'at'), function(match) { - return match.replace(/^ *if *\(noCharByIndex[^}]+}\n/m, ''); + return match.replace(/^ *if *\(noCharByIndex[^}]+}\n+/m, ''); }); // remove `noCharByIndex` from `_.reduceRight` @@ -2359,19 +2359,19 @@ source = source.replace(/(?: *\/\/.*\n)*( *)if *\(typeof +define[\s\S]+?else /, '$1'); } if (!isNode) { - source = source.replace(/(?: *\/\/.*\n)*( *)if *\(freeModule[\s\S]+?else *{([\s\S]+?\n)\1}\n/, '$1$2'); + source = source.replace(/(?: *\/\/.*\n)*( *)if *\(freeModule[\s\S]+?else *{([\s\S]+?\n)\1}\n+/, '$1$2'); } if (!isCommonJS) { - source = source.replace(/(?: *\/\/.*\n)*(?:( *)else *{)?\s*freeExports\.\w+ *=[\s\S]+?(?:\n\1})?\n/, ''); + source = source.replace(/(?: *\/\/.*\n)*(?:( *)else *{)?\s*freeExports\.\w+ *=[\s\S]+?(?:\n\1})?\n+/, ''); } if (!isGlobal) { - source = source.replace(/(?:( *)(})? *else(?: *if *\(_\))? *{)?(?:\s*\/\/.*)*\s*(?:window\._|_\.templates) *=[\s\S]+?(?:\n\1})?\n/g, '$1$2\n'); + source = source.replace(/(?:( *)(})? *else(?: *if *\(_\))? *{)?(?:\s*\/\/.*)*\s*(?:window\._|_\.templates) *=[\s\S]+?(?:\n\1})?\n+/g, '$1$2\n'); } // remove `if (freeExports) {...}` if it's empty if (isAMD && isGlobal) { - source = source.replace(/(?: *\/\/.*\n)* *(?:else )?if *\(freeExports\) *{\s*}\n/, ''); + source = source.replace(/(?: *\/\/.*\n)* *(?:else )?if *\(freeExports\) *{\s*}\n+/, ''); } else { - source = source.replace(/(?: *\/\/.*\n)* *(?:else )?if *\(freeExports\) *{\s*}(?:\s*else *{([\s\S]+?) *})?\n/, '$1\n'); + source = source.replace(/(?: *\/\/.*\n)* *(?:else )?if *\(freeExports\) *{\s*}(?:\s*else *{([\s\S]+?) *})?\n+/, '$1\n'); } }()); @@ -2454,7 +2454,7 @@ } if (!source.match(/var (?:hasDontEnumBug|hasEnumPrototype|iteratesOwnLast|nonEnumArgs)\b/g)) { // remove IIFE used to assign `hasDontEnumBug`, `hasEnumPrototype`, `iteratesOwnLast`, and `nonEnumArgs` - source = source.replace(/^ *\(function\(\) *{[\s\S]+?}\(1\)\);\n/m, ''); + source = source.replace(/^ *\(function\(\) *{[\s\S]+?}\(1\)\);\n+/m, ''); } } if ((source.match(/\bfreeModule\b/g) || []).length < 2) { diff --git a/build/minify.js b/build/minify.js index 2250669ee..365bd18d9 100755 --- a/build/minify.js +++ b/build/minify.js @@ -353,7 +353,7 @@ source = source.replace(license, ''); } - var hasIIFE = /^;?\(function[^{]+{\s*/.test(source), + var hasIIFE = /^;?\(function[^{]+{/.test(source), isStrict = hasIIFE && /^;?\(function[^{]+{\s*["']use strict["']/.test(source); // to avoid stripping the IIFE, convert it to a function call diff --git a/lodash.js b/lodash.js index dc956f387..975c53379 100644 --- a/lodash.js +++ b/lodash.js @@ -6,7 +6,10 @@ * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud Inc. * Available under MIT license */ -;(function(window, undefined) { +;(function(window) { + + /** Used as a safe reference for `undefined` in pre ES5 environments */ + var undefined; /** Detect free variable `exports` */ var freeExports = typeof exports == 'object' && exports; @@ -115,7 +118,7 @@ * @static * @memberOf _ * @category Utilities - * @param {Object} context The context object. + * @param {Object} [context=window] The context object. * @returns {Function} Returns the `lodash` function. */ function runInContext(context) { @@ -5161,9 +5164,9 @@ /*--------------------------------------------------------------------------*/ + // expose Lo-Dash var lodash = runInContext(); - // expose Lo-Dash // some AMD build optimizers, like r.js, check for specific condition patterns like the following: if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) { // Expose Lo-Dash to the global object even when an AMD loader is present in