Skip to content

Commit

Permalink
top-level-await support
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jul 4, 2023
1 parent c9868f7 commit 586e769
Show file tree
Hide file tree
Showing 35 changed files with 1,096 additions and 115 deletions.
2 changes: 1 addition & 1 deletion crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ pub async fn handle_resolve_error(
})
}

/// ModulePart represnts a part of a module.
/// ModulePart represents a part of a module.
///
/// Currently this is used only for ESMs.
#[turbo_tasks::value]
Expand Down
18 changes: 3 additions & 15 deletions crates/turbopack-ecmascript-runtime/js/src/build/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,8 @@ interface RequireContextEntry {

type ExternalRequire = (id: ModuleId) => Exports | EsmNamespaceObject;

interface TurbopackNodeBuildContext {
e: Module["exports"];
r: CommonJsRequire;
interface TurbopackNodeBuildContext extends TurbopackBaseContext {
x: ExternalRequire;
f: RequireContextFactory;
i: EsmImport;
s: EsmExport;
j: typeof dynamicExport;
v: ExportValue;
n: typeof exportNamespace;
m: Module;
c: ModuleCache;
l: LoadChunk;
g: typeof globalThis;
__dirname: string;
}

type ModuleFactory = (
Expand Down Expand Up @@ -176,13 +163,14 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
// NOTE(alexkirsz) This can fail when the module encounters a runtime error.
try {
moduleFactory.call(module.exports, {
a: asyncModule.bind(null, module),
e: module.exports,
r: commonJsRequire.bind(null, module),
x: externalRequire,
f: requireContext.bind(null, module),
i: esmImport.bind(null, module),
s: esm.bind(null, module.exports),
j: dynamicExport.bind(null, module),
j: dynamicExport.bind(null, module, module.exports),
v: exportValue.bind(null, module),
n: exportNamespace.bind(null, module),
m: module,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,8 @@ type RefreshContext = {

type RefreshHelpers = RefreshRuntimeGlobals["$RefreshHelpers$"];

interface TurbopackDevBaseContext {
e: Module["exports"];
r: CommonJsRequire;
f: RequireContextFactory;
i: EsmImport;
s: EsmExport;
j: typeof dynamicExport;
v: ExportValue;
n: typeof exportNamespace;
m: Module;
c: ModuleCache;
l: LoadChunk;
g: typeof globalThis;
interface TurbopackDevBaseContext extends TurbopackBaseContext {
k: RefreshContext;
__dirname: string;
}

interface TurbopackDevContext extends TurbopackDevBaseContext {}
Expand Down Expand Up @@ -332,12 +319,13 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
moduleFactory.call(
module.exports,
augmentContext({
a: asyncModule.bind(null, module),
e: module.exports,
r: commonJsRequire.bind(null, module),
f: requireContext.bind(null, module),
i: esmImport.bind(null, module),
s: esmExport.bind(null, module),
j: dynamicExport.bind(null, module),
s: esmExport.bind(null, module, module.exports),
j: dynamicExport.bind(null, module, module.exports),
v: exportValue.bind(null, module),
n: exportNamespace.bind(null, module),
m: module,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ interface RequireContextEntry {
}

type ExternalRequire = (id: ModuleId) => Exports | EsmNamespaceObject;
type ExternalImport = (id: ModuleId) => Promise<Exports | EsmNamespaceObject>;

interface TurbopackDevContext {
x: ExternalRequire;
y: ExternalImport;
}

function commonJsRequireContext(
Expand All @@ -27,6 +29,10 @@ function commonJsRequireContext(
: commonJsRequire(sourceModule, entry.id());
}

function externalImport(id: ModuleId) {
return import(id)
}

function externalRequire(
id: ModuleId,
esm: boolean = false
Expand Down Expand Up @@ -62,6 +68,7 @@ externalRequire.resolve = (
function augmentContext(context: TurbopackDevBaseContext): TurbopackDevContext {
const nodejsContext = context as TurbopackDevContext;
nodejsContext.x = externalRequire;
nodejsContext.y = externalImport;
return nodejsContext;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,43 @@ type ChunkData =
};

type CommonJsRequire = (moduleId: ModuleId) => Exports;
type CommonJsExport = (exports: Record<string, any>) => void;

type EsmImport = (
moduleId: ModuleId,
allowExportDefault: boolean
) => EsmNamespaceObject;
) => EsmNamespaceObject | Promise<EsmNamespaceObject>;
type EsmExport = (exportGetters: Record<string, () => any>) => void;
type ExportValue = (value: any) => void;
type ExportNamespace = (namespace: any) => void;
type DynamicExport = (object: Record<string, any>) => void;

type LoadChunk = (chunkPath: ChunkPath) => Promise<any> | undefined;

type ModuleCache = Record<ModuleId, Module>;
type ModuleFactories = Record<ModuleId, ModuleFactory>;

type AsyncModule = (
body: (
handleAsyncDependencies: (
deps: Dep[]
) => Exports[] | Promise<() => Exports[]>,
asyncResult: (err?: any) => void
) => void,
hasAwait: boolean
) => void;

interface TurbopackBaseContext {
a: AsyncModule;
e: Module["exports"];
r: CommonJsRequire;
f: RequireContextFactory;
i: EsmImport;
s: EsmExport;
j: DynamicExport;
v: ExportValue;
n: ExportNamespace;
m: Module;
c: ModuleCache;
l: LoadChunk;
g: typeof globalThis;
__dirname: string;
}
Loading

0 comments on commit 586e769

Please sign in to comment.