Skip to content

Commit

Permalink
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 <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
guybedford authored and gibfahn committed Oct 31, 2017
1 parent b1e2a38 commit e683077
Show file tree
Hide file tree
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
Expand Up @@ -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);
}
}
Expand Down
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 e683077

Please sign in to comment.