Skip to content

Commit

Permalink
scope Node requires to baseURL rooting
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jul 6, 2016
1 parent 5944371 commit 2fbfed4
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ if (typeof require != 'undefined' && typeof process != 'undefined' && !process.b
*/

var parentModuleContext;
function getNodeModule(name) {
function getNodeModule(name, baseURL) {
if (!isPlain(name))
throw new Error('Node module ' + name + ' can\'t be loaded as it is not a package require.');

if (!parentModuleContext) {
var Module = this._nodeRequire('module');
parentModuleContext = new Module(process.cwd());
parentModuleContext.paths = Module._nodeModulePaths(process.cwd());
var base = baseURL.substr(isWindows ? 8 : 7);
parentModuleContext = new Module(base);
parentModuleContext.paths = Module._nodeModulePaths(base);
}
return parentModuleContext.require(name);
}
Expand Down Expand Up @@ -127,7 +128,7 @@ function coreResolve(name, parentName) {
if (name.substr(0, 6) == '@node/') {
if (!this._nodeRequire)
throw new TypeError('Error loading ' + name + '. Can only load node core modules in Node.');
this.set(name, this.newModule(getESModule(getNodeModule.call(this, name.substr(6)))));
this.set(name, this.newModule(getESModule(getNodeModule.call(this, name.substr(6), this.baseURL))));
return name;
}

Expand Down

0 comments on commit 2fbfed4

Please sign in to comment.