Type HMR update instructions in Rust and generate their TS types
HMR update instructions were type-erased to `serde_json::Value` on the way
through the system, costing type safety on both sides of the napi bridge:
- In Rust, `next-api`'s `ChunkListUpdateBuilder` inspected instructions
stringly-typed (`obj.get("type").as_str()`, `as_object`/`as_array`, hand-built
`serde_json::Map`s).
- In TS, the wire shapes were hand-maintained in multiple copies with a
"keep in sync" warning and no generation.
This makes the instructions native, strongly-typed Rust structs end-to-end and
generates the TypeScript definitions from that same source of truth via ts-rs:
- Introduce `HmrUpdateInstruction` plus owned, `Serialize`+`Deserialize`
instruction structs in `turbopack-ecmascript::chunk_list`. Module ids are
modeled as `RcStr` (string), matching the existing wire contract.
- Rewrite `ChunkListUpdateBuilder` to deserialize into the typed enum and
pattern-match; type `chunk_list/update.rs`'s `merged` / `ChunkUpdate::Partial`
fields. The generic `turbopack-core` `PartialUpdate` seam still carries
`serde_json::Value`, so core gains no dependency on `turbopack-ecmascript`.
- Add `Rope` (de)serialization helpers so instruction structs round-trip.
- Generate the TS types with ts-rs (`cargo run --example export_hmr_types`),
vendored by `scripts/generate-hmr-types.ts` (wired into `build-native.ts`,
with a `--check` drift guard). Replace the hand-written copy in
`packages/next`'s `types.ts`; delete the redundant `hmr-types.d.ts` and point
the node runtime at the shared ambient protocol types.
Wire output is unchanged (byte-identical), covered by new unit tests.
<!-- NEXT_JS_LLM -->
Co-authored-by:
Will Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
Showing
Please
register
or
sign in
to comment