lib: reduce cycles in esm loader and load it in snapshot

PR-URL: https://github.com/nodejs/node/pull/61769
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
This commit is contained in:
Joyee Cheung authored and Antoine du Hamel committed 2026-02-22 01:07:45 +01:00
1 parent deda50a819
commit df477202ae
5 files changed
+21 -44

No files matched your search

@@ -296,6 +296,7 @@ require('util');
require('url'); // eslint-disable-line no-restricted-modules
internalBinding('module_wrap');
require('internal/modules/cjs/loader');
require('internal/modules/esm/loader');
require('internal/modules/esm/utils');
// Needed to refresh the time origin.
+6 -24
View File
@@ -65,8 +65,6 @@ const {
validateLoadSloppy,
} = require('internal/modules/customization_hooks');
let defaultResolve, defaultLoadSync;
const { tracingChannel } = require('diagnostics_channel');
const onImport = tracingChannel('module.import');
@@ -100,19 +98,9 @@ function newLoadCache() {
return new LoadCache();
}
let _translators;
function lazyLoadTranslators() {
_translators ??= require('internal/modules/esm/translators');
return _translators;
}
/**
* Lazy-load translators to avoid potentially unnecessary work at startup (ex if ESM is not used).
* @returns {import('./translators.js').Translators}
*/
function getTranslators() {
return lazyLoadTranslators().translators;
}
const { translators } = require('internal/modules/esm/translators');
const { defaultResolve } = require('internal/modules/esm/resolve');
const { defaultLoadSync, throwUnknownModuleFormat } = require('internal/modules/esm/load');
/**
* Generate message about potential race condition caused by requiring a cached module that has started
@@ -181,11 +169,6 @@ class ModuleLoader {
*/
loadCache = newLoadCache();
/**
* Methods which translate input code or other information into ES modules
*/
translators = getTranslators();
/**
* @see {AsyncLoaderHooks.isForAsyncLoaderHookWorker}
* Shortcut to this.#asyncLoaderHooks.isForAsyncLoaderHookWorker.
@@ -459,7 +442,7 @@ class ModuleLoader {
#translate(url, translateContext, parentURL) {
const { translatorKey, format } = translateContext;
this.validateLoadResult(url, format);
const translator = getTranslators().get(translatorKey);
const translator = translators.get(translatorKey);
if (!translator) {
throw new ERR_UNKNOWN_MODULE_FORMAT(translatorKey, url);
@@ -710,7 +693,7 @@ class ModuleLoader {
if (cachedResult != null) {
return cachedResult;
}
defaultResolve ??= require('internal/modules/esm/resolve').defaultResolve;
const result = defaultResolve(specifier, context);
this.#resolveCache.set(requestKey, parentURL, result);
return result;
@@ -787,7 +770,6 @@ class ModuleLoader {
if (this.#asyncLoaderHooks?.loadSync) {
return this.#asyncLoaderHooks.loadSync(url, context);
}
defaultLoadSync ??= require('internal/modules/esm/load').defaultLoadSync;
return defaultLoadSync(url, context);
}
@@ -813,7 +795,7 @@ class ModuleLoader {
validateLoadResult(url, format) {
if (format == null) {
require('internal/modules/esm/load').throwUnknownModuleFormat(url, format);
throwUnknownModuleFormat(url, format);
}
}
+2 -7
View File
@@ -46,8 +46,7 @@ const {
ERR_UNSUPPORTED_DIR_IMPORT,
ERR_UNSUPPORTED_RESOLVE_REQUEST,
} = require('internal/errors').codes;
const { Module: CJSModule } = require('internal/modules/cjs/loader');
const { defaultGetFormatWithoutErrors } = require('internal/modules/esm/get_format');
const { getConditionsSet } = require('internal/modules/esm/utils');
const packageJsonReader = require('internal/modules/package_json_reader');
const internalFsBinding = internalBinding('fs');
@@ -870,6 +869,7 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
*/
function resolveAsCommonJS(specifier, parentURL) {
try {
const { Module: CJSModule } = require('internal/modules/cjs/loader');
const parent = fileURLToPath(parentURL);
const tmpModule = new CJSModule(parent, null);
tmpModule.paths = CJSModule._nodeModulePaths(parent);
@@ -1043,8 +1043,3 @@ module.exports = {
packageResolve,
throwIfInvalidParentURL,
};
// cycle
const {
defaultGetFormatWithoutErrors,
} = require('internal/modules/esm/get_format');
+5 -12
View File
@@ -13,7 +13,7 @@ const {
StringPrototypeReplaceAll,
StringPrototypeSlice,
StringPrototypeStartsWith,
globalThis: { WebAssembly },
globalThis,
} = primordials;
const {
@@ -57,16 +57,7 @@ const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache');
const moduleWrap = internalBinding('module_wrap');
const { ModuleWrap, kEvaluationPhase } = moduleWrap;
// Lazy-loading to avoid circular dependencies.
let getSourceSync;
/**
* @param {Parameters<typeof import('./load').getSourceSync>[0]} url
* @returns {ReturnType<typeof import('./load').getSourceSync>}
*/
function getSource(url) {
getSourceSync ??= require('internal/modules/esm/load').getSourceSync;
return getSourceSync(url);
}
const { getSourceSync } = require('internal/modules/esm/load');
const { parse: cjsParse } = internalBinding('cjs_lexer');
@@ -210,7 +201,7 @@ function createCJSModuleWrap(url, translateContext, parentURL, loadCJS = loadCJS
const isMain = (parentURL === undefined);
const filename = urlToFilename(url);
// In case the source was not provided by the `load` step, we need fetch it now.
source = stringify(source ?? getSource(new URL(url)).source);
source = stringify(source ?? getSourceSync(new URL(url)).source);
const { exportNames, module } = cjsPreparseModuleExports(filename, source, sourceFormat);
cjsCache.set(url, module);
@@ -516,6 +507,8 @@ translators.set('json', function jsonStrategy(url, translateContext) {
const wasmInstances = new SafeWeakMap();
translators.set('wasm', function(url, translateContext) {
const { source } = translateContext;
// WebAssembly global is not available during snapshot building, so we need to get it lazily.
const { WebAssembly } = globalThis;
assertBufferSource(source, false, 'load');
debug(`Translating WASMModule ${url}`, translateContext);
+7 -1
View File
@@ -115,10 +115,10 @@ expected.beforePreExec = new Set([
'NativeModule internal/modules/run_main',
'NativeModule internal/net',
'NativeModule internal/dns/utils',
'NativeModule internal/modules/esm/get_format',
]);
expected.atRunTime = new Set([
'NativeModule internal/modules/esm/get_format',
'NativeModule internal/process/pre_execution',
]);
@@ -126,6 +126,12 @@ const { isMainThread } = require('worker_threads');
if (isMainThread) {
[
'Internal Binding cjs_lexer',
'NativeModule internal/modules/esm/assert',
'NativeModule internal/modules/esm/loader',
'NativeModule internal/modules/esm/load',
'NativeModule internal/modules/esm/resolve',
'NativeModule internal/modules/esm/translators',
'NativeModule url',
].forEach(expected.beforePreExec.add.bind(expected.beforePreExec));
} else { // Worker.