Skip to content

Commit

Permalink
fix bugs in runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jul 6, 2023
1 parent 3feea65 commit b7d8009
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions crates/turbopack-ecmascript-runtime/js/src/shared/runtime-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ function esmExport(
exports: Exports,
getters: Record<string, () => any>
) {
esm((module.namespaceObject = exports), getters);
module.namespaceObject = module.exports;
esm(exports, getters);
}

/**
Expand Down Expand Up @@ -131,16 +132,15 @@ function dynamicExport(
},
});

// `exports` passed to this function will always be an object,
// `module.exports` might have been turned into a promise
// if this is inside an async module.
if (isPromise(module.exports)) {
module.namespaceObject = maybeWrapAsyncModulePromise(
module.exports,
// If this is inside an async module `module.namespaceObject` is a promise,
// so we need to replace it with a new promise.
if (isPromise(module.namespaceObject)) {
module.exports = module.namespaceObject = maybeWrapAsyncModulePromise(
module.namespaceObject,
() => namespaceObject
);
} else {
module.namespaceObject = namespaceObject;
module.exports = module.namespaceObject = namespaceObject;
}
}
reexportedObjects.push(object);
Expand Down Expand Up @@ -204,18 +204,17 @@ function esmImport(
): Exclude<Module["namespaceObject"], undefined> {
const module = getOrInstantiateModuleFromParent(id, sourceModule);
if (module.error) throw module.error;
if (module.namespaceObject) return module.namespaceObject;
const raw = module.exports;

if (isPromise(raw)) {
module.namespaceObject = maybeWrapAsyncModulePromise(raw, (e) =>
interopEsm(e, {}, e.__esModule)
);

return module.namespaceObject;
}
// any async module has to have `module.namespaceObject` defined
if (module.namespaceObject) return module.namespaceObject;

return (module.namespaceObject = interopEsm(raw, {}, raw.__esModule));
// can't be an async module at this point
const raw = module.exports;
return (module.namespaceObject = interopEsm(
raw,
{},
(raw as any).__esModule
));
}

function commonJsRequire(sourceModule: Module, id: ModuleId): Exports {
Expand Down Expand Up @@ -414,7 +413,7 @@ function asyncModule(
},
} satisfies AsyncModuleExt);

module.exports = promise;
module.exports = module.namespaceObject = promise;

function handleAsyncDependencies(deps: Dep[]) {
const currentDeps = wrapDeps(deps);
Expand Down

0 comments on commit b7d8009

Please sign in to comment.