diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 9c59b2b5a772186..a1ebb0652cd7529 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -65,12 +65,19 @@ const { // Map used to store CJS parsing data. const cjsParseCache = new SafeWeakMap(); +/** + * Map of already-loaded CJS modules to use. + */ +const cjsExportsCache = new SafeWeakMap(); // Set first due to cycle with ESM loader functions. module.exports = { - wrapSafe, Module, cjsParseCache, - get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; }, + cjsExportsCache, + cjsParseCache, initializeCJS, + Module, + wrapSafe, + get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; }, }; const { BuiltinModule } = require('internal/bootstrap/realm'); @@ -1206,14 +1213,11 @@ Module.prototype.load = function(filename) { Module._extensions[extension](this, filename); this.loaded = true; - const cascadedLoader = getCascadedLoader(); // Create module entry at load time to snapshot exports correctly const exports = this.exports; - // Preemptively cache - if ((module?.module === undefined || - module.module.getStatus() < kEvaluated) && - !cascadedLoader.cjsCache.has(this)) { - cascadedLoader.cjsCache.set(this, exports); + // Preemptively cache for ESM loader. + if (!cjsExportsCache.has(this)) { + cjsExportsCache.set(this, exports); } }; diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 0694c7ef2b902d4..f1daa719e59b974 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -86,11 +86,6 @@ class ModuleLoader { */ #defaultConditions = getDefaultConditions(); - /** - * Map of already-loaded CJS modules to use - */ - cjsCache = new SafeWeakMap(); - /** * The index for assigning unique URLs to anonymous module evaluation */ diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 47906cff86a225b..4ecf862be9ea7bc 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -42,6 +42,7 @@ const { const { Module: CJSModule, cjsParseCache, + cjsExportsCache, } = require('internal/modules/cjs/loader'); const { fileURLToPath, pathToFileURL, URL } = require('internal/url'); let debug = require('internal/util/debuglog').debuglog('esm', (fn) => { @@ -308,9 +309,9 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) { } let exports; - if (asyncESM.esmLoader.cjsCache.has(module)) { - exports = asyncESM.esmLoader.cjsCache.get(module); - asyncESM.esmLoader.cjsCache.delete(module); + if (cjsExportsCache.has(module)) { + exports = cjsExportsCache.get(module); + cjsExportsCache.delete(module); } else { ({ exports } = module); } diff --git a/lib/internal/modules/helpers.js b/lib/internal/modules/helpers.js index 6b30a1d8c76d4bc..8312f0ca07e9772 100644 --- a/lib/internal/modules/helpers.js +++ b/lib/internal/modules/helpers.js @@ -7,6 +7,7 @@ const { ObjectPrototypeHasOwnProperty, SafeMap, SafeSet, + SafeWeakMap, StringPrototypeCharCodeAt, StringPrototypeIncludes, StringPrototypeSlice,