Skip to content

Globals don't propagate to required modules #11

Closed
domenic opened this Issue Jul 31, 2012 · 4 comments

3 participants

@domenic
Collaborator
domenic commented Jul 31, 2012

I was trying very hard to do

var domify = sandboxedModule.require("domify", { globals: { document: myFakeDocument } });

But domify resolves to /node_modules/domify/index.js, which is just

module.exports = require('./lib/domify');

But if I insert a console.log(document) at the top of /node_modules/domify/lib/domify.js, it fails, indicating the document global didn't make it through the dependency chain.

@domenic domenic added a commit that referenced this issue Feb 12, 2013
@domenic domenic Add failing test for #11 a9971c1
@jamesdavidson

Any ideas on how to do this @felixge or @domenic? It would be really handy!

@domenic
Collaborator
domenic commented Feb 20, 2013

I gave it a shot, but it's pretty tricky. You basically need to inject some kind of sandboxed module require into the sub-dependencies, but that's not easy to do with the current codebase.

You can work around it like so:

var domify = sandboxedModule.require("domify", {
  globals: { document: myFakeDocument }
  requires: {
    "./lib/domify": sandboxedModule.require("domify/lib/domify", {
      globals: { document: myFakeDocument }
    })
  }
});

(it might be "domify/lib/domify" instead of "./lib/domify", not sure.)

@jamesdavidson

Ah, the exact same workaround I'm currently hacking on. It'll do for now. Thanks mate

@thlorenz
thlorenz commented Jun 1, 2014

We recently added this feature to proxyquire.

I'm not sure if that helps you at all since it works with require extensions (so may not apply here), but I thought it might help you to have a look.

If you prefer to not read diffs, it all happens in here.

@Illniyar Illniyar closed this in 350321b Jun 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.