Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit df47720

Browse filesBrowse files
joyeecheungaduh95
authored andcommitted
lib: reduce cycles in esm loader and load it in snapshot
PR-URL: #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>
1 parent deda50a commit df47720
Copy full SHA for df47720

5 files changed

+21-44Lines changed: 21 additions & 44 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/bootstrap/switches/is_main_thread.js‎

Copy file name to clipboardExpand all lines: lib/internal/bootstrap/switches/is_main_thread.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ require('util');
296296
require('url'); // eslint-disable-line no-restricted-modules
297297
internalBinding('module_wrap');
298298
require('internal/modules/cjs/loader');
299+
require('internal/modules/esm/loader');
299300
require('internal/modules/esm/utils');
300301

301302
// Needed to refresh the time origin.
Collapse file

‎lib/internal/modules/esm/loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js
+6-24Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ const {
6565
validateLoadSloppy,
6666
} = require('internal/modules/customization_hooks');
6767

68-
let defaultResolve, defaultLoadSync;
69-
7068
const { tracingChannel } = require('diagnostics_channel');
7169
const onImport = tracingChannel('module.import');
7270

@@ -100,19 +98,9 @@ function newLoadCache() {
10098
return new LoadCache();
10199
}
102100

103-
let _translators;
104-
function lazyLoadTranslators() {
105-
_translators ??= require('internal/modules/esm/translators');
106-
return _translators;
107-
}
108-
109-
/**
110-
* Lazy-load translators to avoid potentially unnecessary work at startup (ex if ESM is not used).
111-
* @returns {import('./translators.js').Translators}
112-
*/
113-
function getTranslators() {
114-
return lazyLoadTranslators().translators;
115-
}
101+
const { translators } = require('internal/modules/esm/translators');
102+
const { defaultResolve } = require('internal/modules/esm/resolve');
103+
const { defaultLoadSync, throwUnknownModuleFormat } = require('internal/modules/esm/load');
116104

117105
/**
118106
* Generate message about potential race condition caused by requiring a cached module that has started
@@ -181,11 +169,6 @@ class ModuleLoader {
181169
*/
182170
loadCache = newLoadCache();
183171

184-
/**
185-
* Methods which translate input code or other information into ES modules
186-
*/
187-
translators = getTranslators();
188-
189172
/**
190173
* @see {AsyncLoaderHooks.isForAsyncLoaderHookWorker}
191174
* Shortcut to this.#asyncLoaderHooks.isForAsyncLoaderHookWorker.
@@ -459,7 +442,7 @@ class ModuleLoader {
459442
#translate(url, translateContext, parentURL) {
460443
const { translatorKey, format } = translateContext;
461444
this.validateLoadResult(url, format);
462-
const translator = getTranslators().get(translatorKey);
445+
const translator = translators.get(translatorKey);
463446

464447
if (!translator) {
465448
throw new ERR_UNKNOWN_MODULE_FORMAT(translatorKey, url);
@@ -710,7 +693,7 @@ class ModuleLoader {
710693
if (cachedResult != null) {
711694
return cachedResult;
712695
}
713-
defaultResolve ??= require('internal/modules/esm/resolve').defaultResolve;
696+
714697
const result = defaultResolve(specifier, context);
715698
this.#resolveCache.set(requestKey, parentURL, result);
716699
return result;
@@ -787,7 +770,6 @@ class ModuleLoader {
787770
if (this.#asyncLoaderHooks?.loadSync) {
788771
return this.#asyncLoaderHooks.loadSync(url, context);
789772
}
790-
defaultLoadSync ??= require('internal/modules/esm/load').defaultLoadSync;
791773
return defaultLoadSync(url, context);
792774
}
793775

@@ -813,7 +795,7 @@ class ModuleLoader {
813795

814796
validateLoadResult(url, format) {
815797
if (format == null) {
816-
require('internal/modules/esm/load').throwUnknownModuleFormat(url, format);
798+
throwUnknownModuleFormat(url, format);
817799
}
818800
}
819801

Collapse file

‎lib/internal/modules/esm/resolve.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+2-7Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ const {
4646
ERR_UNSUPPORTED_DIR_IMPORT,
4747
ERR_UNSUPPORTED_RESOLVE_REQUEST,
4848
} = require('internal/errors').codes;
49-
50-
const { Module: CJSModule } = require('internal/modules/cjs/loader');
49+
const { defaultGetFormatWithoutErrors } = require('internal/modules/esm/get_format');
5150
const { getConditionsSet } = require('internal/modules/esm/utils');
5251
const packageJsonReader = require('internal/modules/package_json_reader');
5352
const internalFsBinding = internalBinding('fs');
@@ -870,6 +869,7 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
870869
*/
871870
function resolveAsCommonJS(specifier, parentURL) {
872871
try {
872+
const { Module: CJSModule } = require('internal/modules/cjs/loader');
873873
const parent = fileURLToPath(parentURL);
874874
const tmpModule = new CJSModule(parent, null);
875875
tmpModule.paths = CJSModule._nodeModulePaths(parent);
@@ -1043,8 +1043,3 @@ module.exports = {
10431043
packageResolve,
10441044
throwIfInvalidParentURL,
10451045
};
1046-
1047-
// cycle
1048-
const {
1049-
defaultGetFormatWithoutErrors,
1050-
} = require('internal/modules/esm/get_format');
Collapse file

‎lib/internal/modules/esm/translators.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+5-12Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const {
1313
StringPrototypeReplaceAll,
1414
StringPrototypeSlice,
1515
StringPrototypeStartsWith,
16-
globalThis: { WebAssembly },
16+
globalThis,
1717
} = primordials;
1818

1919
const {
@@ -57,16 +57,7 @@ const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache');
5757
const moduleWrap = internalBinding('module_wrap');
5858
const { ModuleWrap, kEvaluationPhase } = moduleWrap;
5959

60-
// Lazy-loading to avoid circular dependencies.
61-
let getSourceSync;
62-
/**
63-
* @param {Parameters<typeof import('./load').getSourceSync>[0]} url
64-
* @returns {ReturnType<typeof import('./load').getSourceSync>}
65-
*/
66-
function getSource(url) {
67-
getSourceSync ??= require('internal/modules/esm/load').getSourceSync;
68-
return getSourceSync(url);
69-
}
60+
const { getSourceSync } = require('internal/modules/esm/load');
7061

7162
const { parse: cjsParse } = internalBinding('cjs_lexer');
7263

@@ -210,7 +201,7 @@ function createCJSModuleWrap(url, translateContext, parentURL, loadCJS = loadCJS
210201
const isMain = (parentURL === undefined);
211202
const filename = urlToFilename(url);
212203
// In case the source was not provided by the `load` step, we need fetch it now.
213-
source = stringify(source ?? getSource(new URL(url)).source);
204+
source = stringify(source ?? getSourceSync(new URL(url)).source);
214205

215206
const { exportNames, module } = cjsPreparseModuleExports(filename, source, sourceFormat);
216207
cjsCache.set(url, module);
@@ -516,6 +507,8 @@ translators.set('json', function jsonStrategy(url, translateContext) {
516507
const wasmInstances = new SafeWeakMap();
517508
translators.set('wasm', function(url, translateContext) {
518509
const { source } = translateContext;
510+
// WebAssembly global is not available during snapshot building, so we need to get it lazily.
511+
const { WebAssembly } = globalThis;
519512
assertBufferSource(source, false, 'load');
520513

521514
debug(`Translating WASMModule ${url}`, translateContext);
Collapse file

‎test/parallel/test-bootstrap-modules.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-bootstrap-modules.js
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,23 @@ expected.beforePreExec = new Set([
115115
'NativeModule internal/modules/run_main',
116116
'NativeModule internal/net',
117117
'NativeModule internal/dns/utils',
118+
'NativeModule internal/modules/esm/get_format',
118119
]);
119120

120121
expected.atRunTime = new Set([
121-
'NativeModule internal/modules/esm/get_format',
122122
'NativeModule internal/process/pre_execution',
123123
]);
124124

125125
const { isMainThread } = require('worker_threads');
126126

127127
if (isMainThread) {
128128
[
129+
'Internal Binding cjs_lexer',
130+
'NativeModule internal/modules/esm/assert',
131+
'NativeModule internal/modules/esm/loader',
132+
'NativeModule internal/modules/esm/load',
133+
'NativeModule internal/modules/esm/resolve',
134+
'NativeModule internal/modules/esm/translators',
129135
'NativeModule url',
130136
].forEach(expected.beforePreExec.add.bind(expected.beforePreExec));
131137
} else { // Worker.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.