Skip to content

Commit

Permalink
Merge pull request #505 from NapkinHQ/fix-conditional-export-resolve
Browse files Browse the repository at this point in the history
Support conditional export resolution with custom resolver
  • Loading branch information
XmiliaH authored Feb 2, 2023
2 parents e7828cf + eefe3f1 commit fe3ab68
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/test.js
/node-*
/lib/events.js
/lib/events.js
/test/additional-modules/my-es-module/index.js
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export interface VMRequire {
/** Collection of mock modules (both external or built-in). */
mock?: any;
/* An additional lookup function in case a module wasn't found in one of the traditional node lookup paths. */
resolve?: (moduleName: string, parentDirname: string) => string | undefined;
resolve?: (moduleName: string, parentDirname: string) => string | { path: string, module?: string } | undefined;
/** Custom require to require host and built-in modules. */
customRequire?: (id: string) => any;
/** Load modules in strict mode. (default: true) */
Expand Down
9 changes: 7 additions & 2 deletions lib/resolver-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,13 @@ function resolverFromOptions(vm, options, override, compiler) {
}
const resolved = customResolver(x, path);
if (!resolved) return undefined;
if (externals) externals.push(new RegExp('^' + escapeRegExp(resolved)));
return resolver.loadAsFileOrDirecotry(resolved, extList);
if (typeof resolved === 'string') {
if (externals) externals.push(new RegExp('^' + escapeRegExp(resolved)));
return resolver.loadAsFileOrDirecotry(resolved, extList);
}
const {module=x, path: resolvedPath} = resolved;
if (externals) externals.push(new RegExp('^' + escapeRegExp(resolvedPath)));
return resolver.loadNodeModules(module, [resolvedPath], extList);
};
}

Expand Down
1 change: 1 addition & 0 deletions test/additional-modules/my-es-module/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {additional_cjs_module: true};
1 change: 1 addition & 0 deletions test/additional-modules/my-es-module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {additional_es_module: true};
12 changes: 12 additions & 0 deletions test/additional-modules/my-es-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"main": "index.js",
"type": "module",
"exports": {
".": {
"default": {
"require": "./index.cjs",
"default": "./index.js"
}
}
}
}
11 changes: 11 additions & 0 deletions test/nodevm.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ describe('modules', () => {
assert.ok(vm.run("require('my-module')", __filename));
});

it('can resolve conditional exports with a custom resolver', () => {
const vm = new NodeVM({
require: {
external: ['my-es-module'],
resolve: () => ({ path: path.resolve(__dirname, 'additional-modules') })
}
});

assert.ok(vm.run("require('my-es-module')", __filename));
});

it('allows for multiple root folders', () => {
const vm = new NodeVM({
require: {
Expand Down

0 comments on commit fe3ab68

Please sign in to comment.