Skip to content

Commit 2219859

Browse files
bmeckaddaleax
authored andcommitted
module: be lazy when creating CJS facades
This should remove the penalty for loading CJS that is never imported. PR-URL: nodejs#17153 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b44efde commit 2219859

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

lib/module.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,21 @@ Module.prototype.load = function(filename) {
575575
if (ESMLoader) {
576576
const url = getURLFromFilePath(filename);
577577
const urlString = `${url}`;
578+
const exports = this.exports;
578579
if (ESMLoader.moduleMap.has(urlString) !== true) {
579-
const ctx = createDynamicModule(['default'], url);
580-
ctx.reflect.exports.default.set(this.exports);
581-
ESMLoader.moduleMap.set(urlString,
582-
new ModuleJob(ESMLoader, url, async () => ctx));
580+
ESMLoader.moduleMap.set(
581+
urlString,
582+
new ModuleJob(ESMLoader, url, async () => {
583+
const ctx = createDynamicModule(
584+
['default'], url);
585+
ctx.reflect.exports.default.set(exports);
586+
return ctx;
587+
})
588+
);
583589
} else {
584590
const job = ESMLoader.moduleMap.get(urlString);
585591
if (job.reflect)
586-
job.reflect.exports.default.set(this.exports);
592+
job.reflect.exports.default.set(exports);
587593
}
588594
}
589595
};

0 commit comments

Comments
 (0)