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

Use correct ChunkGroups for chunking

parent 6d82715d
......@@ -1328,16 +1328,31 @@ impl AppEndpoint {
*client_chunking_context,
availability_info,
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()
.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
.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
.client_component_client_chunks
......@@ -1790,7 +1805,7 @@ impl AppEndpoint {
let chunk_group2_assets = chunking_context.evaluated_chunk_group_assets(
app_entry.rsc_entry.ident(),
ChunkGroup::Entry(vec![app_entry.rsc_entry]),
ChunkGroup::Entry([app_entry.rsc_entry].into_iter().collect()),
module_graph,
chunk_group1.await?.availability_info,
);
......@@ -1803,7 +1818,8 @@ impl AppEndpoint {
async {
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();
......@@ -1822,7 +1838,9 @@ impl AppEndpoint {
.iter()
.map(async |m| Ok(ResolvedVc::upcast(m.await?.module)))
.try_join()
.await?;
.await?
.into_iter()
.collect();
let chunk_group = chunking_context
.chunk_group(
AssetIdent::from_path(
......@@ -1832,7 +1850,7 @@ impl AppEndpoint {
ChunkGroup::SharedMerged {
merge_tag: NEXT_SERVER_UTILITY_MERGE_TAG.clone(),
entries: server_utils,
parent: parent_chunk_group,
parent: parent_chunk_group as usize,
},
module_graph,
AvailabilityInfo::root(),
......
......@@ -105,7 +105,7 @@ impl InstrumentationEndpoint {
let edge_chunking_context = this.project.edge_chunking_context(false);
Ok(edge_chunking_context.evaluated_chunk_group_assets(
module.ident(),
ChunkGroup::Entry(vec![module]),
ChunkGroup::Entry([module].into_iter().collect()),
module_graph,
AvailabilityInfo::root(),
))
......@@ -126,7 +126,7 @@ impl InstrumentationEndpoint {
.node_root()
.await?
.join("server/instrumentation.js")?,
ChunkGroup::Entry(vec![userland_module]),
ChunkGroup::Entry([userland_module].into_iter().collect()),
module_graph,
OutputAssets::empty(),
OutputAssets::empty(),
......
......@@ -115,7 +115,7 @@ impl MiddlewareEndpoint {
let edge_chunking_context = this.project.edge_chunking_context(false);
let edge_chunk_grou = edge_chunking_context.evaluated_chunk_group_assets(
module.ident(),
ChunkGroup::Entry(vec![module]),
ChunkGroup::Entry([module].into_iter().collect()),
module_graph,
AvailabilityInfo::root(),
);
......@@ -137,7 +137,7 @@ impl MiddlewareEndpoint {
.node_root()
.await?
.join("server/middleware.js")?,
ChunkGroup::Entry(vec![userland_module]),
ChunkGroup::Entry([userland_module].into_iter().collect()),
module_graph,
OutputAssets::empty(),
OutputAssets::empty(),
......
......@@ -1022,7 +1022,7 @@ impl PageEndpoint {
if is_edge {
let chunk_assets = edge_chunking_context.evaluated_chunk_group_assets(
ssr_module.ident(),
ChunkGroup::Entry(vec![ssr_module]),
ChunkGroup::Entry([ssr_module].into_iter().collect()),
ssr_module_graph,
current_chunk_group.await?.availability_info,
);
......@@ -1050,7 +1050,7 @@ impl PageEndpoint {
let ssr_entry_chunk = node_chunking_context
.entry_chunk_group_asset(
ssr_entry_chunk_path,
ChunkGroup::Entry(vec![ssr_module]),
ChunkGroup::Entry([ssr_module].into_iter().collect()),
ssr_module_graph,
current_chunk_group.primary_assets(),
current_chunk_group.referenced_assets(),
......
......@@ -14,6 +14,8 @@ pub struct AppEntry {
pub original_name: RcStr,
/// The RSC module asset for the route or page.
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.
pub config: ResolvedVc<NextSegmentConfig>,
}
......@@ -101,27 +101,41 @@ pub async fn get_app_page_entry(
AssetContent::file(FileContent::Content(file).cell()),
);
let mut rsc_entry = module_asset_context
let rsc_entry = module_asset_context
.process(
Vc::upcast(source),
ReferenceType::Internal(ResolvedVc::cell(inner_assets)),
)
.module();
if is_edge {
rsc_entry = wrap_edge_page(
let (rsc_entry, rsc_edge_inner) = if is_edge {
let rsc_entry = wrap_edge_page(
*ResolvedVc::upcast(module_asset_context),
project_root.clone(),
rsc_entry,
page,
page.clone(),
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 {
pathname,
original_name,
rsc_entry: rsc_entry.to_resolved().await?,
rsc_edge_inner,
config: config.to_resolved().await?,
}
.cell())
......@@ -213,10 +227,5 @@ async fn wrap_edge_page(
)
.module();
Ok(wrap_edge_entry(
asset_context,
project_root,
wrapped,
app_function_name(&page).into(),
))
Ok(wrapped)
}
......@@ -101,27 +101,41 @@ pub async fn get_app_route_entry(
inner => userland_module
};
let mut rsc_entry = module_asset_context
let rsc_entry = module_asset_context
.process(
virtual_source,
ReferenceType::Internal(ResolvedVc::cell(inner_assets)),
)
.module();
if is_edge {
rsc_entry = wrap_edge_route(
let (rsc_entry, rsc_edge_inner) = if is_edge {
let rsc_entry = wrap_edge_route(
Vc::upcast(module_asset_context),
project_root,
project_root.clone(),
rsc_entry,
page,
page.clone(),
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 {
pathname,
original_name,
rsc_entry: rsc_entry.to_resolved().await?,
rsc_edge_inner,
config: config.to_resolved().await?,
}
.cell())
......@@ -213,10 +227,5 @@ async fn wrap_edge_route(
)
.module();
Ok(wrap_edge_entry(
asset_context,
project_root.clone(),
wrapped,
app_function_name(&page).into(),
))
Ok(wrapped)
}
......@@ -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
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
// server_path() which returns the transformed path (e.g., page.mdx.tsx).
// This ensures the manifest key matches what the LoaderTree stores and what
......@@ -430,10 +438,17 @@ async fn build_manifest(
.entry(server_component_name)
.or_default();
let client_chunks = client_assets.primary_assets().await?;
let client_chunks_with_path =
cached_chunk_paths(&mut client_chunk_path_cache, client_chunks.iter().copied())
.await?;
let client_chunks = client_assets
.iter()
.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?;
// Inlining breaks HMR so it is always disabled in dev.
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::{
Decode, Encode,
de::Decoder,
enc::Encoder,
error::{DecodeError, EncodeError},
};
use rustc_hash::FxBuildHasher;
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::{
self as turbo_tasks, Vc,
self as turbo_tasks, FxIndexSet, NonLocalValue, TaskInput, Vc,
value_type::{ManualDecodeWrapper, ManualEncodeWrapper},
};
......@@ -75,3 +81,72 @@ impl<Context> Decode<Context> for JsonValueDecodeWrapper {
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(
.unwrap(),
)?
.with_extension("entry.js"),
ChunkGroup::Entry(vec![ResolvedVc::upcast(ecmascript)]),
ChunkGroup::Entry(
[ResolvedVc::upcast(ecmascript)]
.into_iter()
.collect(),
),
module_graph,
OutputAssets::empty(),
OutputAssets::empty(),
......
......@@ -170,7 +170,9 @@ impl DevHtmlAsset {
chunking_context
.root_chunk_group_assets(
chunkable_module.ident(),
ChunkGroup::Entry(vec![ResolvedVc::upcast(chunkable_module)]),
ChunkGroup::Entry(
[ResolvedVc::upcast(chunkable_module)].into_iter().collect(),
),
*module_graph,
)
.await?
......
......@@ -726,7 +726,7 @@ impl EvaluateContext for WebpackLoaderContext {
let bootstrap = self.chunking_context.root_entry_chunk_group_asset(
entry_path.clone(),
ChunkGroup::Entry(vec![ResolvedVc::upcast(evaluatable)]),
ChunkGroup::Entry([ResolvedVc::upcast(evaluatable)].into_iter().collect()),
*import_module_graph,
OutputAssets::empty(),
OutputAssets::empty(),
......
......@@ -603,7 +603,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
chunk_root_path
.join(entry_module.ident().path().await?.file_stem().unwrap())?
.with_extension("entry.js"),
ChunkGroup::Entry(entry_modules),
ChunkGroup::Entry(entry_modules.into_iter().collect()),
module_graph,
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