felixge / node-sandboxed-module

A sandboxed node.js module loader that lets you inject dependencies into your modules.
MIT License
342 stars 42 forks source link

Source transformers do not get applied to nested requires #41

Closed OliverJAsh closed 9 years ago

OliverJAsh commented 9 years ago

Given a dependency tree of: main > foo > bar. main requires foo as a sandboxed module, applying the source transformer. foo requires bar, however the source transformer is not applied.

This is demonstrated in my example where I log each time the source transformer is called (see gist for full example):

var SandboxedModule = require('sandboxed-module');

SandboxedModule.require('./foo', { sourceTransformers: {
    test: function (source) {
        console.log('test source transformer:', this.filename);
        return source;
    }
}});

Output:

❯ node main.js
test source transformer: /Users/Oliver/Development/tmp/sandbox-vm-error/foo.js

I expect:

❯ node main.js
test source transformer: /Users/Oliver/Development/tmp/sandbox-vm-error/foo.js
test source transformer: /Users/Oliver/Development/tmp/sandbox-vm-error/bar.js

https://gist.github.com/OliverJAsh/3351f4a96a02ef2e294e

OliverJAsh commented 9 years ago

For context: my sandboxed module (foo.js) is written in ES6, so I have written a transformer to compile to ES5. However, a module (bar.js) that the sandboxed module imports is also written in ES6. I realise this can be fixed by setting singleOnly: true, and running the file that runs SandboxedModule.require (main.js) inside an ES6 environment. However, I do want the nested require to be sandboxed (singleOnly: false) so that I can mock its dependencies – but I also want its source transformed.

Just re-assuring myself that my requirements here are not out of the ordinary.

mminder commented 9 years ago

+1