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: https://github.com/nodejs/node/pull/16381 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
edd78f86ac
commit
d853758a22
@ -431,7 +431,8 @@ Module._load = function(request, parent, isMain) {
|
||||
ESMLoader = new Loader();
|
||||
const userLoader = process.binding('config').userLoader;
|
||||
if (userLoader) {
|
||||
const hooks = await new Loader().import(userLoader);
|
||||
const hooks = await ESMLoader.import(userLoader);
|
||||
ESMLoader = new Loader();
|
||||
ESMLoader.hook(hooks);
|
||||
}
|
||||
}
|
||||
|
5
test/es-module/test-esm-loader-dependency.mjs
Normal file
5
test/es-module/test-esm-loader-dependency.mjs
Normal file
@ -0,0 +1,5 @@
|
||||
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-with-dep.mjs
|
||||
/* eslint-disable required-modules */
|
||||
import './test-esm-ok.mjs';
|
||||
|
||||
// We just test that this module doesn't fail loading
|
1
test/fixtures/es-module-loaders/loader-dep.js
vendored
Normal file
1
test/fixtures/es-module-loaders/loader-dep.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
exports.format = 'esm';
|
7
test/fixtures/es-module-loaders/loader-with-dep.mjs
vendored
Normal file
7
test/fixtures/es-module-loaders/loader-with-dep.mjs
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
import dep from './loader-dep.js';
|
||||
export function resolve (specifier, base, defaultResolve) {
|
||||
return {
|
||||
url: defaultResolve(specifier, base).url,
|
||||
format: dep.format
|
||||
};
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user