Skip to content

Commit b04cc97

Browse files
authored
Turbopack: align chunking with graph entries (#76441)
Shouldn't have any change in behavior, but prepares for #76211
1 parent 0c96be2 commit b04cc97

File tree

16 files changed

+242
-175
lines changed

16 files changed

+242
-175
lines changed

crates/next-api/src/app.rs

+43-37
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use next_core::{
3131
get_server_module_options_context, get_server_resolve_options_context,
3232
get_server_runtime_entries, ServerContextType,
3333
},
34-
next_server_utility::NextServerUtilityTransition,
34+
next_server_utility::{NextServerUtilityTransition, NEXT_SERVER_UTILITY_MERGE_TAG},
3535
parse_segment_config_from_source,
3636
util::NextRuntime,
3737
};
@@ -53,14 +53,15 @@ use turbopack::{
5353
use turbopack_core::{
5454
asset::AssetContent,
5555
chunk::{
56-
availability_info::AvailabilityInfo, ChunkGroupType, ChunkingContext, ChunkingContextExt,
56+
availability_info::AvailabilityInfo, ChunkGroupResult, ChunkingContext, ChunkingContextExt,
5757
EvaluatableAsset, EvaluatableAssets,
5858
},
5959
file_source::FileSource,
6060
ident::AssetIdent,
6161
module::Module,
6262
module_graph::{
63-
chunk_group_info::ChunkGroup, GraphEntries, ModuleGraph, SingleModuleGraph, VisitedModules,
63+
chunk_group_info::{ChunkGroup, ChunkGroupEntry},
64+
GraphEntries, ModuleGraph, SingleModuleGraph, VisitedModules,
6465
},
6566
output::{OutputAsset, OutputAssets},
6667
raw_output::RawOutput,
@@ -826,6 +827,9 @@ impl AppProject {
826827
// Implements layout segment optimization to compute a graph "chain" for each layout
827828
// segment
828829
async move {
830+
let rsc_entry_chunk_group =
831+
ChunkGroupEntry::Entry(vec![ResolvedVc::upcast(rsc_entry)]);
832+
829833
let mut graphs = vec![];
830834
let mut visited_modules = if has_layout_segments {
831835
let ServerEntries {
@@ -835,15 +839,16 @@ impl AppProject {
835839

836840
let graph = SingleModuleGraph::new_with_entries_visited_intern(
837841
vec![
838-
(
839-
server_utils
842+
ChunkGroupEntry::SharedMerged {
843+
parent: Box::new(rsc_entry_chunk_group.clone()),
844+
merge_tag: NEXT_SERVER_UTILITY_MERGE_TAG.clone(),
845+
entries: server_utils
840846
.iter()
841847
.map(async |m| Ok(ResolvedVc::upcast(m.await?.module)))
842848
.try_join()
843849
.await?,
844-
ChunkGroupType::Entry,
845-
),
846-
(client_shared_entries, ChunkGroupType::Evaluated),
850+
},
851+
ChunkGroupEntry::Entry(client_shared_entries),
847852
],
848853
VisitedModules::empty(),
849854
);
@@ -852,7 +857,9 @@ impl AppProject {
852857

853858
for module in server_component_entries.iter() {
854859
let graph = SingleModuleGraph::new_with_entries_visited_intern(
855-
vec![(vec![ResolvedVc::upcast(*module)], ChunkGroupType::Entry)],
860+
// This should really be ChunkGroupEntry::Shared(module.await?.module),
861+
// but that breaks everything for some reason.
862+
vec![ChunkGroupEntry::Entry(vec![ResolvedVc::upcast(*module)])],
856863
visited_modules,
857864
);
858865
graphs.push(graph);
@@ -873,15 +880,15 @@ impl AppProject {
873880
visited_modules
874881
} else {
875882
let graph = SingleModuleGraph::new_with_entries_visited_intern(
876-
vec![(client_shared_entries, ChunkGroupType::Evaluated)],
883+
vec![ChunkGroupEntry::Entry(client_shared_entries)],
877884
VisitedModules::empty(),
878885
);
879886
graphs.push(graph);
880887
VisitedModules::from_graph(graph)
881888
};
882889

883890
let graph = SingleModuleGraph::new_with_entries_visited_intern(
884-
vec![(vec![ResolvedVc::upcast(rsc_entry)], ChunkGroupType::Entry)],
891+
vec![rsc_entry_chunk_group],
885892
visited_modules,
886893
);
887894
graphs.push(graph);
@@ -1664,25 +1671,35 @@ impl AppEndpoint {
16641671

16651672
Ok(match runtime {
16661673
NextRuntime::Edge => {
1674+
let ChunkGroupResult {
1675+
assets,
1676+
availability_info,
1677+
} = *chunking_context
1678+
.evaluated_chunk_group(
1679+
server_action_manifest_loader.ident(),
1680+
Vc::cell(vec![server_action_manifest_loader]),
1681+
module_graph,
1682+
Value::new(AvailabilityInfo::Root),
1683+
)
1684+
.await?;
1685+
16671686
let mut evaluatable_assets =
16681687
this.app_project.edge_rsc_runtime_entries().owned().await?;
16691688
let evaluatable = ResolvedVc::try_sidecast(app_entry.rsc_entry)
16701689
.context("Entry module must be evaluatable")?;
16711690
evaluatable_assets.push(evaluatable);
1672-
evaluatable_assets.push(server_action_manifest_loader);
16731691

1674-
{
1675-
let _span = tracing::info_span!("Server Components");
1692+
assets.concatenate(
16761693
chunking_context
16771694
.evaluated_chunk_group_assets(
16781695
app_entry.rsc_entry.ident(),
16791696
Vc::cell(evaluatable_assets.clone()),
16801697
module_graph,
1681-
Value::new(AvailabilityInfo::Root),
1698+
Value::new(availability_info),
16821699
)
16831700
.resolve()
1684-
.await?
1685-
}
1701+
.await?,
1702+
)
16861703
}
16871704
NextRuntime::NodeJs => {
16881705
let mut evaluatable_assets = this.app_project.rsc_runtime_entries().owned().await?;
@@ -1712,10 +1729,7 @@ impl AppEndpoint {
17121729
AssetIdent::from_path(this.app_project.project().project_path())
17131730
.with_modifier(server_utils_modifier()),
17141731
// TODO this should be ChunkGroup::Shared
1715-
ChunkGroup::Entry {
1716-
entries: server_utils,
1717-
ty: ChunkGroupType::Entry,
1718-
},
1732+
ChunkGroup::Entry(server_utils),
17191733
module_graph,
17201734
Value::new(current_availability_info),
17211735
)
@@ -1751,12 +1765,9 @@ impl AppEndpoint {
17511765
.chunk_group(
17521766
server_component.ident(),
17531767
// TODO this should be ChunkGroup::Shared
1754-
ChunkGroup::Entry {
1755-
entries: vec![ResolvedVc::upcast(
1756-
server_component.await?.module,
1757-
)],
1758-
ty: ChunkGroupType::Entry,
1759-
},
1768+
ChunkGroup::Entry(vec![ResolvedVc::upcast(
1769+
server_component.await?.module,
1770+
)]),
17601771
module_graph,
17611772
Value::new(current_availability_info),
17621773
)
@@ -1930,19 +1941,15 @@ impl Endpoint for AppEndpoint {
19301941
async fn entries(self: Vc<Self>) -> Result<Vc<GraphEntries>> {
19311942
let this = self.await?;
19321943
Ok(Vc::cell(vec![
1933-
(
1934-
vec![self.app_endpoint_entry().await?.rsc_entry],
1935-
ChunkGroupType::Entry,
1936-
),
1937-
(
1944+
ChunkGroupEntry::Entry(vec![self.app_endpoint_entry().await?.rsc_entry]),
1945+
ChunkGroupEntry::Entry(
19381946
this.app_project
19391947
.client_runtime_entries()
19401948
.await?
19411949
.iter()
19421950
.copied()
19431951
.map(ResolvedVc::upcast)
19441952
.collect(),
1945-
ChunkGroupType::Entry,
19461953
),
19471954
]))
19481955
}
@@ -1983,10 +1990,9 @@ impl Endpoint for AppEndpoint {
19831990
.await?,
19841991
);
19851992

1986-
Ok(Vc::cell(vec![(
1987-
vec![server_actions_loader],
1988-
ChunkGroupType::Entry,
1989-
)]))
1993+
Ok(Vc::cell(vec![ChunkGroupEntry::Entry(vec![
1994+
server_actions_loader,
1995+
])]))
19901996
}
19911997
}
19921998

crates/next-api/src/instrumentation.rs

+10-20
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ use turbo_tasks_fs::{File, FileContent, FileSystemPath};
1212
use turbopack_core::{
1313
asset::AssetContent,
1414
chunk::{
15-
availability_info::AvailabilityInfo, ChunkGroupType, ChunkingContext, ChunkingContextExt,
15+
availability_info::AvailabilityInfo, ChunkingContext, ChunkingContextExt,
1616
EntryChunkGroupResult,
1717
},
1818
context::AssetContext,
1919
module::Module,
20-
module_graph::GraphEntries,
20+
module_graph::{chunk_group_info::ChunkGroupEntry, GraphEntries},
2121
output::{OutputAsset, OutputAssets},
2222
reference_type::{EntryReferenceSubType, ReferenceType},
2323
source::Source,
@@ -95,24 +95,13 @@ impl InstrumentationEndpoint {
9595
.cell())
9696
}
9797

98-
#[turbo_tasks::function]
99-
async fn entry_module(self: Vc<Self>) -> Result<Vc<Box<dyn Module>>> {
100-
if self.await?.is_edge {
101-
Ok(*self.core_modules().await?.edge_entry_module)
102-
} else {
103-
Ok(*self.core_modules().await?.userland_module)
104-
}
105-
}
106-
10798
#[turbo_tasks::function]
10899
async fn edge_files(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
109100
let this = self.await?;
110101

111102
let module = self.core_modules().await?.edge_entry_module;
112103

113-
let module_graph = this
114-
.project
115-
.module_graph(*module, ChunkGroupType::Evaluated);
104+
let module_graph = this.project.module_graph(*module);
116105

117106
let mut evaluatable_assets = get_server_runtime_entries(
118107
Value::new(ServerContextType::Instrumentation {
@@ -155,9 +144,7 @@ impl InstrumentationEndpoint {
155144
let chunking_context = this.project.server_chunking_context(false);
156145

157146
let userland_module = self.core_modules().await?.userland_module;
158-
let module_graph = this
159-
.project
160-
.module_graph(*userland_module, ChunkGroupType::Entry);
147+
let module_graph = this.project.module_graph(*userland_module);
161148

162149
let Some(module) = ResolvedVc::try_downcast(userland_module) else {
163150
bail!("Entry module must be evaluatable");
@@ -294,9 +281,12 @@ impl Endpoint for InstrumentationEndpoint {
294281
#[turbo_tasks::function]
295282
async fn entries(self: Vc<Self>) -> Result<Vc<GraphEntries>> {
296283
let core_modules = self.core_modules().await?;
297-
Ok(Vc::cell(vec![(
298-
vec![core_modules.edge_entry_module],
299-
ChunkGroupType::Evaluated,
284+
Ok(Vc::cell(vec![ChunkGroupEntry::Entry(
285+
if self.await?.is_edge {
286+
vec![core_modules.edge_entry_module]
287+
} else {
288+
vec![core_modules.userland_module]
289+
},
300290
)]))
301291
}
302292
}

crates/next-api/src/middleware.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ use turbo_tasks_fs::{self, File, FileContent, FileSystemPath};
1616
use turbopack_core::{
1717
asset::AssetContent,
1818
chunk::{
19-
availability_info::AvailabilityInfo, ChunkGroupType, ChunkingContext, ChunkingContextExt,
19+
availability_info::AvailabilityInfo, ChunkingContext, ChunkingContextExt,
2020
EntryChunkGroupResult, EvaluatableAsset,
2121
},
2222
context::AssetContext,
2323
module::Module,
24-
module_graph::GraphEntries,
24+
module_graph::{chunk_group_info::ChunkGroupEntry, GraphEntries},
2525
output::{OutputAsset, OutputAssets},
2626
reference_type::{EntryReferenceSubType, ReferenceType},
2727
source::Source,
@@ -128,9 +128,7 @@ impl MiddlewareEndpoint {
128128
evaluatable_assets.push(evaluatable.to_resolved().await?);
129129

130130
let evaluatable_assets = Vc::cell(evaluatable_assets);
131-
let module_graph = this
132-
.project
133-
.module_graph_for_entries(evaluatable_assets, ChunkGroupType::Evaluated);
131+
let module_graph = this.project.module_graph_for_entries(evaluatable_assets);
134132

135133
let edge_chunking_context = this.project.edge_chunking_context(false);
136134

@@ -150,9 +148,7 @@ impl MiddlewareEndpoint {
150148
let chunking_context = this.project.server_chunking_context(false);
151149

152150
let userland_module = self.entry_module().to_resolved().await?;
153-
let module_graph = this
154-
.project
155-
.module_graph(*userland_module, ChunkGroupType::Entry);
151+
let module_graph = this.project.module_graph(*userland_module);
156152

157153
let Some(module) = ResolvedVc::try_downcast(userland_module) else {
158154
bail!("Entry module must be evaluatable");
@@ -409,9 +405,8 @@ impl Endpoint for MiddlewareEndpoint {
409405

410406
#[turbo_tasks::function]
411407
async fn entries(self: Vc<Self>) -> Result<Vc<GraphEntries>> {
412-
Ok(Vc::cell(vec![(
413-
vec![self.entry_module().to_resolved().await?],
414-
ChunkGroupType::Evaluated,
415-
)]))
408+
Ok(Vc::cell(vec![ChunkGroupEntry::Entry(vec![
409+
self.entry_module().to_resolved().await?,
410+
])]))
416411
}
417412
}

crates/next-api/src/pages.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ use turbopack::{
4545
use turbopack_core::{
4646
asset::AssetContent,
4747
chunk::{
48-
availability_info::AvailabilityInfo, ChunkGroupResult, ChunkGroupType, ChunkingContext,
49-
ChunkingContextExt, EntryChunkGroupResult, EvaluatableAsset, EvaluatableAssets,
48+
availability_info::AvailabilityInfo, ChunkGroupResult, ChunkingContext, ChunkingContextExt,
49+
EntryChunkGroupResult, EvaluatableAsset, EvaluatableAssets,
5050
},
5151
context::AssetContext,
5252
file_source::FileSource,
5353
ident::AssetIdent,
5454
module::Module,
55-
module_graph::{GraphEntries, ModuleGraph},
55+
module_graph::{chunk_group_info::ChunkGroupEntry, GraphEntries, ModuleGraph},
5656
output::{OptionOutputAsset, OutputAsset, OutputAssets},
5757
reference_type::{EcmaScriptModulesReferenceSubType, EntryReferenceSubType, ReferenceType},
5858
resolve::{origin::PlainResolveOrigin, parse::Request, pattern::Pattern},
@@ -769,7 +769,7 @@ impl PageEndpoint {
769769
let this = self.await?;
770770
let project = this.pages_project.project();
771771
let evaluatable_assets = self.client_evaluatable_assets();
772-
Ok(project.module_graph_for_entries(evaluatable_assets, ChunkGroupType::Evaluated))
772+
Ok(project.module_graph_for_entries(evaluatable_assets))
773773
}
774774

775775
#[turbo_tasks::function]
@@ -909,7 +909,7 @@ impl PageEndpoint {
909909
// The SSR and Client Graphs are not connected in Pages Router.
910910
// We are only interested in get_next_dynamic_imports_for_endpoint at the
911911
// moment, which only needs the client graph anyway.
912-
let module_graph = project.module_graph(*ssr_module, ChunkGroupType::Entry);
912+
let module_graph = project.module_graph(*ssr_module);
913913

914914
let next_dynamic_imports = if let PageEndpointType::Html = this.ty {
915915
let client_availability_info = self.client_chunks().await?.availability_info;
@@ -1454,19 +1454,17 @@ impl Endpoint for PageEndpoint {
14541454
#[turbo_tasks::function]
14551455
async fn entries(self: Vc<Self>) -> Result<Vc<GraphEntries>> {
14561456
let this = self.await?;
1457-
let mut modules = vec![];
14581457

14591458
let ssr_chunk_module = self.internal_ssr_chunk_module().await?;
1460-
modules.push((vec![ssr_chunk_module.ssr_module], ChunkGroupType::Entry));
1459+
let mut modules = vec![ChunkGroupEntry::Entry(vec![ssr_chunk_module.ssr_module])];
14611460

14621461
if let PageEndpointType::Html = this.ty {
1463-
modules.push((
1462+
modules.push(ChunkGroupEntry::Entry(
14641463
self.client_evaluatable_assets()
14651464
.await?
14661465
.iter()
14671466
.map(|m| ResolvedVc::upcast(*m))
14681467
.collect(),
1469-
ChunkGroupType::Evaluated,
14701468
));
14711469
}
14721470

0 commit comments

Comments
 (0)