Revert "module: disable cjs snapshotting into esm loader" by devsnek · Pull Request #34562 · nodejs/node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions lib/internal/modules/cjs/loader.js
6 changes: 5 additions & 1 deletion lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ require('internal/modules/cjs/loader');

const {
FunctionPrototypeBind,
ObjectSetPrototypeOf
ObjectSetPrototypeOf,
SafeMap,
} = primordials;

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

// Map of already-loaded CJS modules to use
this.cjsCache = new SafeMap();

// This hook is called before the first root module is imported. It's a
// function that returns a piece of code that runs as a sloppy-mode script.
// The script may evaluate to a function that can be called with a
Expand Down
34 changes: 19 additions & 15 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const {
StringPrototypeReplace,
} = primordials;

const assert = require('internal/assert');

let _TYPES = null;
function lazyTypes() {
if (_TYPES !== null) return _TYPES;
Expand Down Expand Up @@ -134,21 +132,27 @@ const isWindows = process.platform === 'win32';
const winSepRegEx = /\//g;
translators.set('commonjs', function commonjsStrategy(url, isMain) {
debug(`Translating CJSModule ${url}`);
let filename = internalURLModule.fileURLToPath(new URL(url));
if (isWindows)
filename = StringPrototypeReplace(filename, winSepRegEx, '\\');
const pathname = internalURLModule.fileURLToPath(new URL(url));
const cached = this.cjsCache.get(url);
if (cached) {
this.cjsCache.delete(url);
return cached;
}
const module = CJSModule._cache[
isWindows ? StringPrototypeReplace(pathname, winSepRegEx, '\\') : pathname
];
if (module && module.loaded) {
const exports = module.exports;
return new ModuleWrap(url, undefined, ['default'], function() {
this.setExport('default', exports);
});
}
return new ModuleWrap(url, undefined, ['default'], function() {
debug(`Loading CJSModule ${url}`);
let module = CJSModule._cache[filename];
if (!module || !module.loaded) {
module = new CJSModule(filename);
module.filename = filename;
module.paths = CJSModule._nodeModulePaths(module.path);
CJSModule._cache[filename] = module;
module.load(filename);
assert(module && module.loaded);
}
this.setExport('default', module.exports);
// We don't care about the return val of _load here because Module#load
// will handle it for us by checking the loader registry and filling the
// exports like above
CJSModule._load(pathname, undefined, isMain);
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/es-module/test-esm-snapshot.mjs