diff --git a/Readme.md b/Readme.md index f1e88cc..f2010d0 100644 --- a/Readme.md +++ b/Readme.md @@ -52,6 +52,8 @@ using the same options that were used for the original sandboxed module. * `sourceTransformersSingleOnly:` If false, the source transformers will not be run against modules required by the sandboxed module. By default it will take the same value as `singleOnly`. +* `ignoreMissing:` If true, injected modules will not be required to have a corresponding file +on the file system. By default this is false. ### SandboxedModule.require(moduleId, [options]) diff --git a/lib/sandboxed_module.js b/lib/sandboxed_module.js index 6bf483c..7a7095e 100644 --- a/lib/sandboxed_module.js +++ b/lib/sandboxed_module.js @@ -157,7 +157,13 @@ SandboxedModule.prototype._createRecursiveRequireProxy = function() { var cache = Object.create(null); var required = this._getRequires(); for (var key in required) { - var injectedFilename = requireLike(this.filename).resolve(key); + var injectedFilename; + try { + injectedFilename = requireLike(this.filename).resolve(key); + } catch(missing) { + if(!this._options.ignoreMissing) {throw missing;} + injectedFilename = key; + } cache[injectedFilename] = required[key]; } cache[this.filename] = this.exports; diff --git a/test/integration/test-inject-missing.js b/test/integration/test-inject-missing.js new file mode 100644 index 0000000..357d174 --- /dev/null +++ b/test/integration/test-inject-missing.js @@ -0,0 +1,9 @@ +var assert = require('assert'); +var SandboxedModule = require('../..'); + +var foo = SandboxedModule.require('../fixture/foo', { + "requires": {"doesNotExist": {}}, + "ignoreMissing": true +}); +assert.strictEqual(foo.foo, 'foo'); +assert.strictEqual(foo.bar, 'bar');