Skip to content

Commit fe3ab68

Browse files
authored
Merge pull request #505 from NapkinHQ/fix-conditional-export-resolve
Support conditional export resolution with custom resolver
2 parents e7828cf + eefe3f1 commit fe3ab68

File tree

7 files changed

+35
-4
lines changed

7 files changed

+35
-4
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/test.js
22
/node-*
3-
/lib/events.js
3+
/lib/events.js
4+
/test/additional-modules/my-es-module/index.js

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export interface VMRequire {
8282
/** Collection of mock modules (both external or built-in). */
8383
mock?: any;
8484
/* An additional lookup function in case a module wasn't found in one of the traditional node lookup paths. */
85-
resolve?: (moduleName: string, parentDirname: string) => string | undefined;
85+
resolve?: (moduleName: string, parentDirname: string) => string | { path: string, module?: string } | undefined;
8686
/** Custom require to require host and built-in modules. */
8787
customRequire?: (id: string) => any;
8888
/** Load modules in strict mode. (default: true) */

lib/resolver-compat.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,13 @@ function resolverFromOptions(vm, options, override, compiler) {
322322
}
323323
const resolved = customResolver(x, path);
324324
if (!resolved) return undefined;
325-
if (externals) externals.push(new RegExp('^' + escapeRegExp(resolved)));
326-
return resolver.loadAsFileOrDirecotry(resolved, extList);
325+
if (typeof resolved === 'string') {
326+
if (externals) externals.push(new RegExp('^' + escapeRegExp(resolved)));
327+
return resolver.loadAsFileOrDirecotry(resolved, extList);
328+
}
329+
const {module=x, path: resolvedPath} = resolved;
330+
if (externals) externals.push(new RegExp('^' + escapeRegExp(resolvedPath)));
331+
return resolver.loadNodeModules(module, [resolvedPath], extList);
327332
};
328333
}
329334

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {additional_cjs_module: true};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {additional_es_module: true};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"main": "index.js",
3+
"type": "module",
4+
"exports": {
5+
".": {
6+
"default": {
7+
"require": "./index.cjs",
8+
"default": "./index.js"
9+
}
10+
}
11+
}
12+
}

test/nodevm.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,17 @@ describe('modules', () => {
239239
assert.ok(vm.run("require('my-module')", __filename));
240240
});
241241

242+
it('can resolve conditional exports with a custom resolver', () => {
243+
const vm = new NodeVM({
244+
require: {
245+
external: ['my-es-module'],
246+
resolve: () => ({ path: path.resolve(__dirname, 'additional-modules') })
247+
}
248+
});
249+
250+
assert.ok(vm.run("require('my-es-module')", __filename));
251+
});
252+
242253
it('allows for multiple root folders', () => {
243254
const vm = new NodeVM({
244255
require: {

0 commit comments

Comments
 (0)