Skip to content

Commit dc00a07

Browse files
committed
Revert "module: disable cjs snapshotting into esm loader"
This reverts commit 1fe39f0. PR-URL: #34562 Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Guy Bedford <[email protected]>
1 parent 2d2a812 commit dc00a07

File tree

4 files changed

+55
-19
lines changed

4 files changed

+55
-19
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const {
3232
ArrayPrototypeJoin,
3333
Error,
3434
JSONParse,
35+
Map,
3536
Number,
3637
ObjectCreate,
3738
ObjectDefineProperty,
@@ -112,6 +113,8 @@ const {
112113
} = require('internal/util/types');
113114

114115
const asyncESM = require('internal/process/esm_loader');
116+
const ModuleJob = require('internal/modules/esm/module_job');
117+
const { ModuleWrap, kInstantiated } = internalBinding('module_wrap');
115118
const {
116119
encodedSepRegEx,
117120
packageInternalResolve
@@ -371,7 +374,7 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
371374
// In order to minimize unnecessary lstat() calls,
372375
// this cache is a list of known-real paths.
373376
// Set to an empty Map to reset.
374-
const realpathCache = new SafeMap();
377+
const realpathCache = new Map();
375378

376379
// Check if the file exists and is not a directory
377380
// if using --preserve-symlinks and isMain is false,
@@ -1115,6 +1118,31 @@ Module.prototype.load = function(filename) {
11151118
}
11161119
Module._extensions[extension](this, filename);
11171120
this.loaded = true;
1121+
1122+
const ESMLoader = asyncESM.ESMLoader;
1123+
const url = `${pathToFileURL(filename)}`;
1124+
const module = ESMLoader.moduleMap.get(url);
1125+
// Create module entry at load time to snapshot exports correctly
1126+
const exports = this.exports;
1127+
// Called from cjs translator
1128+
if (module !== undefined && module.module !== undefined) {
1129+
if (module.module.getStatus() >= kInstantiated)
1130+
module.module.setExport('default', exports);
1131+
} else {
1132+
// Preemptively cache
1133+
// We use a function to defer promise creation for async hooks.
1134+
ESMLoader.moduleMap.set(
1135+
url,
1136+
// Module job creation will start promises.
1137+
// We make it a function to lazily trigger those promises
1138+
// for async hooks compatibility.
1139+
() => new ModuleJob(ESMLoader, url, () =>
1140+
new ModuleWrap(url, undefined, ['default'], function() {
1141+
this.setExport('default', exports);
1142+
})
1143+
, false /* isMain */, false /* inspectBrk */)
1144+
);
1145+
}
11181146
};
11191147

11201148

@@ -1234,7 +1262,7 @@ Module.prototype._compile = function(content, filename) {
12341262
const exports = this.exports;
12351263
const thisValue = exports;
12361264
const module = this;
1237-
if (requireDepth === 0) statCache = new SafeMap();
1265+
if (requireDepth === 0) statCache = new Map();
12381266
if (inspectorWrapper) {
12391267
result = inspectorWrapper(compiledWrapper, thisValue, exports,
12401268
require, module, filename, dirname);

lib/internal/modules/esm/loader.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ require('internal/modules/cjs/loader');
55

66
const {
77
FunctionPrototypeBind,
8-
ObjectSetPrototypeOf
8+
ObjectSetPrototypeOf,
9+
SafeMap,
910
} = primordials;
1011

1112
const {
@@ -45,6 +46,9 @@ class Loader {
4546
// Registry of loaded modules, akin to `require.cache`
4647
this.moduleMap = new ModuleMap();
4748

49+
// Map of already-loaded CJS modules to use
50+
this.cjsCache = new SafeMap();
51+
4852
// This hook is called before the first root module is imported. It's a
4953
// function that returns a piece of code that runs as a sloppy-mode script.
5054
// The script may evaluate to a function that can be called with a

lib/internal/modules/esm/translators.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const {
1111
StringPrototypeReplace,
1212
} = primordials;
1313

14-
const assert = require('internal/assert');
15-
1614
let _TYPES = null;
1715
function lazyTypes() {
1816
if (_TYPES !== null) return _TYPES;
@@ -134,21 +132,27 @@ const isWindows = process.platform === 'win32';
134132
const winSepRegEx = /\//g;
135133
translators.set('commonjs', function commonjsStrategy(url, isMain) {
136134
debug(`Translating CJSModule ${url}`);
137-
let filename = internalURLModule.fileURLToPath(new URL(url));
138-
if (isWindows)
139-
filename = StringPrototypeReplace(filename, winSepRegEx, '\\');
135+
const pathname = internalURLModule.fileURLToPath(new URL(url));
136+
const cached = this.cjsCache.get(url);
137+
if (cached) {
138+
this.cjsCache.delete(url);
139+
return cached;
140+
}
141+
const module = CJSModule._cache[
142+
isWindows ? StringPrototypeReplace(pathname, winSepRegEx, '\\') : pathname
143+
];
144+
if (module && module.loaded) {
145+
const exports = module.exports;
146+
return new ModuleWrap(url, undefined, ['default'], function() {
147+
this.setExport('default', exports);
148+
});
149+
}
140150
return new ModuleWrap(url, undefined, ['default'], function() {
141151
debug(`Loading CJSModule ${url}`);
142-
let module = CJSModule._cache[filename];
143-
if (!module || !module.loaded) {
144-
module = new CJSModule(filename);
145-
module.filename = filename;
146-
module.paths = CJSModule._nodeModulePaths(module.path);
147-
CJSModule._cache[filename] = module;
148-
module.load(filename);
149-
assert(module && module.loaded);
150-
}
151-
this.setExport('default', module.exports);
152+
// We don't care about the return val of _load here because Module#load
153+
// will handle it for us by checking the loader registry and filling the
154+
// exports like above
155+
CJSModule._load(pathname, undefined, isMain);
152156
});
153157
});
154158

test/es-module/test-esm-snapshot.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ import '../fixtures/es-modules/esm-snapshot-mutator.js';
33
import one from '../fixtures/es-modules/esm-snapshot.js';
44
import assert from 'assert';
55

6-
assert.strictEqual(one, 2);
6+
assert.strictEqual(one, 1);

0 commit comments

Comments
 (0)