Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b234023

Browse files
committedJul 11, 2022
esm: leverage loaders when resolving subsequent loaders
1 parent 7d13f5e commit b234023

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed
 

‎dev_fixtures_do_not_merge/index.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import symbol from 'yyy/lib.mjs';
2+
3+
console.log(`main`, symbol);

‎dev_fixtures_do_not_merge/lib.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
console.log(`lib`);
2+
3+
export default 42;
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export async function resolve(specifier, context, defaultResolve) {
2+
console.log(`loader-a`, {specifier});
3+
return defaultResolve(specifier.replace(/^xxx\//, `./`));
4+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export async function resolve(specifier, context, defaultResolve) {
2+
console.log(`loader-b`, {specifier});
3+
return defaultResolve(specifier.replace(/^yyy\//, `./`));
4+
}

‎lib/internal/process/esm_loader.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,24 @@ async function initializeLoader() {
6060
cwd = 'file:///';
6161
}
6262

63+
const keyedExportsList = [];
64+
6365
// A separate loader instance is necessary to avoid cross-contamination
6466
// between internal Node.js and userland. For example, a module with internal
6567
// state (such as a counter) should be independent.
6668
const internalEsmLoader = new ESMLoader();
6769

68-
// Importation must be handled by internal loader to avoid poluting userland
69-
const keyedExportsList = await internalEsmLoader.import(
70-
customLoaders,
71-
pathToFileURL(cwd).href,
72-
ObjectCreate(null),
73-
);
70+
for (const customLoader of customLoaders) {
71+
// Importation must be handled by internal loader to avoid poluting userland
72+
const keyedExportsSublist = await internalEsmLoader.import(
73+
[customLoader],
74+
pathToFileURL(cwd).href,
75+
ObjectCreate(null),
76+
);
77+
78+
await internalEsmLoader.addCustomLoaders(keyedExportsSublist);
79+
keyedExportsList.push(...keyedExportsSublist);
80+
}
7481

7582
// Hooks must then be added to external/public loader
7683
// (so they're triggered in userland)

0 commit comments

Comments
 (0)
Please sign in to comment.