Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJohannes Ewald <johannes.ewald@peerigon.com>2015-04-28 00:40:29 +0300
committerJohannes Ewald <johannes.ewald@peerigon.com>2015-04-28 00:47:37 +0300
commit839e62b3842d075b23b71bf7c02a2bc9ca1e695a (patch)
treea2deb630d6d6f9fe3a14aac551551fc1de3b4179 /lib
parent88f6024b9ed4b733ec652e9c8317f9afc7a2c76d (diff)
Use IIFE like in https://github.com/jhnns/rewire-webpack/blob/e6ae0c2be0cd7d273ef272b91c2934ecf3447edf/lib/rewire.loader.js
Closes #56
Diffstat (limited to 'lib')
-rw-r--r--lib/rewire.js12
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/rewire.js b/lib/rewire.js
index 7d0b138..6d52aac 100644
--- a/lib/rewire.js
+++ b/lib/rewire.js
@@ -36,8 +36,20 @@ function internalRewire(parentModulePath, targetPath) {
// We prepend a list of all globals declared with var so they can be overridden (without changing original globals)
prelude = getImportGlobalsSrc();
+ // The module src is wrapped inside a self-executing function.
+ // This is necessary to separate the module src from the preceding importGlobalsSrc,
+ // because the module src can be in strict mode.
+ // In strict mode eval() can only declare vars in the current scope. In this case our setters
+ // and getters won't work.
+ // @see http://whereswalden.com/2011/01/10/new-es5-strict-mode-support-new-vars-created-by-strict-mode-eval-code-are-local-to-that-code-only/
+ // It also circumvents a problem with identical global variables and function declarations
+ // @see https://github.com/jhnns/rewire/issues/56
+ prelude += "(function () { ";
+
// We append our special setter and getter.
appendix = "\n" + getDefinePropertySrc();
+ // End of self-executing function
+ appendix += "})();";
// Check if the module uses the strict mode.
// If so we must ensure that "use strict"; stays at the beginning of the module.