Commit 95e991e0 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig

Use correct ChunkGroups for chunking

parent 6d82715d
...@@ -1328,16 +1328,31 @@ impl AppEndpoint { ...@@ -1328,16 +1328,31 @@ impl AppEndpoint {
*client_chunking_context, *client_chunking_context,
availability_info, availability_info,
ssr_chunking_context.map(|ctx| *ctx), ssr_chunking_context.map(|ctx| *ctx),
if let Some(rsc_edge_inner) = app_entry.rsc_edge_inner {
ChunkGroup::Async(rsc_edge_inner)
} else {
ChunkGroup::Entry([app_entry.rsc_entry].into_iter().collect())
},
project.project_path(),
) )
.to_resolved() .to_resolved()
.await?; .await?;
let client_references_chunks_ref = client_references_chunks.await?; let client_references_chunks_ref = client_references_chunks.await?;
for &assets in client_references_chunks_ref for assets in client_references_chunks_ref
.layout_segment_client_chunks .layout_segment_client_chunks
.values() .values()
{ {
client_assets.extend(assets.all_assets().await?.iter().copied()); client_assets.extend(
assets
.iter()
.map(|a| a.all_assets())
.try_join()
.await?
.iter()
.flatten()
.copied(),
);
} }
for &assets in client_references_chunks_ref for &assets in client_references_chunks_ref
.client_component_client_chunks .client_component_client_chunks
...@@ -1790,7 +1805,7 @@ impl AppEndpoint { ...@@ -1790,7 +1805,7 @@ impl AppEndpoint {
let chunk_group2_assets = chunking_context.evaluated_chunk_group_assets( let chunk_group2_assets = chunking_context.evaluated_chunk_group_assets(
app_entry.rsc_entry.ident(), app_entry.rsc_entry.ident(),
ChunkGroup::Entry(vec![app_entry.rsc_entry]), ChunkGroup::Entry([app_entry.rsc_entry].into_iter().collect()),
module_graph, module_graph,
chunk_group1.await?.availability_info, chunk_group1.await?.availability_info,
); );
...@@ -1803,7 +1818,8 @@ impl AppEndpoint { ...@@ -1803,7 +1818,8 @@ impl AppEndpoint {
async { async {
let mut current_chunk_group = ChunkGroupResult::empty_resolved(); let mut current_chunk_group = ChunkGroupResult::empty_resolved();
let entry_chunk_group = ChunkGroup::Entry(vec![app_entry.rsc_entry]); let entry_chunk_group =
ChunkGroup::Entry([app_entry.rsc_entry].into_iter().collect());
let chunk_group_info = module_graph.chunk_group_info(); let chunk_group_info = module_graph.chunk_group_info();
...@@ -1822,7 +1838,9 @@ impl AppEndpoint { ...@@ -1822,7 +1838,9 @@ impl AppEndpoint {
.iter() .iter()
.map(async |m| Ok(ResolvedVc::upcast(m.await?.module))) .map(async |m| Ok(ResolvedVc::upcast(m.await?.module)))
.try_join() .try_join()
.await?; .await?
.into_iter()
.collect();
let chunk_group = chunking_context let chunk_group = chunking_context
.chunk_group( .chunk_group(
AssetIdent::from_path( AssetIdent::from_path(
...@@ -1832,7 +1850,7 @@ impl AppEndpoint { ...@@ -1832,7 +1850,7 @@ impl AppEndpoint {
ChunkGroup::SharedMerged { ChunkGroup::SharedMerged {
merge_tag: NEXT_SERVER_UTILITY_MERGE_TAG.clone(), merge_tag: NEXT_SERVER_UTILITY_MERGE_TAG.clone(),
entries: server_utils, entries: server_utils,
parent: parent_chunk_group, parent: parent_chunk_group as usize,
}, },
module_graph, module_graph,
AvailabilityInfo::root(), AvailabilityInfo::root(),
......
...@@ -105,7 +105,7 @@ impl InstrumentationEndpoint { ...@@ -105,7 +105,7 @@ impl InstrumentationEndpoint {
let edge_chunking_context = this.project.edge_chunking_context(false); let edge_chunking_context = this.project.edge_chunking_context(false);
Ok(edge_chunking_context.evaluated_chunk_group_assets( Ok(edge_chunking_context.evaluated_chunk_group_assets(
module.ident(), module.ident(),
ChunkGroup::Entry(vec![module]), ChunkGroup::Entry([module].into_iter().collect()),
module_graph, module_graph,
AvailabilityInfo::root(), AvailabilityInfo::root(),
)) ))
...@@ -126,7 +126,7 @@ impl InstrumentationEndpoint { ...@@ -126,7 +126,7 @@ impl InstrumentationEndpoint {
.node_root() .node_root()
.await? .await?
.join("server/instrumentation.js")?, .join("server/instrumentation.js")?,
ChunkGroup::Entry(vec![userland_module]), ChunkGroup::Entry([userland_module].into_iter().collect()),
module_graph, module_graph,
OutputAssets::empty(), OutputAssets::empty(),
OutputAssets::empty(), OutputAssets::empty(),
......
...@@ -115,7 +115,7 @@ impl MiddlewareEndpoint { ...@@ -115,7 +115,7 @@ impl MiddlewareEndpoint {
let edge_chunking_context = this.project.edge_chunking_context(false); let edge_chunking_context = this.project.edge_chunking_context(false);
let edge_chunk_grou = edge_chunking_context.evaluated_chunk_group_assets( let edge_chunk_grou = edge_chunking_context.evaluated_chunk_group_assets(
module.ident(), module.ident(),
ChunkGroup::Entry(vec![module]), ChunkGroup::Entry([module].into_iter().collect()),
module_graph, module_graph,
AvailabilityInfo::root(), AvailabilityInfo::root(),
); );
...@@ -137,7 +137,7 @@ impl MiddlewareEndpoint { ...@@ -137,7 +137,7 @@ impl MiddlewareEndpoint {
.node_root() .node_root()
.await? .await?
.join("server/middleware.js")?, .join("server/middleware.js")?,
ChunkGroup::Entry(vec![userland_module]), ChunkGroup::Entry([userland_module].into_iter().collect()),
module_graph, module_graph,
OutputAssets::empty(), OutputAssets::empty(),
OutputAssets::empty(), OutputAssets::empty(),
......
...@@ -1022,7 +1022,7 @@ impl PageEndpoint { ...@@ -1022,7 +1022,7 @@ impl PageEndpoint {
if is_edge { if is_edge {
let chunk_assets = edge_chunking_context.evaluated_chunk_group_assets( let chunk_assets = edge_chunking_context.evaluated_chunk_group_assets(
ssr_module.ident(), ssr_module.ident(),
ChunkGroup::Entry(vec![ssr_module]), ChunkGroup::Entry([ssr_module].into_iter().collect()),
ssr_module_graph, ssr_module_graph,
current_chunk_group.await?.availability_info, current_chunk_group.await?.availability_info,
); );
...@@ -1050,7 +1050,7 @@ impl PageEndpoint { ...@@ -1050,7 +1050,7 @@ impl PageEndpoint {
let ssr_entry_chunk = node_chunking_context let ssr_entry_chunk = node_chunking_context
.entry_chunk_group_asset( .entry_chunk_group_asset(
ssr_entry_chunk_path, ssr_entry_chunk_path,
ChunkGroup::Entry(vec![ssr_module]), ChunkGroup::Entry([ssr_module].into_iter().collect()),
ssr_module_graph, ssr_module_graph,
current_chunk_group.primary_assets(), current_chunk_group.primary_assets(),
current_chunk_group.referenced_assets(), current_chunk_group.referenced_assets(),
......
...@@ -14,6 +14,8 @@ pub struct AppEntry { ...@@ -14,6 +14,8 @@ pub struct AppEntry {
pub original_name: RcStr, pub original_name: RcStr,
/// The RSC module asset for the route or page. /// The RSC module asset for the route or page.
pub rsc_entry: ResolvedVc<Box<dyn Module>>, pub rsc_entry: ResolvedVc<Box<dyn Module>>,
/// For edge, the module wrapped by the sync-ifying facade
pub rsc_edge_inner: Option<ResolvedVc<Box<dyn Module>>>,
/// The source code config for this entry. /// The source code config for this entry.
pub config: ResolvedVc<NextSegmentConfig>, pub config: ResolvedVc<NextSegmentConfig>,
} }
...@@ -101,27 +101,41 @@ pub async fn get_app_page_entry( ...@@ -101,27 +101,41 @@ pub async fn get_app_page_entry(
AssetContent::file(FileContent::Content(file).cell()), AssetContent::file(FileContent::Content(file).cell()),
); );
let mut rsc_entry = module_asset_context let rsc_entry = module_asset_context
.process( .process(
Vc::upcast(source), Vc::upcast(source),
ReferenceType::Internal(ResolvedVc::cell(inner_assets)), ReferenceType::Internal(ResolvedVc::cell(inner_assets)),
) )
.module(); .module();
if is_edge { let (rsc_entry, rsc_edge_inner) = if is_edge {
rsc_entry = wrap_edge_page( let rsc_entry = wrap_edge_page(
*ResolvedVc::upcast(module_asset_context), *ResolvedVc::upcast(module_asset_context),
project_root.clone(), project_root.clone(),
rsc_entry, rsc_entry,
page, page.clone(),
next_config, next_config,
); )
.to_resolved()
.await?;
(
wrap_edge_entry(
*ResolvedVc::upcast(module_asset_context),
project_root,
*rsc_entry,
app_function_name(&page).into(),
),
Some(rsc_entry),
)
} else {
(rsc_entry, None)
}; };
Ok(AppEntry { Ok(AppEntry {
pathname, pathname,
original_name, original_name,
rsc_entry: rsc_entry.to_resolved().await?, rsc_entry: rsc_entry.to_resolved().await?,
rsc_edge_inner,
config: config.to_resolved().await?, config: config.to_resolved().await?,
} }
.cell()) .cell())
...@@ -213,10 +227,5 @@ async fn wrap_edge_page( ...@@ -213,10 +227,5 @@ async fn wrap_edge_page(
) )
.module(); .module();
Ok(wrap_edge_entry( Ok(wrapped)
asset_context,
project_root,
wrapped,
app_function_name(&page).into(),
))
} }
...@@ -101,27 +101,41 @@ pub async fn get_app_route_entry( ...@@ -101,27 +101,41 @@ pub async fn get_app_route_entry(
inner => userland_module inner => userland_module
}; };
let mut rsc_entry = module_asset_context let rsc_entry = module_asset_context
.process( .process(
virtual_source, virtual_source,
ReferenceType::Internal(ResolvedVc::cell(inner_assets)), ReferenceType::Internal(ResolvedVc::cell(inner_assets)),
) )
.module(); .module();
if is_edge { let (rsc_entry, rsc_edge_inner) = if is_edge {
rsc_entry = wrap_edge_route( let rsc_entry = wrap_edge_route(
Vc::upcast(module_asset_context), Vc::upcast(module_asset_context),
project_root, project_root.clone(),
rsc_entry, rsc_entry,
page, page.clone(),
next_config, next_config,
); )
} .to_resolved()
.await?;
(
wrap_edge_entry(
Vc::upcast(module_asset_context),
project_root,
*rsc_entry,
app_function_name(&page).into(),
),
Some(rsc_entry),
)
} else {
(rsc_entry, None)
};
Ok(AppEntry { Ok(AppEntry {
pathname, pathname,
original_name, original_name,
rsc_entry: rsc_entry.to_resolved().await?, rsc_entry: rsc_entry.to_resolved().await?,
rsc_edge_inner,
config: config.to_resolved().await?, config: config.to_resolved().await?,
} }
.cell()) .cell())
...@@ -213,10 +227,5 @@ async fn wrap_edge_route( ...@@ -213,10 +227,5 @@ async fn wrap_edge_route(
) )
.module(); .module();
Ok(wrap_edge_entry( Ok(wrapped)
asset_context,
project_root.clone(),
wrapped,
app_function_name(&page).into(),
))
} }
...@@ -409,8 +409,16 @@ async fn build_manifest( ...@@ -409,8 +409,16 @@ async fn build_manifest(
} }
} }
// The server utility chunks are merged into the first layout segment
let mut server_utility_chunks = layout_segment_client_chunks.get(&None);
// per layout segment chunks need to be emitted into the manifest too // per layout segment chunks need to be emitted into the manifest too
for (server_component, client_assets) in layout_segment_client_chunks.iter() { for (server_component, client_assets) in layout_segment_client_chunks.iter() {
let Some(server_component) = server_component else {
// Handled via server_utility_chunks
continue;
};
// Use source_path() to get the original source path (e.g., page.mdx) instead of // Use source_path() to get the original source path (e.g., page.mdx) instead of
// server_path() which returns the transformed path (e.g., page.mdx.tsx). // server_path() which returns the transformed path (e.g., page.mdx.tsx).
// This ensures the manifest key matches what the LoaderTree stores and what // This ensures the manifest key matches what the LoaderTree stores and what
...@@ -430,9 +438,16 @@ async fn build_manifest( ...@@ -430,9 +438,16 @@ async fn build_manifest(
.entry(server_component_name) .entry(server_component_name)
.or_default(); .or_default();
let client_chunks = client_assets.primary_assets().await?; let client_chunks = client_assets
let client_chunks_with_path = .iter()
cached_chunk_paths(&mut client_chunk_path_cache, client_chunks.iter().copied()) .chain(server_utility_chunks.take().into_iter().flatten())
.map(|assets| assets.primary_assets())
.try_join()
.await?;
let client_chunks_with_path = cached_chunk_paths(
&mut client_chunk_path_cache,
client_chunks.iter().flatten().copied(),
)
.await?; .await?;
// Inlining breaks HMR so it is always disabled in dev. // Inlining breaks HMR so it is always disabled in dev.
let inlined_css = *next_config.inline_css().await? && mode.is_production(); let inlined_css = *next_config.inline_css().await? && mode.is_production();
......
use std::time::Duration; use std::{
hash::{BuildHasher, Hash},
ops::{Deref, DerefMut},
time::Duration,
};
use anyhow::Result;
use bincode::{ use bincode::{
Decode, Encode, Decode, Encode,
de::Decoder, de::Decoder,
enc::Encoder, enc::Encoder,
error::{DecodeError, EncodeError}, error::{DecodeError, EncodeError},
}; };
use rustc_hash::FxBuildHasher;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks_macros::primitive as __turbo_tasks_internal_primitive; use turbo_tasks_macros::{TraceRawVcs, primitive as __turbo_tasks_internal_primitive};
use crate::{ use crate::{
self as turbo_tasks, Vc, self as turbo_tasks, FxIndexSet, NonLocalValue, TaskInput, Vc,
value_type::{ManualDecodeWrapper, ManualEncodeWrapper}, value_type::{ManualDecodeWrapper, ManualEncodeWrapper},
}; };
...@@ -75,3 +81,72 @@ impl<Context> Decode<Context> for JsonValueDecodeWrapper { ...@@ -75,3 +81,72 @@ impl<Context> Decode<Context> for JsonValueDecodeWrapper {
Ok(Self(turbo_bincode::serde_self_describing::decode(decoder)?)) Ok(Self(turbo_bincode::serde_self_describing::decode(decoder)?))
} }
} }
/// An IndexSet with a Hash implementation that is order-independent (and just like IndexSet,
/// equality is also order-independent).
#[derive(Clone, Debug, PartialEq, Eq, Decode, Encode, TraceRawVcs)]
pub struct HashableIndexSet<T: Hash + Eq + Decode<()> + Encode>(
#[bincode(with = "turbo_bincode::indexset")] pub FxIndexSet<T>,
);
impl<T: Hash + Eq + Decode<()> + Encode> From<FxIndexSet<T>> for HashableIndexSet<T> {
fn from(set: FxIndexSet<T>) -> Self {
HashableIndexSet(set)
}
}
impl<T: Hash + Eq + Decode<()> + Encode> FromIterator<T> for HashableIndexSet<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
HashableIndexSet(iter.into_iter().collect())
}
}
impl<T: Hash + Eq + Decode<()> + Encode> Deref for HashableIndexSet<T> {
type Target = FxIndexSet<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: Hash + Eq + Decode<()> + Encode> DerefMut for HashableIndexSet<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T: Hash + Eq + Decode<()> + Encode> Hash for HashableIndexSet<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let mut result = 0u64;
for item in self.iter() {
let item_hash = FxBuildHasher.hash_one(item);
result ^= item_hash;
}
state.write_u64(result);
}
}
impl<T> TaskInput for HashableIndexSet<T>
where
T: TaskInput,
{
fn is_resolved(&self) -> bool {
self.iter().all(TaskInput::is_resolved)
}
fn is_transient(&self) -> bool {
self.iter().any(TaskInput::is_transient)
}
async fn resolve_input(&self) -> Result<Self> {
let mut resolved = FxIndexSet::with_capacity_and_hasher(self.len(), Default::default());
for value in self.iter() {
resolved.insert(value.resolve_input().await?);
}
Ok(HashableIndexSet(resolved))
}
}
unsafe impl<T: NonLocalValue + Hash + Eq + Decode<()> + Encode> NonLocalValue
for HashableIndexSet<T>
{
}
...@@ -476,7 +476,11 @@ async fn build_internal( ...@@ -476,7 +476,11 @@ async fn build_internal(
.unwrap(), .unwrap(),
)? )?
.with_extension("entry.js"), .with_extension("entry.js"),
ChunkGroup::Entry(vec![ResolvedVc::upcast(ecmascript)]), ChunkGroup::Entry(
[ResolvedVc::upcast(ecmascript)]
.into_iter()
.collect(),
),
module_graph, module_graph,
OutputAssets::empty(), OutputAssets::empty(),
OutputAssets::empty(), OutputAssets::empty(),
......
...@@ -170,7 +170,9 @@ impl DevHtmlAsset { ...@@ -170,7 +170,9 @@ impl DevHtmlAsset {
chunking_context chunking_context
.root_chunk_group_assets( .root_chunk_group_assets(
chunkable_module.ident(), chunkable_module.ident(),
ChunkGroup::Entry(vec![ResolvedVc::upcast(chunkable_module)]), ChunkGroup::Entry(
[ResolvedVc::upcast(chunkable_module)].into_iter().collect(),
),
*module_graph, *module_graph,
) )
.await? .await?
......
...@@ -726,7 +726,7 @@ impl EvaluateContext for WebpackLoaderContext { ...@@ -726,7 +726,7 @@ impl EvaluateContext for WebpackLoaderContext {
let bootstrap = self.chunking_context.root_entry_chunk_group_asset( let bootstrap = self.chunking_context.root_entry_chunk_group_asset(
entry_path.clone(), entry_path.clone(),
ChunkGroup::Entry(vec![ResolvedVc::upcast(evaluatable)]), ChunkGroup::Entry([ResolvedVc::upcast(evaluatable)].into_iter().collect()),
*import_module_graph, *import_module_graph,
OutputAssets::empty(), OutputAssets::empty(),
OutputAssets::empty(), OutputAssets::empty(),
......
...@@ -603,7 +603,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> { ...@@ -603,7 +603,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
chunk_root_path chunk_root_path
.join(entry_module.ident().path().await?.file_stem().unwrap())? .join(entry_module.ident().path().await?.file_stem().unwrap())?
.with_extension("entry.js"), .with_extension("entry.js"),
ChunkGroup::Entry(entry_modules), ChunkGroup::Entry(entry_modules.into_iter().collect()),
module_graph, module_graph,
OutputAssets::empty(), OutputAssets::empty(),
OutputAssets::empty(), OutputAssets::empty(),
......
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