module: move the CJS exports cache to internal/modules/cjs/loader · nodejs/node@b621ada · GitHub
Skip to content

Commit b621ada

Browse files
joyeecheungRafaelGSS
authored andcommitted
module: move the CJS exports cache to internal/modules/cjs/loader
This puts it together with the cjsParseCache and reduces the circular dependency on the singleton loader, which is the only place where this cache is stored. Drive-by: remove always-false module status check because there's no longer a local module variable after #34605 which is now invalid leftover code at this point and only doesn't throw because we happen to have a top-level variable called module. PR-URL: #51157 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9bfd84c commit b621ada

4 files changed

Lines changed: 18 additions & 19 deletions

File tree

lib/internal/bootstrap/switches/is_main_thread.js

Lines changed: 2 additions & 1 deletion

lib/internal/modules/cjs/loader.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,19 @@ const {
6565

6666
// Map used to store CJS parsing data.
6767
const cjsParseCache = new SafeWeakMap();
68+
/**
69+
* Map of already-loaded CJS modules to use.
70+
*/
71+
const cjsExportsCache = new SafeWeakMap();
6872

6973
// Set first due to cycle with ESM loader functions.
7074
module.exports = {
71-
wrapSafe, Module, cjsParseCache,
72-
get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; },
75+
cjsExportsCache,
76+
cjsParseCache,
7377
initializeCJS,
78+
Module,
79+
wrapSafe,
80+
get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; },
7481
};
7582

7683
const { BuiltinModule } = require('internal/bootstrap/realm');
@@ -150,7 +157,6 @@ const {
150157
isProxy,
151158
} = require('internal/util/types');
152159

153-
const { kEvaluated } = internalBinding('module_wrap');
154160
const isWindows = process.platform === 'win32';
155161

156162
const relativeResolveCache = { __proto__: null };
@@ -1206,14 +1212,11 @@ Module.prototype.load = function(filename) {
12061212
Module._extensions[extension](this, filename);
12071213
this.loaded = true;
12081214

1209-
const cascadedLoader = getCascadedLoader();
12101215
// Create module entry at load time to snapshot exports correctly
12111216
const exports = this.exports;
1212-
// Preemptively cache
1213-
if ((module?.module === undefined ||
1214-
module.module.getStatus() < kEvaluated) &&
1215-
!cascadedLoader.cjsCache.has(this)) {
1216-
cascadedLoader.cjsCache.set(this, exports);
1217+
// Preemptively cache for ESM loader.
1218+
if (!cjsExportsCache.has(this)) {
1219+
cjsExportsCache.set(this, exports);
12171220
}
12181221
};
12191222

lib/internal/modules/esm/loader.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const {
1111
JSONStringify,
1212
ObjectSetPrototypeOf,
1313
RegExpPrototypeSymbolReplace,
14-
SafeWeakMap,
1514
encodeURIComponent,
1615
hardenRegExp,
1716
} = primordials;
@@ -86,11 +85,6 @@ class ModuleLoader {
8685
*/
8786
#defaultConditions = getDefaultConditions();
8887

89-
/**
90-
* Map of already-loaded CJS modules to use
91-
*/
92-
cjsCache = new SafeWeakMap();
93-
9488
/**
9589
* The index for assigning unique URLs to anonymous module evaluation
9690
*/

lib/internal/modules/esm/translators.js

Lines changed: 4 additions & 3 deletions

0 commit comments

Comments
 (0)