Skip to content

Commit

Permalink
Fix shadowing of internal module, exports and require when a gl…
Browse files Browse the repository at this point in the history
…obal counterpart exists

@see jhnns/rewire-webpack#6
  • Loading branch information
jhnns committed Nov 4, 2014
1 parent 1eede09 commit 09dc5be
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/getImportGlobalsSrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function getImportGlobalsSrc(ignore) {
ignore = ignore || [];
// global itself can't be overridden because it's the only reference to our real global objects
ignore.push("global");
// ignore 'module', 'exports' and 'require' on the global scope, because otherwise our code would
// shadow the module-internal variables
// @see https://github.com/jhnns/rewire-webpack/pull/6
ignore.push("module", "exports", "require");

for (key in globalObj) { /* jshint forin: false */
if (ignore.indexOf(key) !== -1) {
Expand Down
17 changes: 15 additions & 2 deletions test/getImportGlobalsSrc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@ describe("getImportGlobalsSrc", function () {
var context = {
global: global
},
expectedGlobals,
src,
actualGlobals,
expectedGlobals = Object.keys(global);
actualGlobals;

// Temporarily set module-internal variables on the global scope to check if getImportGlobalsSrc()
// ignores them properly
global.module = module;
global.exports = exports;
global.require = require;

src = getImportGlobalsSrc();

delete global.module;
delete global.exports;
delete global.require;

expectedGlobals = Object.keys(global);

vm.runInNewContext(src, context);
actualGlobals = Object.keys(context).filter(function (key) {
// node v0.10 does not set a constructor property on the context
Expand Down

0 comments on commit 09dc5be

Please sign in to comment.