Skip to content

Commit

Permalink
module: be lazy when creating CJS facades
Browse files Browse the repository at this point in the history
This should remove the penalty for loading CJS that is never imported.

PR-URL: #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>
  • Loading branch information
bmeck authored and gibfahn committed Dec 20, 2017
1 parent 5d1b76c commit 371e3f1
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,15 +557,21 @@ Module.prototype.load = function(filename) {
if (ESMLoader) {
const url = getURLFromFilePath(filename);
const urlString = `${url}`;
const exports = this.exports;
if (ESMLoader.moduleMap.has(urlString) !== true) {
const ctx = createDynamicModule(['default'], url);
ctx.reflect.exports.default.set(this.exports);
ESMLoader.moduleMap.set(urlString,
new ModuleJob(ESMLoader, url, async () => ctx));
ESMLoader.moduleMap.set(
urlString,
new ModuleJob(ESMLoader, url, async () => {
const ctx = createDynamicModule(
['default'], url);
ctx.reflect.exports.default.set(exports);
return ctx;
})
);
} else {
const job = ESMLoader.moduleMap.get(urlString);
if (job.reflect)
job.reflect.exports.default.set(this.exports);
job.reflect.exports.default.set(exports);
}
}
};
Expand Down

0 comments on commit 371e3f1

Please sign in to comment.