Commit 24576ee4 authored by Will Binns-Smith's avatar Will Binns-Smith

wip

parent eeb629f2
...@@ -10147,7 +10147,11 @@ version = "0.1.0" ...@@ -10147,7 +10147,11 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bincode 2.0.1", "bincode 2.0.1",
"either",
"indoc", "indoc",
"serde",
"serde_json",
"serde_qs",
"tracing", "tracing",
"turbo-bincode", "turbo-bincode",
"turbo-rcstr", "turbo-rcstr",
......
...@@ -72,14 +72,20 @@ declare global { ...@@ -72,14 +72,20 @@ declare global {
| undefined | undefined
} }
// Type for server HMR updates (matches EcmascriptMergedUpdate from the runtime) // Type for server HMR updates (matches the Update type from turbopack)
interface ServerHmrUpdate { interface ServerHmrUpdate {
type: 'EcmascriptMergedUpdate' type: 'partial'
entries: Record< instruction?: {
string, type: 'ChunkListUpdate'
{ code: string; url: string; map?: string | undefined } merged?: Array<{
> type: 'EcmascriptMergedUpdate'
chunks?: Record<string, { type: 'partial' }> entries: Record<
string,
{ code: string; url: string; map?: string | undefined }
>
chunks?: Record<string, { type: 'partial' }>
}>
}
} }
// hot-reloader modules are not bundled so we need to inject `__next__clear_chunk_cache__` // hot-reloader modules are not bundled so we need to inject `__next__clear_chunk_cache__`
......
...@@ -143,14 +143,20 @@ const sessionId = Math.floor(Number.MAX_SAFE_INTEGER * Math.random()) ...@@ -143,14 +143,20 @@ const sessionId = Math.floor(Number.MAX_SAFE_INTEGER * Math.random())
declare const __next__clear_chunk_cache__: (() => void) | null | undefined declare const __next__clear_chunk_cache__: (() => void) | null | undefined
// Type for server HMR updates (matches EcmascriptMergedUpdate from the runtime) // Type for server HMR updates (matches the Update type from turbopack)
interface ServerHmrUpdate { interface ServerHmrUpdate {
type: 'EcmascriptMergedUpdate' type: 'partial'
entries: Record< instruction?: {
string, type: 'ChunkListUpdate'
{ code: string; url: string; map?: string | undefined } merged?: Array<{
> type: 'EcmascriptMergedUpdate'
chunks?: Record<string, { type: 'partial' }> entries: Record<
string,
{ code: string; url: string; map?: string | undefined }
>
chunks?: Record<string, { type: 'partial' }>
}>
}
} }
declare const __next__server_hmr_apply__: declare const __next__server_hmr_apply__:
| ((update: ServerHmrUpdate) => boolean) | ((update: ServerHmrUpdate) => boolean)
...@@ -1491,32 +1497,35 @@ export async function createHotReloaderTurbopack( ...@@ -1491,32 +1497,35 @@ export async function createHotReloaderTurbopack(
function applyServerHmrUpdate(update: TurbopackUpdate): boolean { function applyServerHmrUpdate(update: TurbopackUpdate): boolean {
if (typeof __next__server_hmr_apply__ !== 'function') { if (typeof __next__server_hmr_apply__ !== 'function') {
// Server HMR not available (e.g., Pages Router or Edge Runtime) // Server HMR not available (e.g., Pages Router or Edge Runtime)
console.log('[Server HMR] __next__server_hmr_apply__ not available')
return false return false
} }
if (update.type !== 'partial') { if (update.type !== 'partial') {
// Only partial updates can be applied incrementally // Only partial updates can be applied incrementally
console.log('[Server HMR] Update type is not partial:', update.type)
return false return false
} }
const merged = update.instruction?.merged const merged = update.instruction?.merged
if (!merged || merged.length === 0) { if (!merged || merged.length === 0) {
// No actual module updates // No actual module updates
console.log('[Server HMR] No merged updates in instruction')
return false return false
} }
// Try to apply each merged update console.log(
for (const mergedUpdate of merged) { '[Server HMR] Applying update with',
if (mergedUpdate.type !== 'EcmascriptMergedUpdate') { merged.length,
// Unknown update type, fall back to full reload 'merged updates'
return false )
}
const applied = __next__server_hmr_apply__(mergedUpdate) // Pass the full update to the runtime - it handles extracting merged updates
if (!applied) { const applied = __next__server_hmr_apply__(update as any)
// Update was not accepted, need full reload if (!applied) {
return false // Update was not accepted, need full reload
} console.log('[Server HMR] Runtime did not accept update')
return false
} }
return true return true
...@@ -1528,19 +1537,39 @@ export async function createHotReloaderTurbopack( ...@@ -1528,19 +1537,39 @@ export async function createHotReloaderTurbopack(
*/ */
function subscribeToServerHmr(identifier: string) { function subscribeToServerHmr(identifier: string) {
if (serverHmrSubscriptions.has(identifier)) { if (serverHmrSubscriptions.has(identifier)) {
console.log('[Server HMR] Already subscribed to:', identifier)
return return
} }
console.log('[Server HMR] Creating subscription for:', identifier)
const subscription = project.serverHmrEvents(identifier) const subscription = project.serverHmrEvents(identifier)
serverHmrSubscriptions.set(identifier, subscription) serverHmrSubscriptions.set(identifier, subscription)
// Start listening for changes in background // Start listening for changes in background
;(async () => { ;(async () => {
// Skip initial state // Skip initial state
console.log('[Server HMR] Waiting for initial state:', identifier)
await subscription.next() await subscription.next()
console.log(
'[Server HMR] Subscription active, waiting for updates:',
identifier
)
for await (const result of subscription) { for await (const result of subscription) {
const update = result as TurbopackUpdate const update = result as TurbopackUpdate
console.log(
'[Server HMR] Received update for:',
identifier,
'type:',
update.type
)
// Skip non-partial updates (e.g., 'issues' updates are just diagnostics)
if (update.type !== 'partial') {
console.log('[Server HMR] Skipping non-partial update:', update.type)
continue
}
const applied = applyServerHmrUpdate(update) const applied = applyServerHmrUpdate(update)
if (applied) { if (applied) {
...@@ -1567,15 +1596,22 @@ export async function createHotReloaderTurbopack( ...@@ -1567,15 +1596,22 @@ export async function createHotReloaderTurbopack(
// Subscribe to server HMR identifiers to track available server modules // Subscribe to server HMR identifiers to track available server modules
;(async () => { ;(async () => {
console.log('[Server HMR] Starting identifiers subscription...')
const serverHmrIdentifiers = project.serverHmrIdentifiersSubscribe() const serverHmrIdentifiers = project.serverHmrIdentifiersSubscribe()
// Skip initial state
await serverHmrIdentifiers.next()
// Process identifiers (both initial and subsequent updates)
for await (const data of serverHmrIdentifiers) { for await (const data of serverHmrIdentifiers) {
console.log(
'[Server HMR] Got identifiers update:',
data.identifiers.length,
'total'
)
// Subscribe to HMR events for all server modules // Subscribe to HMR events for all server modules
// For now, focus on page.js files as a starting point // For now, focus on page.js files as a starting point
for (const identifier of data.identifiers) { for (const identifier of data.identifiers) {
if (identifier.includes('page.js')) { // Skip source maps and other non-JS files
if (identifier.endsWith('.js') && identifier.includes('page.js')) {
console.log('[Server HMR] Subscribing to:', identifier)
subscribeToServerHmr(identifier) subscribeToServerHmr(identifier)
} }
} }
......
...@@ -652,7 +652,13 @@ function getAffectedModuleEffects(moduleId: ModuleId): AffectedModuleEffect { ...@@ -652,7 +652,13 @@ function getAffectedModuleEffects(moduleId: ModuleId): AffectedModuleEffect {
} }
if (runtimeModules.has(moduleId)) { if (runtimeModules.has(moduleId)) {
// Reached a runtime module that doesn't accept itself // Reached a runtime module
if (serverHmrAutoAccept) {
// In server HMR mode with auto-accept enabled, treat runtime modules as accepting
// This allows server component updates to be applied without requiring the runtime
// module to explicitly call module.hot.accept()
continue
}
return { type: 'unaccepted', dependencyChain: newDependencyChain } return { type: 'unaccepted', dependencyChain: newDependencyChain }
} }
......
...@@ -21,7 +21,11 @@ workspace = true ...@@ -21,7 +21,11 @@ workspace = true
[dependencies] [dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }
bincode = { workspace = true } bincode = { workspace = true }
either = { workspace = true }
indoc = { workspace = true } indoc = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_qs = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
urlencoding = { workspace = true } urlencoding = { workspace = true }
......
...@@ -63,7 +63,7 @@ fn modifier() -> RcStr { ...@@ -63,7 +63,7 @@ fn modifier() -> RcStr {
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl EcmascriptBuildNodeChunk { impl EcmascriptBuildNodeChunk {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn own_content(self: Vc<Self>) -> Result<Vc<EcmascriptBuildNodeChunkContent>> { pub(crate) async fn own_content(self: Vc<Self>) -> Result<Vc<EcmascriptBuildNodeChunkContent>> {
let this = self.await?; let this = self.await?;
Ok(EcmascriptBuildNodeChunkContent::new( Ok(EcmascriptBuildNodeChunkContent::new(
*this.chunking_context, *this.chunking_context,
......
...@@ -7,19 +7,26 @@ use turbopack_core::{ ...@@ -7,19 +7,26 @@ use turbopack_core::{
code_builder::{Code, CodeBuilder}, code_builder::{Code, CodeBuilder},
output::OutputAsset, output::OutputAsset,
source_map::{GenerateSourceMap, SourceMapAsset}, source_map::{GenerateSourceMap, SourceMapAsset},
version::{Version, VersionedContent}, version::{
MergeableVersionedContent, Update, Version, VersionedContent, VersionedContentMerger,
},
}; };
use turbopack_ecmascript::{chunk::EcmascriptChunkContent, minify::minify, utils::StringifyJs}; use turbopack_ecmascript::{chunk::EcmascriptChunkContent, minify::minify, utils::StringifyJs};
use super::{chunk::EcmascriptBuildNodeChunk, version::EcmascriptBuildNodeChunkVersion}; use super::{
chunk::EcmascriptBuildNodeChunk, content_entry::EcmascriptBuildNodeChunkContentEntries,
merged::merger::EcmascriptBuildNodeChunkContentMerger,
single_chunk_update::update_single_ecmascript_node_chunk,
version::EcmascriptBuildNodeChunkVersion,
};
use crate::NodeJsChunkingContext; use crate::NodeJsChunkingContext;
#[turbo_tasks::value] #[turbo_tasks::value]
pub(super) struct EcmascriptBuildNodeChunkContent { pub(crate) struct EcmascriptBuildNodeChunkContent {
pub(super) content: ResolvedVc<EcmascriptChunkContent>, pub(crate) content: ResolvedVc<EcmascriptChunkContent>,
pub(super) chunking_context: ResolvedVc<NodeJsChunkingContext>, pub(crate) chunking_context: ResolvedVc<NodeJsChunkingContext>,
pub(super) chunk: ResolvedVc<EcmascriptBuildNodeChunk>, pub(crate) chunk: ResolvedVc<EcmascriptBuildNodeChunk>,
pub(super) source_map: ResolvedVc<SourceMapAsset>, pub(crate) source_map: ResolvedVc<SourceMapAsset>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -85,6 +92,11 @@ impl EcmascriptBuildNodeChunkContent { ...@@ -85,6 +92,11 @@ impl EcmascriptBuildNodeChunkContent {
*self.chunking_context.minify_type().await?, *self.chunking_context.minify_type().await?,
)) ))
} }
#[turbo_tasks::function]
pub fn entries(&self) -> Vc<EcmascriptBuildNodeChunkContentEntries> {
EcmascriptBuildNodeChunkContentEntries::new(*self.content)
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -114,4 +126,17 @@ impl VersionedContent for EcmascriptBuildNodeChunkContent { ...@@ -114,4 +126,17 @@ impl VersionedContent for EcmascriptBuildNodeChunkContent {
fn version(self: Vc<Self>) -> Vc<Box<dyn Version>> { fn version(self: Vc<Self>) -> Vc<Box<dyn Version>> {
Vc::upcast(self.own_version()) Vc::upcast(self.own_version())
} }
#[turbo_tasks::function]
fn update(self: Vc<Self>, from_version: Vc<Box<dyn Version>>) -> Vc<Update> {
update_single_ecmascript_node_chunk(self, from_version)
}
}
#[turbo_tasks::value_impl]
impl MergeableVersionedContent for EcmascriptBuildNodeChunkContent {
#[turbo_tasks::function]
fn get_merger(&self) -> Vc<Box<dyn VersionedContentMerger>> {
Vc::upcast(EcmascriptBuildNodeChunkContentMerger::new())
}
} }
use anyhow::Result;
use either::Either;
use turbo_tasks::{FxIndexMap, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc};
use turbopack_core::{
chunk::{AsyncModuleInfo, ChunkItemExt, ModuleId},
code_builder::Code,
};
use turbopack_ecmascript::chunk::{
EcmascriptChunkContent, EcmascriptChunkItem, EcmascriptChunkItemExt,
EcmascriptChunkItemOrBatchWithAsyncInfo, EcmascriptChunkItemWithAsyncInfo,
};
/// A chunk item's content entry for Node.js builds.
///
/// Instead of storing the [`Vc<Box<dyn EcmascriptChunkItem>>`] itself from
/// which `code` and `hash` are derived, we store `Vc`s directly. This avoids
/// creating tasks in a hot loop when iterating over thousands of entries when
/// computing updates.
#[turbo_tasks::value]
#[derive(Debug)]
pub struct EcmascriptBuildNodeChunkContentEntry {
pub code: ResolvedVc<Code>,
pub hash: ResolvedVc<u64>,
}
impl EcmascriptBuildNodeChunkContentEntry {
pub async fn new(
chunk_item: ResolvedVc<Box<dyn EcmascriptChunkItem>>,
async_module_info: Option<Vc<AsyncModuleInfo>>,
) -> Result<Self> {
let code = chunk_item.code(async_module_info).to_resolved().await?;
Ok(EcmascriptBuildNodeChunkContentEntry {
code,
hash: code.source_code_hash().to_resolved().await?,
})
}
}
#[turbo_tasks::value(transparent)]
pub struct EcmascriptBuildNodeChunkContentEntries(
#[bincode(with = "turbo_bincode::indexmap")]
FxIndexMap<ModuleId, EcmascriptBuildNodeChunkContentEntry>,
);
#[turbo_tasks::value_impl]
impl EcmascriptBuildNodeChunkContentEntries {
#[turbo_tasks::function]
pub async fn new(
chunk_content: Vc<EcmascriptChunkContent>,
) -> Result<Vc<EcmascriptBuildNodeChunkContentEntries>> {
let chunk_content = chunk_content.await?;
let entries: FxIndexMap<_, _> = chunk_content
.chunk_items
.iter()
.map(async |item| {
Ok(match item {
&EcmascriptChunkItemOrBatchWithAsyncInfo::ChunkItem(
EcmascriptChunkItemWithAsyncInfo {
chunk_item,
async_info,
},
) => Either::Left(std::iter::once((
chunk_item.id().await?,
EcmascriptBuildNodeChunkContentEntry::new(
chunk_item,
async_info.map(|info| *info),
)
.await?,
))),
EcmascriptChunkItemOrBatchWithAsyncInfo::Batch(batch) => {
let batch = batch.await?;
Either::Right(
batch
.chunk_items
.iter()
.map(|item| async move {
Ok((
item.chunk_item.id().await?,
EcmascriptBuildNodeChunkContentEntry::new(
item.chunk_item,
item.async_info.map(|info| *info),
)
.await?,
))
})
.try_join()
.await?
.into_iter(),
)
}
})
})
.try_flat_join()
.await?
.into_iter()
.collect();
Ok(Vc::cell(entries))
}
}
...@@ -15,10 +15,13 @@ use turbopack_core::{ ...@@ -15,10 +15,13 @@ use turbopack_core::{
OutputAssetsWithReferenced, OutputAssetsWithReferenced,
}, },
source_map::{GenerateSourceMap, SourceMapAsset}, source_map::{GenerateSourceMap, SourceMapAsset},
version::VersionedContent,
}; };
use turbopack_ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs}; use turbopack_ecmascript::{chunk::EcmascriptChunkPlaceable, utils::StringifyJs};
use super::runtime::EcmascriptBuildNodeRuntimeChunk; use super::{
content::EcmascriptBuildNodeEntryChunkContent, runtime::EcmascriptBuildNodeRuntimeChunk,
};
use crate::NodeJsChunkingContext; use crate::NodeJsChunkingContext;
/// An Ecmascript chunk that loads a list of parallel chunks, then instantiates /// An Ecmascript chunk that loads a list of parallel chunks, then instantiates
...@@ -63,7 +66,7 @@ impl EcmascriptBuildNodeEntryChunk { ...@@ -63,7 +66,7 @@ impl EcmascriptBuildNodeEntryChunk {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn code(self: Vc<Self>) -> Result<Vc<Code>> { pub(crate) async fn code(self: Vc<Self>) -> Result<Vc<Code>> {
let this = self.await?; let this = self.await?;
let output_root = this.chunking_context.output_root().owned().await?; let output_root = this.chunking_context.output_root().owned().await?;
...@@ -160,6 +163,21 @@ impl EcmascriptBuildNodeEntryChunk { ...@@ -160,6 +163,21 @@ impl EcmascriptBuildNodeEntryChunk {
Vc::upcast(self), Vc::upcast(self),
)) ))
} }
#[turbo_tasks::function]
fn own_content(self: Vc<Self>) -> Vc<EcmascriptBuildNodeEntryChunkContent> {
EcmascriptBuildNodeEntryChunkContent::new(self, self.chunking_context())
}
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<NodeJsChunkingContext> {
*self.chunking_context
}
#[turbo_tasks::function]
pub(crate) fn other_chunks(&self) -> Vc<OutputAssets> {
*self.other_chunks
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -216,6 +234,11 @@ impl Asset for EcmascriptBuildNodeEntryChunk { ...@@ -216,6 +234,11 @@ impl Asset for EcmascriptBuildNodeEntryChunk {
FileContent::Content(File::from(code.source_code().clone())).cell(), FileContent::Content(File::from(code.source_code().clone())).cell(),
)) ))
} }
#[turbo_tasks::function]
fn versioned_content(self: Vc<Self>) -> Vc<Box<dyn VersionedContent>> {
Vc::upcast(self.own_content())
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
use anyhow::Result;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbo_tasks_fs::{File, FileContent};
use turbopack_core::{
asset::AssetContent,
version::{Update, Version, VersionedContent},
};
use super::{
chunk::EcmascriptBuildNodeEntryChunk, update::update_ecmascript_node_entry_chunk,
version::EcmascriptBuildNodeEntryChunkVersion,
};
use crate::{
NodeJsChunkingContext,
ecmascript::node::{chunk::EcmascriptBuildNodeChunk, content::EcmascriptBuildNodeChunkContent},
};
/// Content wrapper for entry chunks that enables HMR updates.
/// Tracks the versions of all child chunks to compute incremental updates.
#[turbo_tasks::value]
pub(crate) struct EcmascriptBuildNodeEntryChunkContent {
pub(crate) entry_chunk: ResolvedVc<EcmascriptBuildNodeEntryChunk>,
pub(crate) chunking_context: ResolvedVc<NodeJsChunkingContext>,
/// The child chunk contents that contain the actual module code
pub(crate) chunk_contents: Vec<ResolvedVc<EcmascriptBuildNodeChunkContent>>,
}
#[turbo_tasks::value_impl]
impl EcmascriptBuildNodeEntryChunkContent {
#[turbo_tasks::function]
pub(crate) async fn new(
entry_chunk: Vc<EcmascriptBuildNodeEntryChunk>,
chunking_context: Vc<NodeJsChunkingContext>,
) -> Result<Vc<Self>> {
// Get the other_chunks via the getter method
let other_chunks = entry_chunk.other_chunks().await?;
let chunk_contents: Vec<ResolvedVc<EcmascriptBuildNodeChunkContent>> = other_chunks
.iter()
.filter_map(|chunk| ResolvedVc::try_downcast_type::<EcmascriptBuildNodeChunk>(*chunk))
.map(|chunk: ResolvedVc<EcmascriptBuildNodeChunk>| async move {
// Use own_content() to get the content
let content = chunk.own_content().to_resolved().await?;
Ok(content)
})
.try_join()
.await?;
Ok(EcmascriptBuildNodeEntryChunkContent {
entry_chunk: entry_chunk.to_resolved().await?,
chunking_context: chunking_context.to_resolved().await?,
chunk_contents,
}
.cell())
}
#[turbo_tasks::function]
pub(crate) async fn own_version(&self) -> Result<Vc<EcmascriptBuildNodeEntryChunkVersion>> {
Ok(EcmascriptBuildNodeEntryChunkVersion {
chunk_versions: self
.chunk_contents
.iter()
.map(|content| async move { content.own_version().await })
.try_join()
.await?,
}
.cell())
}
}
#[turbo_tasks::value_impl]
impl VersionedContent for EcmascriptBuildNodeEntryChunkContent {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let this = self.await?;
// The entry chunk's actual content comes from the entry chunk itself
Ok(AssetContent::file(
FileContent::Content(File::from(
this.entry_chunk.code().await?.source_code().clone(),
))
.cell(),
))
}
#[turbo_tasks::function]
fn version(self: Vc<Self>) -> Vc<Box<dyn Version>> {
Vc::upcast(self.own_version())
}
#[turbo_tasks::function]
fn update(self: Vc<Self>, from_version: Vc<Box<dyn Version>>) -> Vc<Update> {
update_ecmascript_node_entry_chunk(self, from_version)
}
}
pub(crate) mod chunk; pub(crate) mod chunk;
pub(crate) mod content;
pub(crate) mod runtime; pub(crate) mod runtime;
pub(crate) mod update;
pub(crate) mod version;
use std::sync::Arc;
use anyhow::Result;
use serde::Serialize;
use turbo_tasks::{FxIndexMap, FxIndexSet, IntoTraitRef, ReadRef, ResolvedVc, TryJoinIterExt, Vc};
use turbo_tasks_fs::rope::Rope;
use turbopack_core::{
chunk::{ChunkingContext, ModuleId},
code_builder::Code,
output::OutputAsset,
source_map::GenerateSourceMap,
version::{PartialUpdate, TotalUpdate, Update, Version},
};
use super::{
content::EcmascriptBuildNodeEntryChunkContent, version::EcmascriptBuildNodeEntryChunkVersion,
};
use crate::ecmascript::node::{
update::{EcmascriptNodeChunkUpdate, update_ecmascript_node_chunk},
version::EcmascriptBuildNodeChunkVersion,
};
/// Wrapper for the chunk list update format expected by the TypeScript runtime.
/// This mirrors the browser's ChunkListUpdate format.
#[derive(Serialize)]
#[serde(tag = "type")]
#[serde(rename_all = "camelCase")]
struct ChunkListUpdate {
/// List of merged updates since the last version.
#[serde(skip_serializing_if = "Vec::is_empty")]
merged: Vec<serde_json::Value>,
}
#[derive(Serialize, Default)]
#[serde(tag = "type", rename_all = "camelCase")]
struct EcmascriptMergedUpdate<'a> {
/// A map from module id to latest module entry.
#[serde(skip_serializing_if = "FxIndexMap::is_empty")]
entries: FxIndexMap<ModuleId, EcmascriptModuleEntry>,
/// A map from chunk path to the chunk update.
#[serde(skip_serializing_if = "FxIndexMap::is_empty")]
chunks: FxIndexMap<&'a str, EcmascriptMergedChunkUpdate>,
}
impl EcmascriptMergedUpdate<'_> {
fn is_empty(&self) -> bool {
self.entries.is_empty() && self.chunks.is_empty()
}
}
#[derive(Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
enum EcmascriptMergedChunkUpdate {
Added(EcmascriptMergedChunkAdded),
Deleted(EcmascriptMergedChunkDeleted),
Partial(EcmascriptMergedChunkPartial),
}
#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
struct EcmascriptMergedChunkAdded {
#[serde(skip_serializing_if = "FxIndexSet::is_empty")]
modules: FxIndexSet<ModuleId>,
}
#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
struct EcmascriptMergedChunkDeleted {
#[serde(skip_serializing_if = "FxIndexSet::is_empty")]
modules: FxIndexSet<ModuleId>,
}
#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
struct EcmascriptMergedChunkPartial {
#[serde(skip_serializing_if = "FxIndexSet::is_empty")]
added: FxIndexSet<ModuleId>,
#[serde(skip_serializing_if = "FxIndexSet::is_empty")]
deleted: FxIndexSet<ModuleId>,
}
#[derive(Serialize)]
struct EcmascriptModuleEntry {
#[serde(with = "turbo_tasks_fs::rope::ser_as_string")]
code: Rope,
url: String,
#[serde(with = "turbo_tasks_fs::rope::ser_option_as_string")]
map: Option<Rope>,
}
impl EcmascriptModuleEntry {
async fn from_code(id: &ModuleId, code: Vc<Code>, chunk_path: &str) -> Result<Self> {
let map = &*code.generate_source_map().await?;
let map = map.as_content().map(|f| f.content().clone());
/// serde_qs can't serialize a lone enum when it's [serde::untagged].
#[derive(Serialize)]
struct Id<'a> {
id: &'a ModuleId,
}
let id = serde_qs::to_string(&Id { id }).unwrap();
Ok(EcmascriptModuleEntry {
code: code.await?.source_code().clone(),
url: format!("{}?{}", chunk_path, &id),
map,
})
}
}
/// Helper structure to get a module's hash from multiple different chunk
/// versions, without having to actually merge the versions into a single
/// hashmap.
struct MergedModuleMap {
versions: Vec<ReadRef<EcmascriptBuildNodeChunkVersion>>,
}
impl MergedModuleMap {
fn new(versions: Vec<ReadRef<EcmascriptBuildNodeChunkVersion>>) -> Self {
Self { versions }
}
fn get(&self, id: &ModuleId) -> Option<u64> {
for version in &self.versions {
if let Some(hash) = version.entries_hashes.get(id) {
return Some(*hash);
}
}
None
}
}
/// Computes an update for an entry chunk by comparing the versions of its child chunks.
#[turbo_tasks::function]
pub(crate) async fn update_ecmascript_node_entry_chunk(
content: Vc<EcmascriptBuildNodeEntryChunkContent>,
from_version: Vc<Box<dyn Version>>,
) -> Result<Vc<Update>> {
let to_version = content.own_version();
let from_version = if let Some(from) = ResolvedVc::try_downcast_type::<
EcmascriptBuildNodeEntryChunkVersion,
>(from_version.to_resolved().await?)
{
from
} else {
// It's likely `from_version` is `NotFoundVersion`.
return Ok(Update::Total(TotalUpdate {
to: Vc::upcast::<Box<dyn Version>>(to_version)
.into_trait_ref()
.await?,
})
.cell());
};
let to = to_version.await?;
let from = from_version.await?;
// When to and from point to the same value we can skip comparing them.
if from.ptr_eq(&to) {
return Ok(Update::None.cell());
}
let content_ref = content.await?;
// Build a map of chunk paths to their old versions
let mut from_versions_by_chunk_path: FxIndexMap<_, _> = from
.chunk_versions
.iter()
.map(|version| (&*version.chunk_path, version))
.collect();
let merged_module_map = MergedModuleMap::new(from.chunk_versions.to_vec());
// Process each child chunk content
let to_contents = content_ref
.chunk_contents
.iter()
.map(|chunk_content| async move {
let entries = chunk_content.entries().await?;
let chunk_content_ref = chunk_content.await?;
let output_root = chunk_content_ref.chunking_context.output_root().await?;
let path = chunk_content_ref.chunk.path().await?;
Ok((*chunk_content, entries, output_root, path))
})
.try_join()
.await?;
let mut merged_update = EcmascriptMergedUpdate::default();
for (chunk_content, entries, output_root, path) in &to_contents {
let Some(chunk_path) = output_root.get_path_to(path) else {
continue;
};
let chunk_update = if let Some(from_version) =
from_versions_by_chunk_path.swap_remove(chunk_path)
{
// The chunk was present in the previous version, so we must update it.
let update = update_ecmascript_node_chunk(**chunk_content, from_version).await?;
match update {
EcmascriptNodeChunkUpdate::None => {
// Nothing changed, so we can skip this chunk.
continue;
}
EcmascriptNodeChunkUpdate::Partial(chunk_partial) => {
// The chunk was updated.
let mut partial = EcmascriptMergedChunkPartial::default();
for (module_id, (module_hash, module_code)) in chunk_partial.added {
partial.added.insert(module_id.clone());
if merged_module_map.get(&module_id) != Some(module_hash) {
let entry = EcmascriptModuleEntry::from_code(
&module_id,
*module_code,
chunk_path,
)
.await?;
merged_update.entries.insert(module_id, entry);
}
}
partial.deleted.extend(chunk_partial.deleted.into_keys());
for (module_id, module_code) in chunk_partial.modified {
let entry =
EcmascriptModuleEntry::from_code(&module_id, *module_code, chunk_path)
.await?;
merged_update.entries.insert(module_id, entry);
}
EcmascriptMergedChunkUpdate::Partial(partial)
}
}
} else {
// The chunk was added in this version.
let mut added = EcmascriptMergedChunkAdded::default();
for (id, entry) in entries {
let hash = *entry.hash.await?;
added.modules.insert(id.clone());
if merged_module_map.get(id) != Some(hash) {
let entry =
EcmascriptModuleEntry::from_code(id, *entry.code, chunk_path).await?;
merged_update.entries.insert(id.clone(), entry);
}
}
EcmascriptMergedChunkUpdate::Added(added)
};
merged_update.chunks.insert(chunk_path, chunk_update);
}
// Deleted chunks.
for (chunk_path, chunk_version) in from_versions_by_chunk_path {
let hashes = &chunk_version.entries_hashes;
merged_update.chunks.insert(
chunk_path,
EcmascriptMergedChunkUpdate::Deleted(EcmascriptMergedChunkDeleted {
modules: hashes.keys().cloned().collect(),
}),
);
}
let update = if merged_update.is_empty() {
Update::None
} else {
// Wrap in ChunkListUpdate with merged array, matching the browser format
let chunk_list_update = ChunkListUpdate {
merged: vec![serde_json::to_value(&merged_update)?],
};
Update::Partial(PartialUpdate {
to: Vc::upcast::<Box<dyn Version>>(to_version)
.into_trait_ref()
.await?,
instruction: Arc::new(serde_json::to_value(&chunk_list_update)?),
})
};
Ok(update.cell())
}
use turbo_rcstr::RcStr;
use turbo_tasks::{ReadRef, Vc};
use turbopack_core::version::Version;
use crate::ecmascript::node::version::EcmascriptBuildNodeChunkVersion;
/// Version for entry chunks that tracks the versions of all child chunks.
#[turbo_tasks::value(serialization = "none", shared)]
pub(crate) struct EcmascriptBuildNodeEntryChunkVersion {
/// Versions of all child chunks
pub(crate) chunk_versions: Vec<ReadRef<EcmascriptBuildNodeChunkVersion>>,
}
#[turbo_tasks::value_impl]
impl Version for EcmascriptBuildNodeEntryChunkVersion {
#[turbo_tasks::function]
fn id(&self) -> Vc<RcStr> {
// Combine all chunk version IDs
let mut combined = String::new();
for version in &self.chunk_versions {
if !combined.is_empty() {
combined.push(':');
}
combined.push_str(&version.chunk_path);
}
Vc::cell(combined.into())
}
}
use anyhow::{Result, bail};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbopack_core::{
asset::AssetContent,
version::{Update, Version, VersionedContent},
};
use super::{
super::content::EcmascriptBuildNodeChunkContent, update::update_ecmascript_node_merged_chunk,
version::EcmascriptBuildNodeMergedChunkVersion,
};
/// Composite [`EcmascriptBuildNodeChunkContent`] that is the result of merging
/// multiple Node.js chunk contents together through the
/// [`EcmascriptBuildNodeChunkContentMerger`].
#[turbo_tasks::value(serialization = "none", shared)]
pub(crate) struct EcmascriptBuildNodeMergedChunkContent {
pub(super) contents: Vec<ResolvedVc<EcmascriptBuildNodeChunkContent>>,
}
#[turbo_tasks::value_impl]
impl EcmascriptBuildNodeMergedChunkContent {
#[turbo_tasks::function]
pub async fn version(&self) -> Result<Vc<EcmascriptBuildNodeMergedChunkVersion>> {
Ok(EcmascriptBuildNodeMergedChunkVersion {
versions: self
.contents
.iter()
.map(|content| async move { content.own_version().await })
.try_join()
.await?,
}
.cell())
}
}
#[turbo_tasks::value_impl]
impl VersionedContent for EcmascriptBuildNodeMergedChunkContent {
#[turbo_tasks::function]
fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
bail!("EcmascriptBuildNodeMergedChunkContent does not have content")
}
#[turbo_tasks::function]
fn version(self: Vc<Self>) -> Vc<Box<dyn Version>> {
Vc::upcast(self.version())
}
#[turbo_tasks::function]
async fn update(
self: Vc<Self>,
from_version: ResolvedVc<Box<dyn Version>>,
) -> Result<Vc<Update>> {
Ok(update_ecmascript_node_merged_chunk(self, from_version)
.await?
.cell())
}
}
use anyhow::{Result, bail};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use turbopack_core::version::{VersionedContent, VersionedContentMerger, VersionedContents};
use super::{
super::content::EcmascriptBuildNodeChunkContent, content::EcmascriptBuildNodeMergedChunkContent,
};
/// Merges multiple [`EcmascriptBuildNodeChunkContent`] into a single
/// [`EcmascriptBuildNodeMergedChunkContent`]. This is useful for generating a
/// single update for multiple Node.js chunks updating all at the same time.
#[turbo_tasks::value]
pub(crate) struct EcmascriptBuildNodeChunkContentMerger;
#[turbo_tasks::value_impl]
impl EcmascriptBuildNodeChunkContentMerger {
#[turbo_tasks::function]
pub fn new() -> Vc<Self> {
Self::cell(EcmascriptBuildNodeChunkContentMerger)
}
}
#[turbo_tasks::value_impl]
impl VersionedContentMerger for EcmascriptBuildNodeChunkContentMerger {
#[turbo_tasks::function]
async fn merge(
&self,
contents: Vc<VersionedContents>,
) -> Result<Vc<Box<dyn VersionedContent>>> {
let contents = contents
.await?
.iter()
.map(|content| async move {
if let Some(content) =
ResolvedVc::try_downcast_type::<EcmascriptBuildNodeChunkContent>(*content)
{
Ok(content)
} else {
bail!("expected Vc<EcmascriptBuildNodeChunkContent>")
}
})
.try_join()
.await?;
Ok(Vc::upcast(
EcmascriptBuildNodeMergedChunkContent { contents }.cell(),
))
}
}
pub(crate) mod content;
pub(crate) mod merger;
pub(crate) mod update;
pub(crate) mod version;
use std::sync::Arc;
use anyhow::Result;
use serde::Serialize;
use turbo_tasks::{FxIndexMap, FxIndexSet, IntoTraitRef, ReadRef, ResolvedVc, TryJoinIterExt, Vc};
use turbo_tasks_fs::rope::Rope;
use turbopack_core::{
chunk::{ChunkingContext, ModuleId},
code_builder::Code,
output::OutputAsset,
source_map::GenerateSourceMap,
version::{PartialUpdate, TotalUpdate, Update, Version},
};
use super::{
super::{
update::{EcmascriptNodeChunkUpdate, update_ecmascript_node_chunk},
version::EcmascriptBuildNodeChunkVersion,
},
content::EcmascriptBuildNodeMergedChunkContent,
version::EcmascriptBuildNodeMergedChunkVersion,
};
/// Wrapper for the chunk list update format expected by the TypeScript runtime.
/// This mirrors the browser's ChunkListUpdate format.
#[derive(Serialize)]
#[serde(tag = "type")]
#[serde(rename_all = "camelCase")]
struct ChunkListUpdate {
/// List of merged updates since the last version.
#[serde(skip_serializing_if = "Vec::is_empty")]
merged: Vec<serde_json::Value>,
}
#[derive(Serialize, Default)]
#[serde(tag = "type", rename_all = "camelCase")]
struct EcmascriptMergedUpdate<'a> {
/// A map from module id to latest module entry.
#[serde(skip_serializing_if = "FxIndexMap::is_empty")]
entries: FxIndexMap<ModuleId, EcmascriptModuleEntry>,
/// A map from chunk path to the chunk update.
#[serde(skip_serializing_if = "FxIndexMap::is_empty")]
chunks: FxIndexMap<&'a str, EcmascriptMergedChunkUpdate>,
}
impl EcmascriptMergedUpdate<'_> {
fn is_empty(&self) -> bool {
self.entries.is_empty() && self.chunks.is_empty()
}
}
#[derive(Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
enum EcmascriptMergedChunkUpdate {
Added(EcmascriptMergedChunkAdded),
Deleted(EcmascriptMergedChunkDeleted),
Partial(EcmascriptMergedChunkPartial),
}
#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
struct EcmascriptMergedChunkAdded {
#[serde(skip_serializing_if = "FxIndexSet::is_empty")]
modules: FxIndexSet<ModuleId>,
}
#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
struct EcmascriptMergedChunkDeleted {
#[serde(skip_serializing_if = "FxIndexSet::is_empty")]
modules: FxIndexSet<ModuleId>,
}
#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
struct EcmascriptMergedChunkPartial {
#[serde(skip_serializing_if = "FxIndexSet::is_empty")]
added: FxIndexSet<ModuleId>,
#[serde(skip_serializing_if = "FxIndexSet::is_empty")]
deleted: FxIndexSet<ModuleId>,
}
#[derive(Serialize)]
struct EcmascriptModuleEntry {
#[serde(with = "turbo_tasks_fs::rope::ser_as_string")]
code: Rope,
url: String,
#[serde(with = "turbo_tasks_fs::rope::ser_option_as_string")]
map: Option<Rope>,
}
impl EcmascriptModuleEntry {
async fn from_code(id: &ModuleId, code: Vc<Code>, chunk_path: &str) -> Result<Self> {
let map = &*code.generate_source_map().await?;
let map = map.as_content().map(|f| f.content().clone());
/// serde_qs can't serialize a lone enum when it's [serde::untagged].
#[derive(Serialize)]
struct Id<'a> {
id: &'a ModuleId,
}
let id = serde_qs::to_string(&Id { id }).unwrap();
Ok(EcmascriptModuleEntry {
// Cloning a rope is cheap.
code: code.await?.source_code().clone(),
url: format!("{}?{}", chunk_path, &id),
map,
})
}
}
/// Helper structure to get a module's hash from multiple different chunk
/// versions, without having to actually merge the versions into a single
/// hashmap.
struct MergedModuleMap {
versions: Vec<ReadRef<EcmascriptBuildNodeChunkVersion>>,
}
impl MergedModuleMap {
fn new(versions: Vec<ReadRef<EcmascriptBuildNodeChunkVersion>>) -> Self {
Self { versions }
}
fn get(&self, id: &ModuleId) -> Option<u64> {
for version in &self.versions {
if let Some(hash) = version.entries_hashes.get(id) {
return Some(*hash);
}
}
None
}
}
pub(super) async fn update_ecmascript_node_merged_chunk(
content: Vc<EcmascriptBuildNodeMergedChunkContent>,
from_version: ResolvedVc<Box<dyn Version>>,
) -> Result<Update> {
let to_merged_version = content.version();
let from_merged_version = if let Some(from) =
ResolvedVc::try_downcast_type::<EcmascriptBuildNodeMergedChunkVersion>(from_version)
{
from
} else {
// It's likely `from_version` is `NotFoundVersion`.
return Ok(Update::Total(TotalUpdate {
to: Vc::upcast::<Box<dyn Version>>(to_merged_version)
.into_trait_ref()
.await?,
}));
};
let to = to_merged_version.await?;
let from = from_merged_version.await?;
// When to and from point to the same value we can skip comparing them.
if from.ptr_eq(&to) {
return Ok(Update::None);
}
let mut from_versions_by_chunk_path: FxIndexMap<_, _> = from
.versions
.iter()
.map(|version| (&*version.chunk_path, version))
.collect();
let merged_module_map = MergedModuleMap::new(from.versions.to_vec());
let content = content.await?;
let to_contents = content
.contents
.iter()
.map(|content| async move {
let entries = content.entries().await?;
let content_ref = content.await?;
let output_root = content_ref.chunking_context.output_root().await?;
let path = content_ref.chunk.path().await?;
Ok((*content, entries, output_root, path))
})
.try_join()
.await?;
let mut merged_update = EcmascriptMergedUpdate::default();
for (content, entries, output_root, path) in &to_contents {
let Some(chunk_path) = output_root.get_path_to(path) else {
continue;
};
let chunk_update = if let Some(from_version) =
from_versions_by_chunk_path.swap_remove(chunk_path)
{
// The chunk was present in the previous version, so we must update it.
let update = update_ecmascript_node_chunk(**content, from_version).await?;
match update {
EcmascriptNodeChunkUpdate::None => {
// Nothing changed, so we can skip this chunk.
continue;
}
EcmascriptNodeChunkUpdate::Partial(chunk_partial) => {
// The chunk was updated.
let mut partial = EcmascriptMergedChunkPartial::default();
for (module_id, (module_hash, module_code)) in chunk_partial.added {
partial.added.insert(module_id.clone());
if merged_module_map.get(&module_id) != Some(module_hash) {
let entry = EcmascriptModuleEntry::from_code(
&module_id,
*module_code,
chunk_path,
)
.await?;
merged_update.entries.insert(module_id, entry);
}
}
partial.deleted.extend(chunk_partial.deleted.into_keys());
for (module_id, module_code) in chunk_partial.modified {
let entry =
EcmascriptModuleEntry::from_code(&module_id, *module_code, chunk_path)
.await?;
merged_update.entries.insert(module_id, entry);
}
EcmascriptMergedChunkUpdate::Partial(partial)
}
}
} else {
// The chunk was added in this version.
let mut added = EcmascriptMergedChunkAdded::default();
for (id, entry) in entries {
let hash = *entry.hash.await?;
added.modules.insert(id.clone());
if merged_module_map.get(id) != Some(hash) {
let entry =
EcmascriptModuleEntry::from_code(id, *entry.code, chunk_path).await?;
merged_update.entries.insert(id.clone(), entry);
}
}
EcmascriptMergedChunkUpdate::Added(added)
};
merged_update.chunks.insert(chunk_path, chunk_update);
}
// Deleted chunks.
for (chunk_path, chunk_version) in from_versions_by_chunk_path {
let hashes = &chunk_version.entries_hashes;
merged_update.chunks.insert(
chunk_path,
EcmascriptMergedChunkUpdate::Deleted(EcmascriptMergedChunkDeleted {
modules: hashes.keys().cloned().collect(),
}),
);
}
let update = if merged_update.is_empty() {
Update::None
} else {
// Wrap in ChunkListUpdate with merged array, matching the browser format
let chunk_list_update = ChunkListUpdate {
merged: vec![serde_json::to_value(&merged_update)?],
};
Update::Partial(PartialUpdate {
to: Vc::upcast::<Box<dyn Version>>(to_merged_version)
.into_trait_ref()
.await?,
instruction: Arc::new(serde_json::to_value(&chunk_list_update)?),
})
};
Ok(update)
}
use anyhow::Result;
use turbo_rcstr::RcStr;
use turbo_tasks::{ReadRef, TryJoinIterExt, Vc};
use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_hex};
use turbopack_core::version::Version;
use super::super::version::EcmascriptBuildNodeChunkVersion;
/// The version of a [`super::content::EcmascriptBuildNodeMergedChunkContent`].
/// This is essentially a composite [`EcmascriptBuildNodeChunkVersion`].
#[turbo_tasks::value(serialization = "none", shared)]
pub(super) struct EcmascriptBuildNodeMergedChunkVersion {
#[turbo_tasks(trace_ignore)]
pub(super) versions: Vec<ReadRef<EcmascriptBuildNodeChunkVersion>>,
}
#[turbo_tasks::value_impl]
impl Version for EcmascriptBuildNodeMergedChunkVersion {
#[turbo_tasks::function]
async fn id(&self) -> Result<Vc<RcStr>> {
let mut hasher = Xxh3Hash64Hasher::new();
hasher.write_value(self.versions.len());
let sorted_ids = {
let mut sorted_ids = self
.versions
.iter()
.map(|version| async move { ReadRef::cell(version.clone()).id().await })
.try_join()
.await?;
sorted_ids.sort();
sorted_ids
};
for id in sorted_ids {
hasher.write_value(id);
}
let hash = hasher.finish();
let hex_hash = encode_hex(hash);
Ok(Vc::cell(hex_hash.into()))
}
}
pub(crate) mod chunk; pub(crate) mod chunk;
pub(crate) mod content; pub(crate) mod content;
pub(crate) mod content_entry;
pub(crate) mod entry; pub(crate) mod entry;
pub(crate) mod merged;
pub(crate) mod single_chunk_update;
pub(crate) mod update;
pub(crate) mod version; pub(crate) mod version;
use std::sync::Arc;
use anyhow::Result;
use serde::Serialize;
use turbo_tasks::{FxIndexMap, FxIndexSet, IntoTraitRef, ResolvedVc, Vc};
use turbo_tasks_fs::rope::Rope;
use turbopack_core::{
chunk::{ChunkingContext, ModuleId},
code_builder::Code,
output::OutputAsset,
source_map::GenerateSourceMap,
version::{PartialUpdate, TotalUpdate, Update, Version},
};
use super::{content::EcmascriptBuildNodeChunkContent, version::EcmascriptBuildNodeChunkVersion};
/// Wrapper for the chunk list update format expected by the TypeScript runtime.
/// This mirrors the browser's ChunkListUpdate format.
#[derive(Serialize)]
#[serde(tag = "type")]
#[serde(rename_all = "camelCase")]
struct ChunkListUpdate {
/// List of merged updates since the last version.
#[serde(skip_serializing_if = "Vec::is_empty")]
merged: Vec<serde_json::Value>,
}
#[derive(Serialize, Default)]
#[serde(tag = "type", rename_all = "camelCase")]
struct EcmascriptMergedUpdate<'a> {
/// A map from module id to latest module entry.
#[serde(skip_serializing_if = "FxIndexMap::is_empty")]
entries: FxIndexMap<ModuleId, EcmascriptModuleEntry>,
/// A map from chunk path to the chunk update.
#[serde(skip_serializing_if = "FxIndexMap::is_empty")]
chunks: FxIndexMap<&'a str, EcmascriptChunkUpdate>,
}
impl EcmascriptMergedUpdate<'_> {
fn is_empty(&self) -> bool {
self.entries.is_empty() && self.chunks.is_empty()
}
}
#[derive(Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
enum EcmascriptChunkUpdate {
Partial(EcmascriptChunkPartial),
}
#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
struct EcmascriptChunkPartial {
#[serde(skip_serializing_if = "FxIndexSet::is_empty")]
added: FxIndexSet<ModuleId>,
#[serde(skip_serializing_if = "FxIndexSet::is_empty")]
deleted: FxIndexSet<ModuleId>,
}
#[derive(Serialize)]
struct EcmascriptModuleEntry {
#[serde(with = "turbo_tasks_fs::rope::ser_as_string")]
code: Rope,
url: String,
#[serde(with = "turbo_tasks_fs::rope::ser_option_as_string")]
map: Option<Rope>,
}
impl EcmascriptModuleEntry {
async fn from_code(id: &ModuleId, code: Vc<Code>, chunk_path: &str) -> Result<Self> {
let map = &*code.generate_source_map().await?;
let map = map.as_content().map(|f| f.content().clone());
/// serde_qs can't serialize a lone enum when it's [serde::untagged].
#[derive(Serialize)]
struct Id<'a> {
id: &'a ModuleId,
}
let id = serde_qs::to_string(&Id { id }).unwrap();
Ok(EcmascriptModuleEntry {
code: code.await?.source_code().clone(),
url: format!("{}?{}", chunk_path, &id),
map,
})
}
}
/// Computes an update for a single Node.js chunk.
/// This produces `Partial` updates with module code for HMR.
#[turbo_tasks::function]
pub async fn update_single_ecmascript_node_chunk(
content: Vc<EcmascriptBuildNodeChunkContent>,
from_version: Vc<Box<dyn Version>>,
) -> Result<Vc<Update>> {
let to_version = content.own_version();
let from_version = if let Some(from) = ResolvedVc::try_downcast_type::<
EcmascriptBuildNodeChunkVersion,
>(from_version.to_resolved().await?)
{
from
} else {
// It's likely `from_version` is `NotFoundVersion`.
return Ok(Update::Total(TotalUpdate {
to: Vc::upcast::<Box<dyn Version>>(to_version)
.into_trait_ref()
.await?,
})
.cell());
};
let to = to_version.await?;
let from = from_version.await?;
// When to and from point to the same value we can skip comparing them.
if from.ptr_eq(&to) {
return Ok(Update::None.cell());
}
let entries = content.entries().await?;
let content_ref = content.await?;
let output_root = content_ref.chunking_context.output_root().await?;
let path = content_ref.chunk.path().await?;
let Some(chunk_path) = output_root.get_path_to(&path) else {
return Ok(Update::Total(TotalUpdate {
to: Vc::upcast::<Box<dyn Version>>(to_version)
.into_trait_ref()
.await?,
})
.cell());
};
let mut merged_update = EcmascriptMergedUpdate::default();
let mut chunk_partial = EcmascriptChunkPartial::default();
// Find modified and deleted entries
for (id, from_hash) in &from.entries_hashes {
if let Some(entry) = entries.get(id) {
let to_hash = *entry.hash.await?;
if to_hash != *from_hash {
// Modified
let entry = EcmascriptModuleEntry::from_code(id, *entry.code, chunk_path).await?;
merged_update.entries.insert(id.clone(), entry);
}
} else {
// Deleted
chunk_partial.deleted.insert(id.clone());
}
}
// Find added entries
for (id, entry) in entries.iter() {
if !from.entries_hashes.contains_key(id) {
chunk_partial.added.insert(id.clone());
let entry = EcmascriptModuleEntry::from_code(id, *entry.code, chunk_path).await?;
merged_update.entries.insert(id.clone(), entry);
}
}
// Only add chunk update if there are changes
if !chunk_partial.added.is_empty() || !chunk_partial.deleted.is_empty() {
merged_update
.chunks
.insert(chunk_path, EcmascriptChunkUpdate::Partial(chunk_partial));
}
let update = if merged_update.is_empty() {
Update::None
} else {
// Wrap in ChunkListUpdate with merged array, matching the browser format
let chunk_list_update = ChunkListUpdate {
merged: vec![serde_json::to_value(&merged_update)?],
};
Update::Partial(PartialUpdate {
to: Vc::upcast::<Box<dyn Version>>(to_version)
.into_trait_ref()
.await?,
instruction: Arc::new(serde_json::to_value(&chunk_list_update)?),
})
};
Ok(update.cell())
}
use anyhow::Result;
use turbo_tasks::{FxIndexMap, ReadRef, ResolvedVc, Vc};
use turbopack_core::{chunk::ModuleId, code_builder::Code};
use super::{content::EcmascriptBuildNodeChunkContent, version::EcmascriptBuildNodeChunkVersion};
#[allow(clippy::large_enum_variant)]
pub(crate) enum EcmascriptNodeChunkUpdate {
None,
Partial(EcmascriptNodeChunkPartialUpdate),
}
pub(crate) struct EcmascriptNodeChunkPartialUpdate {
pub added: FxIndexMap<ModuleId, (u64, ResolvedVc<Code>)>,
pub deleted: FxIndexMap<ModuleId, u64>,
pub modified: FxIndexMap<ModuleId, ResolvedVc<Code>>,
}
pub(crate) async fn update_ecmascript_node_chunk(
content: Vc<EcmascriptBuildNodeChunkContent>,
from: &ReadRef<EcmascriptBuildNodeChunkVersion>,
) -> Result<EcmascriptNodeChunkUpdate> {
let to = content.own_version().await?;
// When to and from point to the same value we can skip comparing them.
if from.ptr_eq(&to) {
return Ok(EcmascriptNodeChunkUpdate::None);
}
let entries = content.entries().await?;
let mut added = FxIndexMap::default();
let mut modified = FxIndexMap::default();
let mut deleted = FxIndexMap::default();
for (id, from_hash) in &from.entries_hashes {
if let Some(entry) = entries.get(id) {
if *entry.hash.await? != *from_hash {
modified.insert(id.clone(), entry.code);
}
} else {
deleted.insert(id.clone(), *from_hash);
}
}
// Remaining entries are added
for (id, entry) in entries.iter() {
if !from.entries_hashes.contains_key(id) {
added.insert(id.clone(), (*entry.hash.await?, entry.code));
}
}
let update = if added.is_empty() && modified.is_empty() && deleted.is_empty() {
EcmascriptNodeChunkUpdate::None
} else {
EcmascriptNodeChunkUpdate::Partial(EcmascriptNodeChunkPartialUpdate {
added,
modified,
deleted,
})
};
Ok(update)
}
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ReadRef, Vc}; use turbo_tasks::{FxIndexMap, ReadRef, Vc};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_hex}; use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_hex};
use turbopack_core::{chunk::MinifyType, version::Version}; use turbopack_core::{
chunk::{MinifyType, ModuleId},
version::Version,
};
use turbopack_ecmascript::chunk::{CodeAndIds, EcmascriptChunkContent}; use turbopack_ecmascript::chunk::{CodeAndIds, EcmascriptChunkContent};
use super::content_entry::EcmascriptBuildNodeChunkContentEntries;
#[turbo_tasks::value(serialization = "none")] #[turbo_tasks::value(serialization = "none")]
pub(super) struct EcmascriptBuildNodeChunkVersion { pub(crate) struct EcmascriptBuildNodeChunkVersion {
chunk_path: String, pub(crate) chunk_path: String,
pub(crate) entries_hashes: FxIndexMap<ModuleId, u64>,
chunk_items: Vec<ReadRef<CodeAndIds>>, chunk_items: Vec<ReadRef<CodeAndIds>>,
minify_type: MinifyType, minify_type: MinifyType,
} }
...@@ -30,8 +36,18 @@ impl EcmascriptBuildNodeChunkVersion { ...@@ -30,8 +36,18 @@ impl EcmascriptBuildNodeChunkVersion {
bail!("chunk path {chunk_path} is not in client root {output_root}"); bail!("chunk path {chunk_path} is not in client root {output_root}");
}; };
let chunk_items = content.await?.chunk_item_code_and_ids().await?; let chunk_items = content.await?.chunk_item_code_and_ids().await?;
// Build entries_hashes for HMR update diffing
let entries = EcmascriptBuildNodeChunkContentEntries::new(content).await?;
let mut entries_hashes =
FxIndexMap::with_capacity_and_hasher(entries.len(), Default::default());
for (id, entry) in entries.iter() {
entries_hashes.insert(id.clone(), *entry.hash.await?);
}
Ok(EcmascriptBuildNodeChunkVersion { Ok(EcmascriptBuildNodeChunkVersion {
chunk_path: chunk_path.to_string(), chunk_path: chunk_path.to_string(),
entries_hashes,
chunk_items, chunk_items,
minify_type, minify_type,
} }
......
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