Skip to content

Commit e683077

Browse files
guybedfordgibfahn
authored andcommitted
module: fix hook module CJS dependency loading
It can be useful to load dependencies as part of the loader hook definition file. This fixes a bug where `import x from 'x'` would always return `x` as `undefined` if the import was made in a loader hooks definition module. A parallel change to the CJS loading injection process meant that the CJS module wasn't being injected into the correct loader instance, which is corrected here with a test. PR-URL: #16381 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent b1e2a38 commit e683077

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

lib/module.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ Module._load = function(request, parent, isMain) {
447447
ESMLoader = new Loader();
448448
const userLoader = process.binding('config').userLoader;
449449
if (userLoader) {
450-
const hooks = await new Loader().import(userLoader);
450+
const hooks = await ESMLoader.import(userLoader);
451+
ESMLoader = new Loader();
451452
ESMLoader.hook(hooks);
452453
}
453454
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-with-dep.mjs
2+
/* eslint-disable required-modules */
3+
import './test-esm-ok.mjs';
4+
5+
// We just test that this module doesn't fail loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.format = 'esm';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import dep from './loader-dep.js';
2+
export function resolve (specifier, base, defaultResolve) {
3+
return {
4+
url: defaultResolve(specifier, base).url,
5+
format: dep.format
6+
};
7+
}

0 commit comments

Comments
 (0)