Skip to content

Commit f6af595

Browse files
committed
[turbopack] Add registerEntry() to handle inline bootstrapping
1 parent 2c0d2e4 commit f6af595

16 files changed

Lines changed: 245 additions & 43 deletions

File tree

turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/base/build-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ contextPrototype.c = moduleCache
1010
// @ts-ignore
1111
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1212
function getOrInstantiateRuntimeModule(
13-
chunkPath: ChunkPath,
13+
chunkPath: ChunkPath | undefined,
1414
moduleId: ModuleId
1515
): Module {
1616
const module = moduleCache[moduleId]

turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/base/dev-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const chunkChunkListsMap: Map<ChunkPath, Set<ChunkListPath>> = new Map()
8686
*/
8787
// @ts-ignore
8888
function getOrInstantiateRuntimeModule(
89-
chunkPath: ChunkPath,
89+
chunkPath: ChunkPath | undefined,
9090
moduleId: ModuleId
9191
): Module {
9292
const module = devModuleCache[moduleId]

turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/base/runtime-base.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ interface RuntimeBackend {
6363
chunkPath: ChunkPath | ChunkScript,
6464
params?: RuntimeParams
6565
) => void
66+
/**
67+
* Registers an entry-only registration (inlined into the HTML): loads the params'
68+
* other chunks and runs its runtime modules, with no self chunk identity.
69+
*/
70+
registerEntry: (params: RuntimeParams) => void
6671
/**
6772
* Returns the same Promise for the same chunk URL.
6873
*/
@@ -90,7 +95,12 @@ function loadChunk(
9095
}
9196
browserContextPrototype.l = loadChunk
9297

93-
function loadInitialChunk(chunkPath: ChunkPath, chunkData: ChunkData) {
98+
// `chunkPath` is the source chunk; it is `undefined` for entry-only registrations,
99+
// which have no self chunk (`SourceData` already allows this).
100+
function loadInitialChunk(
101+
chunkPath: ChunkPath | undefined,
102+
chunkData: ChunkData
103+
) {
94104
return loadChunkInternal(SourceType.Runtime, chunkPath, chunkData)
95105
}
96106

turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/dom/runtime-backend-dom.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ const chunkResolvers: Map<ChunkUrl, ChunkResolver> = new Map()
6767
}
6868
},
6969

70+
// Handles an inlined entry-only registration. Load the entry's other chunks and run its
71+
// runtime modules with no source chunk (`undefined`).
72+
async registerEntry(params) {
73+
for (const otherChunkData of params.otherChunks) {
74+
const otherChunkUrl = getChunkRelativeUrl(getChunkPath(otherChunkData))
75+
getOrCreateResolver(otherChunkUrl)
76+
}
77+
78+
await Promise.all(
79+
params.otherChunks.map((otherChunkData) =>
80+
loadInitialChunk(undefined, otherChunkData)
81+
)
82+
)
83+
84+
for (const moduleId of params.runtimeModuleIds) {
85+
getOrInstantiateRuntimeModule(undefined, moduleId)
86+
}
87+
},
88+
7089
/**
7190
* Loads the given chunk, and returns a promise that resolves once the chunk
7291
* has been loaded.

turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/edge/runtime-backend-edge.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ let BACKEND: RuntimeBackend
5353
}
5454
},
5555

56+
// Entry-only registrations are inlined into the HTML and only produced for
57+
// the browser/DOM runtime; the edge ("none") runtime never receives them
58+
// (its chunks self-register via `registerChunk`).
59+
registerEntry(_params) {
60+
throw new Error('inline entry registration is not supported')
61+
},
62+
5663
loadChunkCached(_sourceType: SourceType, _chunkUrl: ChunkUrl) {
5764
throw new Error('chunk loading is not supported')
5865
},

turbopack/crates/turbopack-ecmascript-runtime/src/browser_runtime.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,23 @@ pub async fn get_browser_runtime_code(
211211
);
212212
}
213213

214-
// Registering chunks and chunk lists depends on the BACKEND variable, which is set by the
215-
// specific runtime code, hence it must be appended after it.
214+
// Registering chunks/chunk lists depends on the BACKEND variable set by the specific
215+
// runtime code, so it must be appended after it. `registerChunkOrEntry` dispatches
216+
// each queued registration: arrays are chunk registrations, while the inlined entry
217+
// objects (`{ otherChunks, runtimeModuleIds }`) go to `BACKEND.registerEntry`.
216218
writedoc!(
217219
code,
218220
r#"
221+
function registerChunkOrEntry(registration) {{
222+
if (Array.isArray(registration)) {{
223+
registerChunk(registration);
224+
}} else {{
225+
BACKEND.registerEntry(registration);
226+
}}
227+
}}
219228
var chunksToRegister = globalThis[{chunk_loading_global}];
220-
globalThis[{chunk_loading_global}] = {{ push: registerChunk }};
221-
chunksToRegister.forEach(registerChunk);
229+
globalThis[{chunk_loading_global}] = {{ push: registerChunkOrEntry }};
230+
chunksToRegister.forEach(registerChunkOrEntry);
222231
"#,
223232
chunk_loading_global = StringifyJs(&chunk_loading_global),
224233
)?;

turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/output/[turbopack]_runtime.js.map

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/output/turbopack-[turbopack]_runtime.js

Lines changed: 25 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_dev_runtime/output/[turbopack]_runtime.js.map

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_dev_runtime/output/turbopack-[turbopack]_runtime.js

Lines changed: 23 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)