Skip to content

Commit

Permalink
fixup! module: make CJS load from ESM loader
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed May 20, 2023
1 parent 8eee769 commit 1d7e50d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class DefaultModuleLoader {
moduleProvider,
parentURL === undefined,
inspectBrk,
sync,
);

this.moduleMap.set(url, importAssertions.type, job);
Expand Down
19 changes: 14 additions & 5 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ModuleJob {
// `loader` is the Loader instance used for loading dependencies.
// `moduleProvider` is a function
constructor(loader, url, importAssertions = { __proto__: null },
moduleProvider, isMain, inspectBrk) {
moduleProvider, isMain, inspectBrk, sync = false) {
this.loader = loader;
this.importAssertions = importAssertions;
this.isMain = isMain;
Expand All @@ -64,6 +64,11 @@ class ModuleJob {
// `this.module` is also filled in below.
this.modulePromise = ReflectApply(moduleProvider, loader, [url, isMain]);

if (sync) {
this.module = this.modulePromise;
this.modulePromise = PromiseResolve(this.module);
}

// Wait for the ModuleWrap instance being linked with all dependencies.
const link = async () => {
this.module = await this.modulePromise;
Expand Down Expand Up @@ -187,14 +192,18 @@ class ModuleJob {
}

runSync() {
assert(this.modulePromise instanceof ModuleWrap);
if (this.instantiated !== undefined) {
return this.instantiated;
}
assert(this.module instanceof ModuleWrap);

// TODO: better handle errors
this.modulePromise.instantiate();
this.module.instantiate();
this.instantiated = PromiseResolve();
const timeout = -1;
const breakOnSigint = false;
this.modulePromise.evaluate(timeout, breakOnSigint);
return { __proto__: null, module: this.modulePromise };
this.module.evaluate(timeout, breakOnSigint);
return { __proto__: null, module: this.module };
}

async run() {
Expand Down

0 comments on commit 1d7e50d

Please sign in to comment.