diff --git a/lib/internal/bootstrap/switches/is_main_thread.js b/lib/internal/bootstrap/switches/is_main_thread.js index 74486ba5310..06ac27c8486 100644 --- a/lib/internal/bootstrap/switches/is_main_thread.js +++ b/lib/internal/bootstrap/switches/is_main_thread.js @@ -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. diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 41513d1b9f3..37eb267e154 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -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); } } diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 473398bf29b..d253a3ff672 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -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'); diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index f45defe9dad..8cf008d52ab 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -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[0]} url - * @returns {ReturnType} - */ -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); diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index 4c8be110e06..67a59c3ff7f 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -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.