Skip to content

Commit 1d7e50d

Browse files
committed
fixup! module: make CJS load from ESM loader
1 parent 8eee769 commit 1d7e50d

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/internal/modules/esm/loader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ class DefaultModuleLoader {
240240
moduleProvider,
241241
parentURL === undefined,
242242
inspectBrk,
243+
sync,
243244
);
244245

245246
this.moduleMap.set(url, importAssertions.type, job);

lib/internal/modules/esm/module_job.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ModuleJob {
5151
// `loader` is the Loader instance used for loading dependencies.
5252
// `moduleProvider` is a function
5353
constructor(loader, url, importAssertions = { __proto__: null },
54-
moduleProvider, isMain, inspectBrk) {
54+
moduleProvider, isMain, inspectBrk, sync = false) {
5555
this.loader = loader;
5656
this.importAssertions = importAssertions;
5757
this.isMain = isMain;
@@ -64,6 +64,11 @@ class ModuleJob {
6464
// `this.module` is also filled in below.
6565
this.modulePromise = ReflectApply(moduleProvider, loader, [url, isMain]);
6666

67+
if (sync) {
68+
this.module = this.modulePromise;
69+
this.modulePromise = PromiseResolve(this.module);
70+
}
71+
6772
// Wait for the ModuleWrap instance being linked with all dependencies.
6873
const link = async () => {
6974
this.module = await this.modulePromise;
@@ -187,14 +192,18 @@ class ModuleJob {
187192
}
188193

189194
runSync() {
190-
assert(this.modulePromise instanceof ModuleWrap);
195+
if (this.instantiated !== undefined) {
196+
return this.instantiated;
197+
}
198+
assert(this.module instanceof ModuleWrap);
191199

192200
// TODO: better handle errors
193-
this.modulePromise.instantiate();
201+
this.module.instantiate();
202+
this.instantiated = PromiseResolve();
194203
const timeout = -1;
195204
const breakOnSigint = false;
196-
this.modulePromise.evaluate(timeout, breakOnSigint);
197-
return { __proto__: null, module: this.modulePromise };
205+
this.module.evaluate(timeout, breakOnSigint);
206+
return { __proto__: null, module: this.module };
198207
}
199208

200209
async run() {

0 commit comments

Comments
 (0)