Skip to content

Commit a95dab5

Browse files
committed
Remove cjsRetryAsESMCache
1 parent a7e857a commit a95dab5

File tree

2 files changed

+4
-12
lines changed

2 files changed

+4
-12
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,10 @@ const cjsParseCache = new SafeWeakMap();
6969
*/
7070
const cjsExportsCache = new SafeWeakMap();
7171

72-
/**
73-
* Map of CJS modules where the initial attempt to parse as CommonJS failed;
74-
* we want to retry as ESM but avoid reading the module from disk again.
75-
* @type {SafeMap<string, string>} filename: contents
76-
*/
77-
const cjsRetryAsESMCache = new SafeMap();
78-
7972
// Set first due to cycle with ESM loader functions.
8073
module.exports = {
8174
cjsExportsCache,
8275
cjsParseCache,
83-
cjsRetryAsESMCache,
8476
initializeCJS,
8577
Module,
8678
wrapSafe,
@@ -1304,7 +1296,7 @@ function wrapSafe(filename, content, cjsModuleInstance, codeCache) {
13041296
if (process.mainModule === cjsModuleInstance) {
13051297
if (getOptionValue('--experimental-detect-module')) {
13061298
// For the main entry point, cache the source to potentially retry as ESM.
1307-
cjsRetryAsESMCache.set(filename, content);
1299+
process.mainModule._retryAsESMSource = content;
13081300
} else {
13091301
// We only enrich the error (print a warning) if we're sure we're going to for-sure throw it; so if we're
13101302
// retrying as ESM, wait until we know whether we're going to retry before calling `enrichCJSError`.

lib/internal/modules/run_main.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ function executeUserEntryPoint(main = process.argv[1]) {
168168
try {
169169
Module._load(main, null, true);
170170
} catch (error) {
171-
const { cjsRetryAsESMCache } = require('internal/modules/cjs/loader');
172-
const source = cjsRetryAsESMCache.get(resolvedMain);
173-
cjsRetryAsESMCache.delete(resolvedMain);
171+
const source = process.mainModule._retryAsESMSource;
172+
// In case the entry point is a large file, such as a bundle, release it from memory since we're done with it.
173+
process.mainModule._retryAsESMSource = undefined; // No need to `delete` since we don't care if the key exists.
174174
const { shouldRetryAsESM } = require('internal/modules/helpers');
175175
retryAsESM = shouldRetryAsESM(error.message, source);
176176
if (!retryAsESM) {

0 commit comments

Comments
 (0)