Skip to content

Commit 09dc5be

Browse files
committed
Fix shadowing of internal module, exports and require when a global counterpart exists
@see jhnns/rewire-webpack#6
1 parent 1eede09 commit 09dc5be

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/getImportGlobalsSrc.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ function getImportGlobalsSrc(ignore) {
1616
ignore = ignore || [];
1717
// global itself can't be overridden because it's the only reference to our real global objects
1818
ignore.push("global");
19+
// ignore 'module', 'exports' and 'require' on the global scope, because otherwise our code would
20+
// shadow the module-internal variables
21+
// @see https://github.com/jhnns/rewire-webpack/pull/6
22+
ignore.push("module", "exports", "require");
1923

2024
for (key in globalObj) { /* jshint forin: false */
2125
if (ignore.indexOf(key) !== -1) {

test/getImportGlobalsSrc.test.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,24 @@ describe("getImportGlobalsSrc", function () {
77
var context = {
88
global: global
99
},
10+
expectedGlobals,
1011
src,
11-
actualGlobals,
12-
expectedGlobals = Object.keys(global);
12+
actualGlobals;
13+
14+
// Temporarily set module-internal variables on the global scope to check if getImportGlobalsSrc()
15+
// ignores them properly
16+
global.module = module;
17+
global.exports = exports;
18+
global.require = require;
1319

1420
src = getImportGlobalsSrc();
21+
22+
delete global.module;
23+
delete global.exports;
24+
delete global.require;
25+
26+
expectedGlobals = Object.keys(global);
27+
1528
vm.runInNewContext(src, context);
1629
actualGlobals = Object.keys(context).filter(function (key) {
1730
// node v0.10 does not set a constructor property on the context

0 commit comments

Comments
 (0)