Skip to content

Commit e5cae10

Browse files
committed
Revert "module: synchronously load most ES modules"
This reverts commit c442f47. PR-URL: #66786 Refs: #66767 Refs: #62530 Reviewed-By: Richard Lau <richard.lau@ibm.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent 12daa54 commit e5cae10

9 files changed

Lines changed: 84 additions & 373 deletions

File tree

‎benchmark/esm/startup-esm-graph.js‎

Lines changed: 0 additions & 66 deletions
This file was deleted.

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

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const {
1111
PromisePrototypeThen,
1212
RegExpPrototypeSymbolReplace,
1313
encodeURIComponent,
14-
globalThis,
1514
hardenRegExp,
1615
} = primordials;
1716

@@ -23,11 +22,6 @@ const {
2322
kRequiredModuleSymbol,
2423
} = require('internal/modules/cjs/loader');
2524
const { imported_cjs_symbol } = internalBinding('symbols');
26-
const {
27-
privateSymbols: {
28-
entry_point_module_private_symbol,
29-
},
30-
} = internalBinding('util');
3125

3226
const assert = require('internal/assert');
3327
const {
@@ -356,39 +350,6 @@ class ModuleLoader {
356350
return { wrap: job.module, namespace: job.runSync(parent).namespace };
357351
}
358352

359-
/**
360-
* Load and evaluate the entry point module.
361-
* The graph is always loaded and linked synchronously. When it contains no top-level
362-
* await it is evaluated synchronously too, so startup creates no promises at all.
363-
* When it does contain top-level await, evaluation falls back to `job.run()` here,
364-
* reusing the job that was just built rather than making the caller re-enter the
365-
* loader and redo the resolution and linking for the same module.
366-
* @param {string} url The URL of the entry point module.
367-
* @returns {ModuleJobSync|undefined} The job still needing asynchronous evaluation when
368-
* the graph has top-level await, otherwise `undefined` - the module is then already
369-
* fully evaluated and no promise was ever created.
370-
*/
371-
importForEntryPoint(url) {
372-
return onImport.traceSync(() => {
373-
const request = { specifier: url, phase: kEvaluationPhase, attributes: kEmptyObject, __proto__: null };
374-
const job = this.getOrCreateModuleJob(undefined, request, kImportInImportedESM);
375-
job.instantiateSync();
376-
if (job.module.hasAsyncGraph) {
377-
// Hand the job back so the caller can evaluate it through the ordinary
378-
// asynchronous entry point machinery, without re-resolving and re-linking
379-
// the module the way re-entering `import()` would.
380-
return job;
381-
}
382-
globalThis[entry_point_module_private_symbol] = job.module;
383-
job.runSync();
384-
return undefined;
385-
}, {
386-
__proto__: null,
387-
parentURL: undefined,
388-
url,
389-
});
390-
}
391-
392353
/**
393354
* Check invariants on a cached module job when require()'d from ESM.
394355
* @param {string} specifier The first parameter of require().
@@ -611,14 +572,10 @@ class ModuleLoader {
611572
assert(moduleOrModulePromise instanceof ModuleWrap, `Expected ModuleWrap for loading ${url}`);
612573
}
613574

575+
// TODO(joyeecheung): use ModuleJobSync for kRequireInImportedCJS too.
576+
const ModuleJobCtor = (requestType === kImportInRequiredESM ? ModuleJobSync : ModuleJob);
614577
const isMain = (parentURL === undefined);
615578
const inspectBrk = (isMain && getOptionValue('--inspect-brk'));
616-
// Use ModuleJobSync whenever we're on the main thread (not the async loader hook worker),
617-
// except for kRequireInImportedCJS (TODO: consolidate that case too).
618-
// TODO(joyeecheung): use ModuleJobSync for kRequireInImportedCJS too.
619-
const ModuleJobCtor = (this.isForAsyncLoaderHookWorker ||
620-
requestType === kRequireInImportedCJS) ?
621-
ModuleJob : ModuleJobSync;
622579
job = new ModuleJobCtor(
623580
this,
624581
url,
@@ -645,9 +602,8 @@ class ModuleLoader {
645602
*/
646603
getOrCreateModuleJob(parentURL, request, requestType) {
647604
let maybePromise;
648-
if (!this.isForAsyncLoaderHookWorker) {
649-
// On the main thread, always resolve synchronously;
650-
// `resolveSync` coordinates with the async loader hook worker if needed.
605+
if (requestType === kRequireInImportedCJS || requestType === kImportInRequiredESM) {
606+
// In these two cases, resolution must be synchronous.
651607
maybePromise = this.resolveSync(parentURL, request);
652608
assert(!isPromise(maybePromise));
653609
} else {

0 commit comments

Comments
 (0)