Remove undefined from the IIFE to allow easier customizations with the iife build option.

Former-commit-id: a496b1fba74fdab3b2819d27b769371f73364684
This commit is contained in:
John-David Dalton
2013-02-23 14:08:48 -08:00
parent 1ad2c92324
commit 756587d080
3 changed files with 17 additions and 14 deletions

View File

@@ -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) {

View File

@@ -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

View File

@@ -6,7 +6,10 @@
* Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
* Available under MIT license <http://lodash.com/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