Skip to content

Commit df47720

Browse 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

5 files changed

Lines changed: 21 additions & 44 deletions

File tree

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

Lines 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.

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

Lines 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-
letdefaultResolve,defaultLoadSync;
69-
7068
const{ tracingChannel }=require('diagnostics_channel');
7169
constonImport=tracingChannel('module.import');
7270

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

103-
let_translators;
104-
functionlazyLoadTranslators(){
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-
functiongetTranslators(){
114-
returnlazyLoadTranslators().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-
consttranslator=getTranslators().get(translatorKey);
445+
consttranslator=translators.get(translatorKey);
463446

464447
if(!translator){
465448
thrownewERR_UNKNOWN_MODULE_FORMAT(translatorKey,url);
@@ -710,7 +693,7 @@ class ModuleLoader {
710693
if(cachedResult!=null){
711694
returncachedResult;
712695
}
713-
defaultResolve??=require('internal/modules/esm/resolve').defaultResolve;
696+
714697
constresult=defaultResolve(specifier,context);
715698
this.#resolveCache.set(requestKey,parentURL,result);
716699
returnresult;
@@ -787,7 +770,6 @@ class ModuleLoader {
787770
if(this.#asyncLoaderHooks?.loadSync){
788771
returnthis.#asyncLoaderHooks.loadSync(url,context);
789772
}
790-
defaultLoadSync??=require('internal/modules/esm/load').defaultLoadSync;
791773
returndefaultLoadSync(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

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

Lines 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
constpackageJsonReader=require('internal/modules/package_json_reader');
5352
constinternalFsBinding=internalBinding('fs');
@@ -870,6 +869,7 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
870869
*/
871870
functionresolveAsCommonJS(specifier,parentURL){
872871
try{
872+
const{Module: CJSModule}=require('internal/modules/cjs/loader');
873873
constparent=fileURLToPath(parentURL);
874874
consttmpModule=newCJSModule(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');

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

Lines 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
constmoduleWrap=internalBinding('module_wrap');
5858
const{ ModuleWrap, kEvaluationPhase }=moduleWrap;
5959

60-
// Lazy-loading to avoid circular dependencies.
61-
letgetSourceSync;
62-
/**
63-
* @param {Parameters<typeof import('./load').getSourceSync>[0]} url
64-
* @returns {ReturnType<typeof import('./load').getSourceSync>}
65-
*/
66-
functiongetSource(url){
67-
getSourceSync??=require('internal/modules/esm/load').getSourceSync;
68-
returngetSourceSync(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
constisMain=(parentURL===undefined);
211202
constfilename=urlToFilename(url);
212203
// In case the source was not provided by the `load` step, we need fetch it now.
213-
source=stringify(source??getSource(newURL(url)).source);
204+
source=stringify(source??getSourceSync(newURL(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
constwasmInstances=newSafeWeakMap();
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);

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

Lines 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=newSet([
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)