Skip to content

Commit 06da8a7

Browse files
bmeckMylesBorins
authored andcommitted
module: be lazy when creating CJS facades
This should remove the penalty for loading CJS that is never imported. PR-URL: #17153 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 4667c5e commit 06da8a7

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/module.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -572,15 +572,21 @@ Module.prototype.load = function(filename) {
572572
if (ESMLoader) {
573573
const url = getURLFromFilePath(filename);
574574
const urlString = `${url}`;
575+
const exports = this.exports;
575576
if (ESMLoader.moduleMap.has(urlString) !== true) {
576-
const ctx = createDynamicModule(['default'], url);
577-
ctx.reflect.exports.default.set(this.exports);
578-
ESMLoader.moduleMap.set(urlString,
579-
new ModuleJob(ESMLoader, url, async () => ctx));
577+
ESMLoader.moduleMap.set(
578+
urlString,
579+
new ModuleJob(ESMLoader, url, async () => {
580+
const ctx = createDynamicModule(
581+
['default'], url);
582+
ctx.reflect.exports.default.set(exports);
583+
return ctx;
584+
})
585+
);
580586
} else {
581587
const job = ESMLoader.moduleMap.get(urlString);
582588
if (job.reflect)
583-
job.reflect.exports.default.set(this.exports);
589+
job.reflect.exports.default.set(exports);
584590
}
585591
}
586592
};

0 commit comments

Comments
 (0)