diff options
Diffstat (limited to 'lib/rewire.js')
-rw-r--r-- | lib/rewire.js | 12 |
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. |