Unverified Commit 8e02bdd1 authored by vercel-fleet[bot]'s avatar vercel-fleet[bot] Committed by GitHub

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: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 5ada2ffa
...@@ -9872,6 +9872,28 @@ dependencies = [ ...@@ -9872,6 +9872,28 @@ dependencies = [
"toml 1.0.1+spec-1.1.0", "toml 1.0.1+spec-1.1.0",
] ]
[[package]]
name = "ts-rs"
version = "12.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "756050066659291d47a554a9f558125db17428b073c5ffce1daf5dcb0f7231d8"
dependencies = [
"thiserror 2.0.12",
"ts-rs-macros",
]
[[package]]
name = "ts-rs-macros"
version = "12.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38d90eea51bc7988ef9e674bf80a85ba6804739e535e9cab48e4bb34a8b652aa"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.104",
"termcolor",
]
[[package]] [[package]]
name = "tungstenite" name = "tungstenite"
version = "0.18.0" version = "0.18.0"
...@@ -10645,6 +10667,7 @@ dependencies = [ ...@@ -10645,6 +10667,7 @@ dependencies = [
"thread_local", "thread_local",
"tokio", "tokio",
"tracing", "tracing",
"ts-rs",
"turbo-bincode", "turbo-bincode",
"turbo-esregex", "turbo-esregex",
"turbo-frozenmap", "turbo-frozenmap",
......
...@@ -357,6 +357,7 @@ tokio = "1.52.1" ...@@ -357,6 +357,7 @@ tokio = "1.52.1"
tokio-util = { version = "0.7.18", features = ["io", "rt"] } tokio-util = { version = "0.7.18", features = ["io", "rt"] }
tracing = "0.1.44" tracing = "0.1.44"
tracing-subscriber = "0.3.16" tracing-subscriber = "0.3.16"
ts-rs = "12.0.1"
triomphe = { git = "https://github.com/sokra/triomphe", branch = "sokra/unstable" } triomphe = { git = "https://github.com/sokra/triomphe", branch = "sokra/unstable" }
xxhash-rust = { version = "0.8.12", features = ["xxh3"] } xxhash-rust = { version = "0.8.12", features = ["xxh3"] }
unsize = "1.1.0" unsize = "1.1.0"
......
use std::sync::Arc; use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use rustc_hash::FxHashMap; use rustc_hash::FxHashSet;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, FxIndexSet, ReadRef, ResolvedVc, TraitRef, TryJoinIterExt, Vc}; use turbo_tasks::{FxIndexMap, ReadRef, ResolvedVc, TraitRef, TryJoinIterExt, Vc};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_base64}; use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_base64};
use turbopack_browser::ecmascript::list::content::EcmascriptDevChunkListContent; use turbopack_browser::ecmascript::list::content::EcmascriptDevChunkListContent;
use turbopack_core::version::{PartialUpdate, Update, Version, VersionState, VersionedContent}; use turbopack_core::version::{PartialUpdate, Update, Version, VersionState, VersionedContent};
use turbopack_ecmascript::chunk_list::update::{
ChunkListUpdate, ChunkUpdate, HmrUpdateInstruction,
};
use turbopack_nodejs::ecmascript::node::entry::chunk_list_content::EcmascriptBuildNodeChunkListContent; use turbopack_nodejs::ecmascript::node::entry::chunk_list_content::EcmascriptBuildNodeChunkListContent;
use crate::versioned_content_map::VersionedContentMap; use crate::versioned_content_map::VersionedContentMap;
...@@ -94,69 +97,64 @@ impl AggregateHmrVersion { ...@@ -94,69 +97,64 @@ impl AggregateHmrVersion {
} }
/// Aggregates per-entry HMR instructions into a single combined `ChunkListUpdate`. /// Aggregates per-entry HMR instructions into a single combined `ChunkListUpdate`.
///
/// Each per-entry instruction is deserialized into the strongly-typed
/// [`HmrUpdateInstruction`] rather than being inspected as an untyped
/// `serde_json::Value`, so the aggregation logic is compiler-checked and shares
/// the exact wire-format definition produced by the ecmascript chunking
/// contexts.
#[derive(Default)] #[derive(Default)]
pub struct ChunkListUpdateBuilder { pub struct ChunkListUpdateBuilder {
chunks: FxHashMap<String, serde_json::Value>, chunks: FxIndexMap<RcStr, ChunkUpdate>,
merged: FxIndexSet<serde_json::Value>, merged: Vec<HmrUpdateInstruction>,
/// Tracks the serialized form of instructions already pushed into `merged`
/// so identical merged updates are only sent once (preserving the
/// deduplication the previous `FxIndexSet<Value>` implementation provided
/// while keeping insertion order).
seen_merged: FxHashSet<String>,
} }
impl ChunkListUpdateBuilder { impl ChunkListUpdateBuilder {
pub fn add_instruction(&mut self, instruction: &serde_json::Value) { pub fn add_instruction(&mut self, instruction: &serde_json::Value) -> Result<()> {
let Some(obj) = instruction.as_object() else { match serde_json::from_value::<HmrUpdateInstruction>(instruction.clone())? {
return; HmrUpdateInstruction::ChunkListUpdate(ChunkListUpdate { chunks, merged }) => {
}; self.chunks.extend(chunks);
match obj.get("type").and_then(|v| v.as_str()) {
Some("ChunkListUpdate") => {
if let Some(chunks) = obj.get("chunks").and_then(|v| v.as_object()) {
for (k, v) in chunks {
self.chunks.insert(k.clone(), v.clone());
}
}
if let Some(merged) = obj.get("merged").and_then(|v| v.as_array()) {
for update in merged { for update in merged {
self.push_merged(update); self.push_merged(update)?;
} }
} }
merged @ HmrUpdateInstruction::EcmascriptMergedUpdate(_) => {
self.push_merged(merged)?;
} }
Some("EcmascriptMergedUpdate") => {
self.push_merged(instruction);
}
// Unknown instruction shapes are ignored; the caller already
// escalates `Total`/`Missing` updates to a full restart.
_ => {}
} }
Ok(())
} }
fn push_merged(&mut self, update: &serde_json::Value) { fn push_merged(&mut self, update: HmrUpdateInstruction) -> Result<()> {
self.merged.insert(update.clone()); let key = serde_json::to_string(&update)?;
if self.seen_merged.insert(key) {
self.merged.push(update);
}
Ok(())
} }
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.chunks.is_empty() && self.merged.is_empty() self.chunks.is_empty() && self.merged.is_empty()
} }
pub fn build(self, to: TraitRef<Box<dyn Version>>) -> Update { /// Assembles the aggregated, typed [`HmrUpdateInstruction::ChunkListUpdate`].
let mut instruction = serde_json::Map::new(); fn into_instruction(self) -> HmrUpdateInstruction {
instruction.insert( HmrUpdateInstruction::ChunkListUpdate(ChunkListUpdate {
"type".to_string(), chunks: self.chunks,
serde_json::Value::String("ChunkListUpdate".to_string()), merged: self.merged,
); })
if !self.chunks.is_empty() {
instruction.insert(
"chunks".to_string(),
serde_json::Value::Object(self.chunks.into_iter().collect()),
);
}
if !self.merged.is_empty() {
instruction.insert(
"merged".to_string(),
serde_json::Value::Array(self.merged.into_iter().collect()),
);
} }
Update::Partial(PartialUpdate {
pub fn build(self, to: TraitRef<Box<dyn Version>>) -> Result<Update> {
Ok(Update::Partial(PartialUpdate {
to, to,
instruction: Arc::new(serde_json::Value::Object(instruction)), instruction: Arc::new(serde_json::to_value(&self.into_instruction())?),
}) }))
} }
} }
...@@ -215,3 +213,58 @@ pub async fn diff_chunks_against( ...@@ -215,3 +213,58 @@ pub async fn diff_chunks_against(
has_new_chunks, has_new_chunks,
}) })
} }
#[cfg(test)]
mod tests {
use super::ChunkListUpdateBuilder;
/// The builder folds a `ChunkListUpdate` and a standalone
/// `EcmascriptMergedUpdate` into a single typed `ChunkListUpdate`, matching
/// the previous hand-built `serde_json::Value` output.
#[test]
fn aggregates_typed_instructions() {
let mut builder = ChunkListUpdateBuilder::default();
let chunk_list = serde_json::json!({
"type": "ChunkListUpdate",
"chunks": { "server/a.js": { "type": "total" } },
"merged": [{
"type": "EcmascriptMergedUpdate",
"chunks": { "server/a.js": { "type": "partial", "added": ["1"] } }
}]
});
let standalone_merged = serde_json::json!({
"type": "EcmascriptMergedUpdate",
"chunks": { "server/b.js": { "type": "added", "modules": ["2"] } }
});
builder.add_instruction(&chunk_list).unwrap();
builder.add_instruction(&standalone_merged).unwrap();
assert!(!builder.is_empty());
let instruction = serde_json::to_string(&builder.into_instruction()).unwrap();
assert_eq!(
instruction,
r#"{"type":"ChunkListUpdate","chunks":{"server/a.js":{"type":"total"}},"merged":[{"type":"EcmascriptMergedUpdate","chunks":{"server/a.js":{"type":"partial","added":["1"]}}},{"type":"EcmascriptMergedUpdate","chunks":{"server/b.js":{"type":"added","modules":["2"]}}}]}"#
);
}
/// Identical merged updates are only emitted once (dedup preserved from the
/// previous `FxIndexSet<Value>` implementation).
#[test]
fn deduplicates_identical_merged_updates() {
let mut builder = ChunkListUpdateBuilder::default();
let merged = serde_json::json!({
"type": "EcmascriptMergedUpdate",
"chunks": { "server/a.js": { "type": "added", "modules": ["1"] } }
});
builder.add_instruction(&merged).unwrap();
builder.add_instruction(&merged).unwrap();
let instruction = serde_json::to_string(&builder.into_instruction()).unwrap();
assert_eq!(
instruction,
r#"{"type":"ChunkListUpdate","merged":[{"type":"EcmascriptMergedUpdate","chunks":{"server/a.js":{"type":"added","modules":["1"]}}}]}"#
);
}
}
...@@ -2661,7 +2661,7 @@ impl Project { ...@@ -2661,7 +2661,7 @@ impl Project {
// the *next* change produces a real diff; returning `Total` instead would // the *next* change produces a real diff; returning `Total` instead would
// force a needless full re-evaluation. // force a needless full re-evaluation.
if chunk_updates.is_empty() && !has_new_chunks { if chunk_updates.is_empty() && !has_new_chunks {
return Ok(ChunkListUpdateBuilder::default().build(to_ref).cell()); return Ok(ChunkListUpdateBuilder::default().build(to_ref)?.cell());
} }
let mut builder = ChunkListUpdateBuilder::default(); let mut builder = ChunkListUpdateBuilder::default();
...@@ -2672,7 +2672,7 @@ impl Project { ...@@ -2672,7 +2672,7 @@ impl Project {
return Ok(Update::Total(TotalUpdate { to: to_ref }).cell()); return Ok(Update::Total(TotalUpdate { to: to_ref }).cell());
} }
Update::Partial(PartialUpdate { instruction, .. }) => { Update::Partial(PartialUpdate { instruction, .. }) => {
builder.add_instruction(instruction); builder.add_instruction(instruction)?;
} }
} }
} }
...@@ -2681,7 +2681,7 @@ impl Project { ...@@ -2681,7 +2681,7 @@ impl Project {
return Ok(Update::None.cell()); return Ok(Update::None.cell());
} }
Ok(builder.build(to_ref).cell()) Ok(builder.build(to_ref)?.cell())
} }
/// Gets a list of all HMR chunk names that can be subscribed to for the /// Gets a list of all HMR chunk names that can be subscribed to for the
......
...@@ -95,6 +95,8 @@ ...@@ -95,6 +95,8 @@
"patch-next": "tsx scripts/patch-next.ts", "patch-next": "tsx scripts/patch-next.ts",
"unpack-next": "tsx scripts/unpack-next.ts", "unpack-next": "tsx scripts/unpack-next.ts",
"swc-build-native": "tsx scripts/build-native.ts", "swc-build-native": "tsx scripts/build-native.ts",
"swc-generate-hmr-types": "tsx scripts/generate-hmr-types.ts",
"swc-generate-hmr-types-check": "tsx scripts/generate-hmr-types.ts --check",
"swc-build-wasm": "tsx scripts/build-wasm.cjs", "swc-build-wasm": "tsx scripts/build-wasm.cjs",
"build-turbopack-cli": "cargo build -p turbopack-cli --release", "build-turbopack-cli": "cargo build -p turbopack-cli --release",
"sweep": "tsx scripts/sweep.cjs", "sweep": "tsx scripts/sweep.cjs",
......
// DO NOT MANUALLY EDIT THIS FILE
//
// These types are generated from the Rust source of truth
// (`turbopack/crates/turbopack-ecmascript/src/chunk_list`) via `ts-rs`.
// Regenerate with `pnpm swc-generate-hmr-types` from the repo root.
export type EcmascriptModuleEntry = {
code: string
url: string
map: string | null
}
export type EcmascriptMergedChunkAdded = { modules?: Array<string> }
export type EcmascriptMergedChunkDeleted = { modules?: Array<string> }
export type EcmascriptMergedChunkPartial = {
added?: Array<string>
deleted?: Array<string>
}
export type EcmascriptMergedChunkUpdate =
| ({ type: 'added' } & EcmascriptMergedChunkAdded)
| ({ type: 'deleted' } & EcmascriptMergedChunkDeleted)
| ({ type: 'partial' } & EcmascriptMergedChunkPartial)
export type EcmascriptMergedUpdate = {
/**
* A map from module id to its latest module entry (code + source map url).
*/
entries?: { [key in string]: EcmascriptModuleEntry }
/**
* A map from chunk path to the update for that chunk.
*/
chunks?: { [key in string]: EcmascriptMergedChunkUpdate }
}
export type ChunkUpdate =
| { type: 'total' }
| { type: 'partial'; instruction: HmrUpdateInstruction }
| { type: 'added' }
| { type: 'deleted' }
export type ChunkListUpdate = {
/**
* A map from chunk path to a corresponding update of that chunk.
*/
chunks?: { [key in string]: ChunkUpdate }
/**
* List of merged updates since the last version.
*/
merged?: Array<HmrUpdateInstruction>
}
export type HmrUpdateInstruction =
| ({ type: 'ChunkListUpdate' } & ChunkListUpdate)
| ({ type: 'EcmascriptMergedUpdate' } & EcmascriptMergedUpdate)
import type { NextConfigComplete } from '../../server/config-shared' import type { NextConfigComplete } from '../../server/config-shared'
import type { __ApiPreviewProps } from '../../server/api-utils' import type { __ApiPreviewProps } from '../../server/api-utils'
import type { HmrUpdateInstruction } from './generated-hmr-types'
import type { import type {
ExternalObject, ExternalObject,
RefCell, RefCell,
...@@ -212,51 +213,16 @@ interface IssuesUpdate extends BaseUpdate { ...@@ -212,51 +213,16 @@ interface IssuesUpdate extends BaseUpdate {
type: 'issues' type: 'issues'
} }
interface EcmascriptMergedUpdate {
type: 'EcmascriptMergedUpdate'
chunks: { [moduleName: string]: { type: 'partial' } }
entries: { [moduleName: string]: { code: string; map: string; url: string } }
}
interface PartialUpdate extends BaseUpdate { interface PartialUpdate extends BaseUpdate {
type: 'partial' type: 'partial'
instruction: { instruction: HmrUpdateInstruction
type: 'ChunkListUpdate'
merged: EcmascriptMergedUpdate[] | undefined
}
} }
export type Update = IssuesUpdate | PartialUpdate export type Update = IssuesUpdate | PartialUpdate
/**
* IMPORTANT: This type is duplicated in:
* turbopack/crates/turbopack-ecmascript-runtime/js/src/nodejs/hmr-types.d.ts
*
* The runtime file cannot import from this ES module without triggering module semantics,
* so we maintain a copy there. Please keep both definitions in sync.
*/
export interface NodeJsEcmascriptMergedUpdate {
type: 'EcmascriptMergedUpdate'
entries?: Record<
string,
{ code: string; url: string; map?: string | undefined }
>
chunks?: Record<
string,
| { type: 'added' | 'deleted'; modules?: string[] }
| { type: 'partial'; added?: string[]; deleted?: string[] }
>
}
export interface NodeJsChunkListUpdate {
type: 'ChunkListUpdate'
merged?: NodeJsEcmascriptMergedUpdate[]
chunks?: Record<string, { type: 'added' | 'deleted' | 'total' | 'partial' }>
}
export interface NodeJsPartialHmrUpdate extends BaseUpdate { export interface NodeJsPartialHmrUpdate extends BaseUpdate {
type: 'partial' type: 'partial'
instruction: NodeJsEcmascriptMergedUpdate | NodeJsChunkListUpdate instruction: HmrUpdateInstruction
} }
export interface NodeJsRestartHmrUpdate { export interface NodeJsRestartHmrUpdate {
......
...@@ -135,7 +135,11 @@ function extractModulesFromTurbopackMessage( ...@@ -135,7 +135,11 @@ function extractModulesFromTurbopackMessage(
} }
for (const mergedUpdate of update.instruction.merged) { for (const mergedUpdate of update.instruction.merged) {
if (!mergedUpdate.entries) continue if (
mergedUpdate.type !== 'EcmascriptMergedUpdate' ||
!mergedUpdate.entries
)
continue
for (const name of Object.keys(mergedUpdate.entries)) { for (const name of Object.keys(mergedUpdate.entries)) {
const res = /(.*)\s+[([].*/.exec(name) const res = /(.*)\s+[([].*/.exec(name)
if (res === null) { if (res === null) {
......
...@@ -5,6 +5,7 @@ import path from 'node:path' ...@@ -5,6 +5,7 @@ import path from 'node:path'
import url from 'node:url' import url from 'node:url'
import execa from 'execa' import execa from 'execa'
import { NEXT_DIR, logCommand } from './pack-util' import { NEXT_DIR, logCommand } from './pack-util'
import writeGeneratedHmrTypes from './generate-hmr-types'
const nextSwcDir = path.join(NEXT_DIR, 'packages/next-swc') const nextSwcDir = path.join(NEXT_DIR, 'packages/next-swc')
...@@ -26,6 +27,9 @@ export default async function buildNative( ...@@ -26,6 +27,9 @@ export default async function buildNative(
}) })
await writeTypes() await writeTypes()
// Keep the ts-rs-generated HMR wire-protocol types in sync with their Rust
// source of truth alongside the napi-generated bindings.
await writeGeneratedHmrTypes()
} }
// Check if this file is being run directly // Check if this file is being run directly
......
#!/usr/bin/env node
// Generates the TypeScript definitions for the Turbopack HMR update-instruction
// wire protocol from their Rust source of truth (the `chunk_list` instruction
// structs in `turbopack-ecmascript`), via `ts-rs`.
//
// The Rust `cargo run --example export_hmr_types` prints the type declarations;
// this script wraps them with a "do not edit" header, formats them with
// prettier, and vendors the result into `packages/next`.
//
// Usage:
// tsx scripts/generate-hmr-types.ts # (re)generate the file
// tsx scripts/generate-hmr-types.ts --check # fail if the file is stale
//
// The `--check` mode is intended for CI: it regenerates the types in memory and
// exits non-zero if they differ from what is checked in.
import { promises as fs } from 'node:fs'
import path from 'node:path'
import url from 'node:url'
import execa from 'execa'
import { NEXT_DIR, logCommand } from './pack-util'
const GENERATED_FILE = path.join(
NEXT_DIR,
'packages/next/src/build/swc/generated-hmr-types.ts'
)
const HEADER =
'// DO NOT MANUALLY EDIT THIS FILE\n' +
'//\n' +
'// These types are generated from the Rust source of truth\n' +
'// (`turbopack/crates/turbopack-ecmascript/src/chunk_list`) via `ts-rs`.\n' +
'// Regenerate with `pnpm swc-generate-hmr-types` from the repo root.\n\n'
async function generate(): Promise<string> {
const command = [
'cargo',
'run',
'--quiet',
'-p',
'turbopack-ecmascript',
'--example',
'export_hmr_types',
]
logCommand('Generate HMR types', command)
const { stdout } = await execa(command[0], command.slice(1), {
cwd: NEXT_DIR,
// Only capture stdout (the type declarations); cargo build progress goes to
// stderr.
stderr: 'inherit',
})
const prettifyCommand = ['prettier', '--stdin-filepath', GENERATED_FILE]
const { stdout: formatted } = await execa(
prettifyCommand[0],
prettifyCommand.slice(1),
{
cwd: NEXT_DIR,
input: HEADER + stdout + '\n',
preferLocal: true,
}
)
return formatted.endsWith('\n') ? formatted : formatted + '\n'
}
async function main(): Promise<void> {
const check = process.argv.includes('--check')
const generated = await generate()
if (check) {
let existing: string | null = null
try {
existing = await fs.readFile(GENERATED_FILE, 'utf8')
} catch {}
if (existing !== generated) {
console.error(
`\n${path.relative(NEXT_DIR, GENERATED_FILE)} is out of date.\n` +
'Run `pnpm swc-generate-hmr-types` and commit the result.\n'
)
process.exit(1)
}
logCommand('Check HMR types', 'up to date')
return
}
await writeGeneratedHmrTypes(generated)
}
/** Regenerates and writes the vendored HMR type definitions. */
export default async function writeGeneratedHmrTypes(
generated?: string
): Promise<void> {
generated ??= await generate()
logCommand('Write generated HMR types', GENERATED_FILE)
await fs.writeFile(GENERATED_FILE, generated)
}
if (import.meta.url === url.pathToFileURL(process.argv[1]).toString()) {
main().catch((err) => {
console.error(err)
process.exit(1)
})
}
...@@ -452,7 +452,7 @@ impl<Context> Decode<Context> for Rope { ...@@ -452,7 +452,7 @@ impl<Context> Decode<Context> for Rope {
impl_borrow_decode!(Rope); impl_borrow_decode!(Rope);
pub mod ser_as_string { pub mod ser_as_string {
use serde::{Serializer, ser::Error}; use serde::{Deserialize, Deserializer, Serializer, ser::Error};
use super::Rope; use super::Rope;
...@@ -461,10 +461,16 @@ pub mod ser_as_string { ...@@ -461,10 +461,16 @@ pub mod ser_as_string {
let s = rope.to_str().map_err(Error::custom)?; let s = rope.to_str().map_err(Error::custom)?;
serializer.serialize_str(&s) serializer.serialize_str(&s)
} }
/// Deserializes a Rope from a string (the inverse of [`serialize`]).
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Rope, D::Error> {
let s = String::deserialize(deserializer)?;
Ok(Rope::from(s))
}
} }
pub mod ser_option_as_string { pub mod ser_option_as_string {
use serde::{Serializer, ser::Error}; use serde::{Deserialize, Deserializer, Serializer, ser::Error};
use super::Rope; use super::Rope;
...@@ -477,6 +483,15 @@ pub mod ser_option_as_string { ...@@ -477,6 +483,15 @@ pub mod ser_option_as_string {
serializer.serialize_none() serializer.serialize_none()
} }
} }
/// Deserializes an optional Rope from an optional string (the inverse of
/// [`serialize`]).
pub fn deserialize<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<Option<Rope>, D::Error> {
let s = Option::<String>::deserialize(deserializer)?;
Ok(s.map(Rope::from))
}
} }
impl PartialEq for Rope { impl PartialEq for Rope {
......
...@@ -10,7 +10,10 @@ type NodeJsHmrPayload = { ...@@ -10,7 +10,10 @@ type NodeJsHmrPayload = {
} }
issues: Issue[] issues: Issue[]
type: 'partial' type: 'partial'
instruction: NodeJsEcmascriptMergedUpdate | NodeJsChunkListUpdate // These are the shared, ambient HMR wire-protocol types from
// `dev-protocol.d.ts` (generated on the Rust side from
// `turbopack-ecmascript`'s `chunk_list` instruction structs).
instruction: ChunkListUpdate | EcmascriptMergedUpdate
} }
/** /**
...@@ -114,7 +117,7 @@ function handleNodejsUpdate( ...@@ -114,7 +117,7 @@ function handleNodejsUpdate(
} }
function applyEcmascriptMergedUpdate( function applyEcmascriptMergedUpdate(
instruction: NodeJsEcmascriptMergedUpdate, instruction: EcmascriptMergedUpdate,
moduleFactories: ModuleFactories, moduleFactories: ModuleFactories,
devModuleCache: ModuleCache<HotModule> devModuleCache: ModuleCache<HotModule>
): void { ): void {
......
/**
* Type definitions for Node.js HMR updates.
*
* IMPORTANT: This is a duplicate of the types in packages/next/src/build/swc/types.ts
* to avoid importing from an ES module, which makes TypeScript treat the file as an ES module.
*
* Keep NodeJsPartialHmrUpdate in sync with the copy in packages/next/src/build/swc/types.ts.
*/
interface NodeJsEcmascriptMergedUpdate {
type: 'EcmascriptMergedUpdate'
entries?: Record<
string,
{ code: string; url: string; map?: string | undefined }
>
chunks?: Record<
string,
| { type: 'added' | 'deleted'; modules?: string[] }
| { type: 'partial'; added?: string[]; deleted?: string[] }
>
}
interface NodeJsChunkListUpdate {
type: 'ChunkListUpdate'
merged?: NodeJsEcmascriptMergedUpdate[]
chunks?: Record<string, { type: 'added' | 'deleted' | 'total' | 'partial' }>
}
interface NodeJsPartialHmrUpdate {
type: 'partial'
instruction: NodeJsEcmascriptMergedUpdate | NodeJsChunkListUpdate
}
interface NodeJsRestartHmrUpdate {
type: 'restart'
}
...@@ -53,6 +53,7 @@ strsim = { workspace = true } ...@@ -53,6 +53,7 @@ strsim = { workspace = true }
thread_local = { workspace = true } thread_local = { workspace = true }
tokio = { workspace = true } tokio = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
ts-rs = { workspace = true }
turbo-bincode = { workspace = true } turbo-bincode = { workspace = true }
turbo-esregex = { workspace = true } turbo-esregex = { workspace = true }
turbo-frozenmap = { workspace = true } turbo-frozenmap = { workspace = true }
......
//! Prints the TypeScript definitions for the HMR update-instruction wire
//! protocol to stdout, generated from the Rust source of truth (the
//! `chunk_list` instruction structs) via `ts-rs`.
//!
//! This keeps the generator itself free of any consumer-specific paths: it only
//! emits the type declarations. The Next.js build tooling
//! (`scripts/generate-hmr-types.ts`) captures this output, adds a
//! "do not edit" header, formats it, and vendors it into `packages/next`.
//!
//! Run with:
//!
//! ```sh
//! cargo run -p turbopack-ecmascript --example export_hmr_types
//! ```
use ts_rs::{Config, TS};
use turbopack_ecmascript::chunk_list::{
merged_update::{
EcmascriptMergedChunkAdded, EcmascriptMergedChunkDeleted, EcmascriptMergedChunkPartial,
EcmascriptMergedChunkUpdate, EcmascriptMergedUpdate, EcmascriptModuleEntry,
},
update::{ChunkListUpdate, ChunkUpdate, HmrUpdateInstruction},
};
fn main() {
let cfg = Config::default();
// Emitted in dependency order (leaves first) so the resulting file needs no
// forward declarations.
let decls = [
EcmascriptModuleEntry::decl(&cfg),
EcmascriptMergedChunkAdded::decl(&cfg),
EcmascriptMergedChunkDeleted::decl(&cfg),
EcmascriptMergedChunkPartial::decl(&cfg),
EcmascriptMergedChunkUpdate::decl(&cfg),
EcmascriptMergedUpdate::decl(&cfg),
ChunkUpdate::decl(&cfg),
ChunkListUpdate::decl(&cfg),
HmrUpdateInstruction::decl(&cfg),
];
for decl in decls {
println!("export {decl}");
}
}
...@@ -6,40 +6,65 @@ ...@@ -6,40 +6,65 @@
//! definitions live here to keep the two runtimes from drifting apart on the //! definitions live here to keep the two runtimes from drifting apart on the
//! wire format. //! wire format.
//! //!
//! These are native, strongly-typed Rust structs: they derive both `Serialize`
//! (producing the wire format) and `Deserialize` (so consumers such as the
//! Next.js HMR aggregator can round-trip an instruction through typed structs
//! instead of poking at an untyped `serde_json::Value`). They are also the
//! source of truth the TypeScript definitions are generated from.
//!
//! The turbo-tasks value types (`*MergedChunkContent`, `*MergedChunkVersion`, //! The turbo-tasks value types (`*MergedChunkContent`, `*MergedChunkVersion`,
//! `*ChunkContentMerger`) cannot be generic and therefore remain per-runtime, //! `*ChunkContentMerger`) cannot be generic and therefore remain per-runtime,
//! but they all build and serialize these shared structs. //! but they all build and serialize these shared structs.
//!
//! ## Module ids on the wire
//!
//! Module ids ([`ModuleId`]) are emitted as strings, both as map keys and as
//! array elements (dev-mode HMR — the only place these instructions are
//! produced — always uses string module ids). We therefore model them here as
//! [`RcStr`] rather than [`ModuleId`]: this keeps the wire shape (`string` keys
//! and `string[]` arrays) identical to what the JS client already expects,
//! avoids depending on `ModuleId` implementing `Deserialize`, and matches the
//! generated TypeScript types.
use anyhow::Result; use anyhow::Result;
use serde::Serialize; use serde::{Deserialize, Serialize};
use ts_rs::TS;
use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, FxIndexSet, Vc}; use turbo_tasks::{FxIndexMap, FxIndexSet, Vc};
use turbo_tasks_fs::rope::Rope; use turbo_tasks_fs::rope::Rope;
use turbopack_core::{chunk::ModuleId, code_builder::Code, source_map::GenerateSourceMap}; use turbopack_core::{chunk::ModuleId, code_builder::Code, source_map::GenerateSourceMap};
/// Converts a [`ModuleId`] into its string wire representation, matching how it
/// is serialized as a JSON map key / array element.
pub(crate) fn module_id_key(id: &ModuleId) -> RcStr {
match id {
ModuleId::String(s) => s.clone(),
ModuleId::Number(n) => n.to_string().into(),
}
}
/// A merged update covering one or more ecmascript chunks that share a merger. /// A merged update covering one or more ecmascript chunks that share a merger.
#[derive(Serialize, Default)] #[derive(Serialize, Deserialize, Default, TS)]
#[serde( #[serde(rename_all = "camelCase")]
tag = "type", pub struct EcmascriptMergedUpdate {
rename = "EcmascriptMergedUpdate",
rename_all = "camelCase"
)]
pub struct EcmascriptMergedUpdate<'a> {
/// A map from module id to its latest module entry (code + source map url). /// A map from module id to its latest module entry (code + source map url).
#[serde(skip_serializing_if = "FxIndexMap::is_empty")] #[serde(default, skip_serializing_if = "FxIndexMap::is_empty")]
pub entries: FxIndexMap<ModuleId, EcmascriptModuleEntry>, #[ts(as = "std::collections::BTreeMap<String, EcmascriptModuleEntry>")]
pub entries: FxIndexMap<RcStr, EcmascriptModuleEntry>,
/// A map from chunk path to the update for that chunk. /// A map from chunk path to the update for that chunk.
#[serde(skip_serializing_if = "FxIndexMap::is_empty")] #[serde(default, skip_serializing_if = "FxIndexMap::is_empty")]
pub chunks: FxIndexMap<&'a str, EcmascriptMergedChunkUpdate>, #[ts(as = "std::collections::BTreeMap<String, EcmascriptMergedChunkUpdate>")]
pub chunks: FxIndexMap<RcStr, EcmascriptMergedChunkUpdate>,
} }
impl EcmascriptMergedUpdate<'_> { impl EcmascriptMergedUpdate {
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.entries.is_empty() && self.chunks.is_empty() self.entries.is_empty() && self.chunks.is_empty()
} }
} }
/// Per-chunk portion of an [`EcmascriptMergedUpdate`]. /// Per-chunk portion of an [`EcmascriptMergedUpdate`].
#[derive(Serialize)] #[derive(Serialize, Deserialize, TS)]
#[serde(tag = "type", rename_all = "camelCase")] #[serde(tag = "type", rename_all = "camelCase")]
pub enum EcmascriptMergedChunkUpdate { pub enum EcmascriptMergedChunkUpdate {
Added(EcmascriptMergedChunkAdded), Added(EcmascriptMergedChunkAdded),
...@@ -48,42 +73,48 @@ pub enum EcmascriptMergedChunkUpdate { ...@@ -48,42 +73,48 @@ pub enum EcmascriptMergedChunkUpdate {
} }
/// A chunk that was newly added in this version. /// A chunk that was newly added in this version.
#[derive(Serialize, Default)] #[derive(Serialize, Deserialize, Default, TS)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct EcmascriptMergedChunkAdded { pub struct EcmascriptMergedChunkAdded {
#[serde(skip_serializing_if = "FxIndexSet::is_empty")] #[serde(default, skip_serializing_if = "FxIndexSet::is_empty")]
pub modules: FxIndexSet<ModuleId>, #[ts(as = "Vec<String>")]
pub modules: FxIndexSet<RcStr>,
} }
/// A chunk that was removed in this version. /// A chunk that was removed in this version.
#[derive(Serialize, Default)] #[derive(Serialize, Deserialize, Default, TS)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct EcmascriptMergedChunkDeleted { pub struct EcmascriptMergedChunkDeleted {
// Technically, this is redundant, since the client will already know all // Technically, this is redundant, since the client will already know all
// modules in the chunk from the previous version. However, it's useful for // modules in the chunk from the previous version. However, it's useful for
// merging updates without access to an initial state. // merging updates without access to an initial state.
#[serde(skip_serializing_if = "FxIndexSet::is_empty")] #[serde(default, skip_serializing_if = "FxIndexSet::is_empty")]
pub modules: FxIndexSet<ModuleId>, #[ts(as = "Vec<String>")]
pub modules: FxIndexSet<RcStr>,
} }
/// A chunk that was present in both versions and whose module membership /// A chunk that was present in both versions and whose module membership
/// changed. /// changed.
#[derive(Serialize, Default)] #[derive(Serialize, Deserialize, Default, TS)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct EcmascriptMergedChunkPartial { pub struct EcmascriptMergedChunkPartial {
#[serde(skip_serializing_if = "FxIndexSet::is_empty")] #[serde(default, skip_serializing_if = "FxIndexSet::is_empty")]
pub added: FxIndexSet<ModuleId>, #[ts(as = "Vec<String>")]
#[serde(skip_serializing_if = "FxIndexSet::is_empty")] pub added: FxIndexSet<RcStr>,
pub deleted: FxIndexSet<ModuleId>, #[serde(default, skip_serializing_if = "FxIndexSet::is_empty")]
#[ts(as = "Vec<String>")]
pub deleted: FxIndexSet<RcStr>,
} }
/// The code (and source map) for a single module in a merged update. /// The code (and source map) for a single module in a merged update.
#[derive(Serialize)] #[derive(Serialize, Deserialize, TS)]
pub struct EcmascriptModuleEntry { pub struct EcmascriptModuleEntry {
#[serde(with = "turbo_tasks_fs::rope::ser_as_string")] #[serde(with = "turbo_tasks_fs::rope::ser_as_string")]
#[ts(as = "String")]
pub code: Rope, pub code: Rope,
pub url: String, pub url: String,
#[serde(with = "turbo_tasks_fs::rope::ser_option_as_string")] #[serde(with = "turbo_tasks_fs::rope::ser_option_as_string")]
#[ts(as = "Option<String>")]
pub map: Option<Rope>, pub map: Option<Rope>,
} }
......
use std::sync::Arc; use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use serde::Serialize; use serde::{Deserialize, Serialize};
use ts_rs::TS;
use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, ResolvedVc, TraitRef, Vc}; use turbo_tasks::{FxIndexMap, ResolvedVc, TraitRef, Vc};
use turbopack_core::version::{ use turbopack_core::version::{
MergeableVersionedContent, PartialUpdate, TotalUpdate, Update, Version, VersionedContent, MergeableVersionedContent, PartialUpdate, TotalUpdate, Update, Version, VersionedContent,
VersionedContentMerger, VersionedContentMerger,
}; };
use super::version::ChunkListVersion; use super::{merged_update::EcmascriptMergedUpdate, version::ChunkListVersion};
/// Update of a chunk list from one version to another. /// A single HMR update instruction as sent to the JS client.
#[derive(Serialize)] ///
/// This is the native, strongly-typed representation of the payload stored in
/// [`turbopack_core::version::PartialUpdate::instruction`]. The turbo-tasks
/// `Update`/`PartialUpdate` boundary is generic over arbitrary versioned
/// content and therefore keeps carrying an opaque `serde_json::Value`, but
/// everywhere the ecmascript HMR path produces or consumes an instruction it
/// goes through this enum, so the code is compiler-checked instead of poking at
/// untyped JSON.
///
/// The `type` tag (`"ChunkListUpdate"` / `"EcmascriptMergedUpdate"`) is owned by
/// this enum, which is why the inner structs no longer carry their own serde
/// tag. This keeps the wire format identical while giving a single place that
/// enumerates the known instruction shapes.
#[derive(Serialize, Deserialize, TS)]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum HmrUpdateInstruction {
ChunkListUpdate(ChunkListUpdate),
EcmascriptMergedUpdate(EcmascriptMergedUpdate),
}
/// Update of a chunk list from one version to another.
#[derive(Serialize, Deserialize, Default, TS)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct ChunkListUpdate<'a> { pub struct ChunkListUpdate {
/// A map from chunk path to a corresponding update of that chunk. /// A map from chunk path to a corresponding update of that chunk.
#[serde(skip_serializing_if = "FxIndexMap::is_empty")] #[serde(default, skip_serializing_if = "FxIndexMap::is_empty")]
chunks: FxIndexMap<&'a str, ChunkUpdate>, #[ts(as = "std::collections::BTreeMap<String, ChunkUpdate>")]
pub chunks: FxIndexMap<RcStr, ChunkUpdate>,
/// List of merged updates since the last version. /// List of merged updates since the last version.
#[serde(skip_serializing_if = "Vec::is_empty")] #[serde(default, skip_serializing_if = "Vec::is_empty")]
merged: Vec<Arc<serde_json::Value>>, pub merged: Vec<HmrUpdateInstruction>,
} }
/// Update of a chunk from one version to another. /// Update of a chunk from one version to another.
#[derive(Serialize)] #[derive(Serialize, Deserialize, TS)]
#[serde(tag = "type")] #[serde(tag = "type", rename_all = "camelCase")]
#[serde(rename_all = "camelCase")] pub enum ChunkUpdate {
enum ChunkUpdate {
/// The chunk was updated and must be reloaded. /// The chunk was updated and must be reloaded.
Total, Total,
/// The chunk was updated and can be merged with the previous version. /// The chunk was updated and can be merged with the previous version.
Partial { instruction: Arc<serde_json::Value> }, Partial { instruction: HmrUpdateInstruction },
/// The chunk was added. /// The chunk was added.
Added, Added,
/// The chunk was deleted. /// The chunk was deleted.
Deleted, Deleted,
} }
impl ChunkListUpdate<'_> { impl ChunkListUpdate {
/// Returns `true` if this update is empty. /// Returns `true` if this update is empty.
fn is_empty(&self) -> bool { fn is_empty(&self) -> bool {
let ChunkListUpdate { chunks, merged } = self; let ChunkListUpdate { chunks, merged } = self;
...@@ -95,7 +117,7 @@ pub async fn update_chunk_list( ...@@ -95,7 +117,7 @@ pub async fn update_chunk_list(
} }
} }
let mut chunks = FxIndexMap::<_, _>::default(); let mut chunks = FxIndexMap::<RcStr, ChunkUpdate>::default();
for (chunk_path, from_chunk_version) in &from.by_path { for (chunk_path, from_chunk_version) in &from.by_path {
if let Some(chunk_content) = by_path.swap_remove(chunk_path) { if let Some(chunk_content) = by_path.swap_remove(chunk_path) {
...@@ -105,25 +127,27 @@ pub async fn update_chunk_list( ...@@ -105,25 +127,27 @@ pub async fn update_chunk_list(
match &*chunk_update { match &*chunk_update {
Update::Total(_) => { Update::Total(_) => {
chunks.insert(chunk_path.as_ref(), ChunkUpdate::Total); chunks.insert(chunk_path.as_str().into(), ChunkUpdate::Total);
} }
Update::Partial(partial) => { Update::Partial(partial) => {
chunks.insert( chunks.insert(
chunk_path.as_ref(), chunk_path.as_str().into(),
ChunkUpdate::Partial { ChunkUpdate::Partial {
instruction: partial.instruction.clone(), instruction: serde_json::from_value(
partial.instruction.as_ref().clone(),
)?,
}, },
); );
} }
Update::Missing | Update::None => {} Update::Missing | Update::None => {}
} }
} else { } else {
chunks.insert(chunk_path.as_ref(), ChunkUpdate::Deleted); chunks.insert(chunk_path.as_str().into(), ChunkUpdate::Deleted);
} }
} }
for chunk_path in by_path.keys() { for chunk_path in by_path.keys() {
chunks.insert(chunk_path.as_ref(), ChunkUpdate::Added); chunks.insert(chunk_path.as_str().into(), ChunkUpdate::Added);
} }
let mut merged = vec![]; let mut merged = vec![];
...@@ -147,7 +171,9 @@ pub async fn update_chunk_list( ...@@ -147,7 +171,9 @@ pub async fn update_chunk_list(
.cell()); .cell());
} }
Update::Partial(partial) => { Update::Partial(partial) => {
merged.push(partial.instruction.clone()); merged.push(serde_json::from_value(
partial.instruction.as_ref().clone(),
)?);
} }
Update::Missing | Update::None => {} Update::Missing | Update::None => {}
} }
...@@ -162,9 +188,100 @@ pub async fn update_chunk_list( ...@@ -162,9 +188,100 @@ pub async fn update_chunk_list(
to: Vc::upcast::<Box<dyn Version>>(to_version) to: Vc::upcast::<Box<dyn Version>>(to_version)
.into_trait_ref() .into_trait_ref()
.await?, .await?,
instruction: Arc::new(serde_json::to_value(&update)?), instruction: Arc::new(serde_json::to_value(
&HmrUpdateInstruction::ChunkListUpdate(update),
)?),
}) })
}; };
Ok(update.cell()) Ok(update.cell())
} }
#[cfg(test)]
mod tests {
use turbo_rcstr::RcStr;
use turbo_tasks::FxIndexMap;
use turbo_tasks_fs::rope::Rope;
use super::{ChunkListUpdate, ChunkUpdate, HmrUpdateInstruction};
use crate::chunk_list::merged_update::{
EcmascriptMergedChunkAdded, EcmascriptMergedChunkPartial, EcmascriptMergedChunkUpdate,
EcmascriptMergedUpdate, EcmascriptModuleEntry,
};
fn rc(s: &str) -> RcStr {
RcStr::from(s)
}
/// A single-chunk `EcmascriptMergedUpdate` must serialize to exactly the
/// wire shape the JS client expects, and round-trip through the typed
/// representation without changing a byte.
#[test]
fn ecmascript_merged_update_wire_format() {
let mut entries = FxIndexMap::default();
entries.insert(
rc("[project]/foo.js [test]"),
EcmascriptModuleEntry {
code: Rope::from("console.log(1)".to_string()),
url: "chunk.js?id=%5Bproject%5D%2Ffoo.js".to_string(),
map: None,
},
);
let mut chunks = FxIndexMap::default();
let mut partial = EcmascriptMergedChunkPartial::default();
partial.added.insert(rc("[project]/foo.js [test]"));
chunks.insert(
rc("chunk.js"),
EcmascriptMergedChunkUpdate::Partial(partial),
);
let update = HmrUpdateInstruction::EcmascriptMergedUpdate(EcmascriptMergedUpdate {
entries,
chunks,
});
let json = serde_json::to_string(&update).unwrap();
assert_eq!(
json,
r#"{"type":"EcmascriptMergedUpdate","entries":{"[project]/foo.js [test]":{"code":"console.log(1)","url":"chunk.js?id=%5Bproject%5D%2Ffoo.js","map":null}},"chunks":{"chunk.js":{"type":"partial","added":["[project]/foo.js [test]"]}}}"#
);
let roundtrip: HmrUpdateInstruction = serde_json::from_str(&json).unwrap();
assert_eq!(serde_json::to_string(&roundtrip).unwrap(), json);
}
/// A `ChunkListUpdate` nests a merged update inside `merged` (each carrying
/// its own `type` tag) and describes per-chunk updates in `chunks`.
#[test]
fn chunk_list_update_wire_format() {
let mut cl_chunks = FxIndexMap::default();
cl_chunks.insert(rc("server/chunk.js"), ChunkUpdate::Total);
cl_chunks.insert(rc("server/added.js"), ChunkUpdate::Added);
let mut added = EcmascriptMergedChunkAdded::default();
added.modules.insert(rc("42"));
let mut m_chunks = FxIndexMap::default();
m_chunks.insert(
rc("server/new.js"),
EcmascriptMergedChunkUpdate::Added(added),
);
let merged = vec![HmrUpdateInstruction::EcmascriptMergedUpdate(
EcmascriptMergedUpdate {
entries: FxIndexMap::default(),
chunks: m_chunks,
},
)];
let update = HmrUpdateInstruction::ChunkListUpdate(ChunkListUpdate {
chunks: cl_chunks,
merged,
});
let json = serde_json::to_string(&update).unwrap();
assert_eq!(
json,
r#"{"type":"ChunkListUpdate","chunks":{"server/chunk.js":{"type":"total"},"server/added.js":{"type":"added"}},"merged":[{"type":"EcmascriptMergedUpdate","chunks":{"server/new.js":{"type":"added","modules":["42"]}}}]}"#
);
let roundtrip: HmrUpdateInstruction = serde_json::from_str(&json).unwrap();
assert_eq!(serde_json::to_string(&roundtrip).unwrap(), json);
}
}
use std::sync::Arc; use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, ReadRef, ResolvedVc, TryJoinIterExt, Vc}; use turbo_tasks::{FxIndexMap, ReadRef, ResolvedVc, TryJoinIterExt, Vc};
use turbopack_core::{ use turbopack_core::{
chunk::ModuleId, chunk::ModuleId,
...@@ -10,9 +11,13 @@ use turbopack_core::{ ...@@ -10,9 +11,13 @@ use turbopack_core::{
use crate::{ use crate::{
chunk::EcmascriptChunkContentEntries, chunk::EcmascriptChunkContentEntries,
chunk_list::merged_update::{ chunk_list::{
merged_update::{
EcmascriptMergedChunkAdded, EcmascriptMergedChunkDeleted, EcmascriptMergedChunkPartial, EcmascriptMergedChunkAdded, EcmascriptMergedChunkDeleted, EcmascriptMergedChunkPartial,
EcmascriptMergedChunkUpdate, EcmascriptMergedUpdate, EcmascriptModuleEntry, EcmascriptMergedChunkUpdate, EcmascriptMergedUpdate, EcmascriptModuleEntry,
module_id_key,
},
update::HmrUpdateInstruction,
}, },
hmr::{ hmr::{
EcmascriptHmrChunkContent, EcmascriptHmrChunkContent,
...@@ -115,7 +120,7 @@ fn module_hash(versions: &[ReadRef<EcmascriptChunkVersion>], id: &ModuleId) -> O ...@@ -115,7 +120,7 @@ fn module_hash(versions: &[ReadRef<EcmascriptChunkVersion>], id: &ModuleId) -> O
/// Code only has to be shipped once per update: skip any module another chunk in /// Code only has to be shipped once per update: skip any module another chunk in
/// the group already provides at the same hash. /// the group already provides at the same hash.
async fn insert_entry_unless_shipped( async fn insert_entry_unless_shipped(
entries: &mut FxIndexMap<ModuleId, EcmascriptModuleEntry>, entries: &mut FxIndexMap<RcStr, EcmascriptModuleEntry>,
from_versions: &[ReadRef<EcmascriptChunkVersion>], from_versions: &[ReadRef<EcmascriptChunkVersion>],
id: ModuleId, id: ModuleId,
hash: u128, hash: u128,
...@@ -124,7 +129,7 @@ async fn insert_entry_unless_shipped( ...@@ -124,7 +129,7 @@ async fn insert_entry_unless_shipped(
) -> Result<()> { ) -> Result<()> {
if module_hash(from_versions, &id) != Some(hash) { if module_hash(from_versions, &id) != Some(hash) {
let entry = EcmascriptModuleEntry::from_code(&id, code, chunk_path).await?; let entry = EcmascriptModuleEntry::from_code(&id, code, chunk_path).await?;
entries.insert(id, entry); entries.insert(module_id_key(&id), entry);
} }
Ok(()) Ok(())
} }
...@@ -134,7 +139,7 @@ async fn partial_chunk_update( ...@@ -134,7 +139,7 @@ async fn partial_chunk_update(
update: EcmascriptChunkUpdate, update: EcmascriptChunkUpdate,
chunk_path: &str, chunk_path: &str,
from_versions: &[ReadRef<EcmascriptChunkVersion>], from_versions: &[ReadRef<EcmascriptChunkVersion>],
entries: &mut FxIndexMap<ModuleId, EcmascriptModuleEntry>, entries: &mut FxIndexMap<RcStr, EcmascriptModuleEntry>,
) -> Result<EcmascriptMergedChunkUpdate> { ) -> Result<EcmascriptMergedChunkUpdate> {
let EcmascriptChunkUpdate::Partial { let EcmascriptChunkUpdate::Partial {
added, added,
...@@ -148,15 +153,17 @@ async fn partial_chunk_update( ...@@ -148,15 +153,17 @@ async fn partial_chunk_update(
let mut partial = EcmascriptMergedChunkPartial::default(); let mut partial = EcmascriptMergedChunkPartial::default();
for (id, AddedModule { hash, code }) in added { for (id, AddedModule { hash, code }) in added {
partial.added.insert(id.clone()); partial.added.insert(module_id_key(&id));
insert_entry_unless_shipped(entries, from_versions, id, hash, *code, chunk_path).await?; insert_entry_unless_shipped(entries, from_versions, id, hash, *code, chunk_path).await?;
} }
partial.deleted.extend(deleted.into_keys()); partial
.deleted
.extend(deleted.into_keys().map(|id| module_id_key(&id)));
for (id, code) in modified { for (id, code) in modified {
let entry = EcmascriptModuleEntry::from_code(&id, *code, chunk_path).await?; let entry = EcmascriptModuleEntry::from_code(&id, *code, chunk_path).await?;
entries.insert(id, entry); entries.insert(module_id_key(&id), entry);
} }
Ok(EcmascriptMergedChunkUpdate::Partial(partial)) Ok(EcmascriptMergedChunkUpdate::Partial(partial))
...@@ -167,12 +174,12 @@ async fn added_chunk_update( ...@@ -167,12 +174,12 @@ async fn added_chunk_update(
chunk_entries: &ReadRef<EcmascriptChunkContentEntries>, chunk_entries: &ReadRef<EcmascriptChunkContentEntries>,
chunk_path: &str, chunk_path: &str,
from_versions: &[ReadRef<EcmascriptChunkVersion>], from_versions: &[ReadRef<EcmascriptChunkVersion>],
entries: &mut FxIndexMap<ModuleId, EcmascriptModuleEntry>, entries: &mut FxIndexMap<RcStr, EcmascriptModuleEntry>,
) -> Result<EcmascriptMergedChunkUpdate> { ) -> Result<EcmascriptMergedChunkUpdate> {
let mut added = EcmascriptMergedChunkAdded::default(); let mut added = EcmascriptMergedChunkAdded::default();
for (id, entry) in chunk_entries.iter() { for (id, entry) in chunk_entries.iter() {
added.modules.insert(id.clone()); added.modules.insert(module_id_key(id));
insert_entry_unless_shipped( insert_entry_unless_shipped(
entries, entries,
from_versions, from_versions,
...@@ -268,15 +275,15 @@ pub async fn update_ecmascript_merged_chunk( ...@@ -268,15 +275,15 @@ pub async fn update_ecmascript_merged_chunk(
} }
}; };
merged_update.chunks.insert(chunk_path, chunk_update); merged_update.chunks.insert(chunk_path.into(), chunk_update);
} }
for (chunk_path, chunk_version) in from_versions_by_chunk_path { for (chunk_path, chunk_version) in from_versions_by_chunk_path {
let hashes = &chunk_version.entries_hashes; let hashes = &chunk_version.entries_hashes;
merged_update.chunks.insert( merged_update.chunks.insert(
chunk_path, chunk_path.into(),
EcmascriptMergedChunkUpdate::Deleted(EcmascriptMergedChunkDeleted { EcmascriptMergedChunkUpdate::Deleted(EcmascriptMergedChunkDeleted {
modules: hashes.keys().cloned().collect(), modules: hashes.keys().map(module_id_key).collect(),
}), }),
); );
} }
...@@ -288,7 +295,9 @@ pub async fn update_ecmascript_merged_chunk( ...@@ -288,7 +295,9 @@ pub async fn update_ecmascript_merged_chunk(
to: Vc::upcast::<Box<dyn Version>>(to_merged_version) to: Vc::upcast::<Box<dyn Version>>(to_merged_version)
.into_trait_ref() .into_trait_ref()
.await?, .await?,
instruction: Arc::new(serde_json::to_value(&merged_update)?), instruction: Arc::new(serde_json::to_value(
&HmrUpdateInstruction::EcmascriptMergedUpdate(merged_update),
)?),
}) })
}) })
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment