Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: fix hook module CJS dependency loading
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
guybedford authored and gibfahn committed Oct 30, 2017

Verified

This commit was signed with the committer’s verified signature. The key has expired.
addaleax Anna Henningsen
1 parent 6a20849 commit 7a01402
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
@@ -447,7 +447,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 changes: 5 additions & 0 deletions test/es-module/test-esm-loader-dependency.mjs
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/loader-dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.format = 'esm';
7 changes: 7 additions & 0 deletions test/fixtures/es-module-loaders/loader-with-dep.mjs
Original file line number Diff line number Diff line change
@@ -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
};
}

0 comments on commit 7a01402

Please sign in to comment.