Skip to content

Commit 801e06f

Browse files
authored
Turbopack: accept ChunkGroup in chunking context (#76242)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent 8624a4c commit 801e06f

File tree

13 files changed

+223
-462
lines changed

13 files changed

+223
-462
lines changed

crates/next-api/src/app.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ use turbopack::{
5353
use turbopack_core::{
5454
asset::AssetContent,
5555
chunk::{
56-
availability_info::AvailabilityInfo, ChunkGroupType, ChunkableModules, ChunkingContext,
57-
ChunkingContextExt, EvaluatableAsset, EvaluatableAssets,
56+
availability_info::AvailabilityInfo, ChunkGroupType, ChunkingContext, ChunkingContextExt,
57+
EvaluatableAsset, EvaluatableAssets,
5858
},
5959
file_source::FileSource,
6060
ident::AssetIdent,
6161
module::Module,
62-
module_graph::{GraphEntries, ModuleGraph, SingleModuleGraph, VisitedModules},
62+
module_graph::{
63+
chunk_group_info::ChunkGroup, GraphEntries, ModuleGraph, SingleModuleGraph, VisitedModules,
64+
},
6365
output::{OutputAsset, OutputAssets},
6466
raw_output::RawOutput,
6567
reference_type::{CssReferenceSubType, ReferenceType},
@@ -1668,14 +1670,18 @@ impl AppEndpoint {
16681670
let server_utils = client_references
16691671
.server_utils
16701672
.iter()
1671-
.map(async |m| Ok(Vc::upcast(*m.await?.module)))
1673+
.map(async |m| Ok(ResolvedVc::upcast(m.await?.module)))
16721674
.try_join()
16731675
.await?;
16741676
let chunk_group = chunking_context
1675-
.chunk_group_multiple(
1677+
.chunk_group(
16761678
AssetIdent::from_path(this.app_project.project().project_path())
16771679
.with_modifier(server_utils_modifier()),
1678-
ChunkableModules::interned(server_utils),
1680+
// TODO this should be ChunkGroup::Shared
1681+
ChunkGroup::Entry {
1682+
entries: server_utils,
1683+
ty: ChunkGroupType::Entry,
1684+
},
16791685
module_graph,
16801686
Value::new(current_availability_info),
16811687
)
@@ -1710,7 +1716,13 @@ impl AppEndpoint {
17101716
let chunk_group = chunking_context
17111717
.chunk_group(
17121718
server_component.ident(),
1713-
Vc::upcast(*server_component.await?.module),
1719+
// TODO this should be ChunkGroup::Shared
1720+
ChunkGroup::Entry {
1721+
entries: vec![ResolvedVc::upcast(
1722+
server_component.await?.module,
1723+
)],
1724+
ty: ChunkGroupType::Entry,
1725+
},
17141726
module_graph,
17151727
Value::new(current_availability_info),
17161728
)

crates/next-core/src/next_app/app_client_references_chunks.rs

Lines changed: 116 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ use turbo_tasks::{
55
FxIndexMap, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Value, ValueToString, Vc,
66
};
77
use turbopack_core::{
8-
chunk::{
9-
availability_info::AvailabilityInfo, ChunkableModules, ChunkingContext, ChunkingContextExt,
10-
},
8+
chunk::{availability_info::AvailabilityInfo, ChunkingContext},
119
module::Module,
12-
module_graph::ModuleGraph,
10+
module_graph::{chunk_group_info::ChunkGroup, ModuleGraph},
1311
output::OutputAssets,
1412
};
1513

1614
use crate::{
1715
next_client_reference::{
18-
visit_client_reference::ClientReferenceGraphResult, ClientReferenceType,
16+
ecmascript_client_reference::ecmascript_client_reference_module::{
17+
ECMASCRIPT_CLIENT_REFERENCE_MERGE_TAG_CLIENT, ECMASCRIPT_CLIENT_REFERENCE_MERGE_TAG_SSR,
18+
},
19+
visit_client_reference::ClientReferenceGraphResult,
20+
ClientReferenceType,
1921
},
2022
next_server_component::server_component_module::NextServerComponentModule,
2123
};
@@ -57,91 +59,92 @@ pub async fn get_app_client_references_chunks(
5759
let separate_chunk_group_per_client_reference = false;
5860
let app_client_references = app_client_references.await?;
5961
if separate_chunk_group_per_client_reference {
60-
let app_client_references_chunks: Vec<(_, (_, Option<_>))> = app_client_references
61-
.client_references
62-
.iter()
63-
.map(|client_reference| async move {
64-
let client_reference_ty = client_reference.ty();
65-
Ok((
66-
client_reference_ty,
67-
match client_reference_ty {
68-
ClientReferenceType::EcmascriptClientReference(
69-
ecmascript_client_reference,
70-
) => {
71-
let ecmascript_client_reference_ref =
72-
ecmascript_client_reference.await?;
62+
todo!();
63+
// let app_client_references_chunks: Vec<(_, (_, Option<_>))> = app_client_references
64+
// .client_references
65+
// .iter()
66+
// .map(|client_reference| async move {
67+
// let client_reference_ty = client_reference.ty();
68+
// Ok((
69+
// client_reference_ty,
70+
// match client_reference_ty {
71+
// ClientReferenceType::EcmascriptClientReference(
72+
// ecmascript_client_reference,
73+
// ) => {
74+
// let ecmascript_client_reference_ref =
75+
// ecmascript_client_reference.await?;
7376

74-
let client_chunk_group = client_chunking_context
75-
.root_chunk_group(
76-
*ResolvedVc::upcast(
77-
ecmascript_client_reference_ref.client_module,
78-
),
79-
module_graph,
80-
)
81-
.await?;
82-
83-
(
84-
(
85-
client_chunk_group.assets,
86-
client_chunk_group.availability_info,
87-
),
88-
if let Some(ssr_chunking_context) = ssr_chunking_context {
89-
let ssr_chunk_group = ssr_chunking_context
90-
.root_chunk_group(
91-
*ResolvedVc::upcast(
92-
ecmascript_client_reference_ref.ssr_module,
93-
),
94-
module_graph,
95-
)
96-
.await?;
97-
98-
Some((
99-
ssr_chunk_group.assets,
100-
ssr_chunk_group.availability_info,
101-
))
102-
} else {
103-
None
104-
},
105-
)
106-
}
107-
ClientReferenceType::CssClientReference(css_client_reference) => {
108-
let client_chunk_group = client_chunking_context
109-
.root_chunk_group(
110-
*ResolvedVc::upcast(css_client_reference),
111-
module_graph,
112-
)
113-
.await?;
114-
115-
(
116-
(
117-
client_chunk_group.assets,
118-
client_chunk_group.availability_info,
119-
),
120-
None,
121-
)
122-
}
123-
},
124-
))
125-
})
126-
.try_join()
127-
.await?;
77+
// let client_chunk_group = client_chunking_context
78+
// .root_chunk_group(
79+
// module_graph,
80+
// *ResolvedVc::upcast(
81+
// ecmascript_client_reference_ref.client_module,
82+
// ),
83+
// )
84+
// .await?;
12885

129-
Ok(ClientReferencesChunks {
130-
client_component_client_chunks: app_client_references_chunks
131-
.iter()
132-
.map(|&(client_reference_ty, (client_chunks, _))| {
133-
(client_reference_ty, client_chunks)
134-
})
135-
.collect(),
136-
client_component_ssr_chunks: app_client_references_chunks
137-
.iter()
138-
.flat_map(|&(client_reference_ty, (_, ssr_chunks))| {
139-
ssr_chunks.map(|ssr_chunks| (client_reference_ty, ssr_chunks))
140-
})
141-
.collect(),
142-
layout_segment_client_chunks: FxIndexMap::default(),
143-
}
144-
.cell())
86+
// (
87+
// (
88+
// client_chunk_group.assets,
89+
// client_chunk_group.availability_info,
90+
// ),
91+
// if let Some(ssr_chunking_context) = ssr_chunking_context {
92+
// let ssr_chunk_group = ssr_chunking_context
93+
// .root_chunk_group(
94+
// *ResolvedVc::upcast(
95+
// ecmascript_client_reference_ref.ssr_module,
96+
// ),
97+
// module_graph,
98+
// )
99+
// .await?;
100+
101+
// Some((
102+
// ssr_chunk_group.assets,
103+
// ssr_chunk_group.availability_info,
104+
// ))
105+
// } else {
106+
// None
107+
// },
108+
// )
109+
// }
110+
// ClientReferenceType::CssClientReference(css_client_reference) => {
111+
// let client_chunk_group = client_chunking_context
112+
// .root_chunk_group(
113+
// *ResolvedVc::upcast(css_client_reference),
114+
// module_graph,
115+
// )
116+
// .await?;
117+
118+
// (
119+
// (
120+
// client_chunk_group.assets,
121+
// client_chunk_group.availability_info,
122+
// ),
123+
// None,
124+
// )
125+
// }
126+
// },
127+
// ))
128+
// })
129+
// .try_join()
130+
// .await?;
131+
132+
// Ok(ClientReferencesChunks {
133+
// client_component_client_chunks: app_client_references_chunks
134+
// .iter()
135+
// .map(|&(client_reference_ty, (client_chunks, _))| {
136+
// (client_reference_ty, client_chunks)
137+
// })
138+
// .collect(),
139+
// client_component_ssr_chunks: app_client_references_chunks
140+
// .iter()
141+
// .flat_map(|&(client_reference_ty, (_, ssr_chunks))| {
142+
// ssr_chunks.map(|ssr_chunks| (client_reference_ty, ssr_chunks))
143+
// })
144+
// .collect(),
145+
// layout_segment_client_chunks: FxIndexMap::default(),
146+
// }
147+
// .cell())
145148
} else {
146149
let mut client_references_by_server_component: FxIndexMap<_, Vec<_>> =
147150
FxIndexMap::default();
@@ -166,6 +169,8 @@ pub async fn get_app_client_references_chunks(
166169
list.extend(framework_reference_types);
167170
}
168171

172+
let chunk_group_info = module_graph.chunk_group_info();
173+
169174
let mut current_client_availability_info = client_availability_info.into_value();
170175
let mut current_client_chunks = OutputAssets::empty().to_resolved().await?;
171176
let mut current_ssr_availability_info = AvailabilityInfo::Root;
@@ -178,6 +183,12 @@ pub async fn get_app_client_references_chunks(
178183
for (server_component, client_reference_types) in
179184
client_references_by_server_component.into_iter()
180185
{
186+
let parent_chunk_group = *chunk_group_info
187+
.get_index_of(ChunkGroup::Shared(ResolvedVc::upcast(
188+
server_component.await?.module,
189+
)))
190+
.await?;
191+
181192
let base_ident = server_component.ident();
182193

183194
let server_path = server_component.server_path();
@@ -194,7 +205,7 @@ pub async fn get_app_client_references_chunks(
194205
let ecmascript_client_reference_ref =
195206
ecmascript_client_reference.await?;
196207

197-
Some(*ResolvedVc::upcast(
208+
Some(ResolvedVc::upcast(
198209
ecmascript_client_reference_ref.ssr_module,
199210
))
200211
}
@@ -212,9 +223,13 @@ pub async fn get_app_client_references_chunks(
212223
)
213224
.entered();
214225

215-
ssr_chunking_context.chunk_group_multiple(
226+
ssr_chunking_context.chunk_group(
216227
base_ident.with_modifier(ssr_modules_modifier()),
217-
ChunkableModules::interned(ssr_modules),
228+
ChunkGroup::IsolatedMerged {
229+
parent: parent_chunk_group,
230+
merge_tag: ECMASCRIPT_CLIENT_REFERENCE_MERGE_TAG_SSR.clone(),
231+
entries: ssr_modules,
232+
},
218233
module_graph,
219234
Value::new(current_ssr_availability_info),
220235
)
@@ -229,11 +244,11 @@ pub async fn get_app_client_references_chunks(
229244
Ok(match client_reference_ty {
230245
ClientReferenceType::EcmascriptClientReference(
231246
ecmascript_client_reference,
232-
) => *ResolvedVc::upcast(
233-
ecmascript_client_reference.await?.client_module,
234-
),
247+
) => {
248+
ResolvedVc::upcast(ecmascript_client_reference.await?.client_module)
249+
}
235250
ClientReferenceType::CssClientReference(css_client_reference) => {
236-
*ResolvedVc::upcast(*css_client_reference)
251+
ResolvedVc::upcast(*css_client_reference)
237252
}
238253
})
239254
})
@@ -246,9 +261,13 @@ pub async fn get_app_client_references_chunks(
246261
)
247262
.entered();
248263

249-
Some(client_chunking_context.chunk_group_multiple(
264+
Some(client_chunking_context.chunk_group(
250265
base_ident.with_modifier(client_modules_modifier()),
251-
ChunkableModules::interned(client_modules),
266+
ChunkGroup::IsolatedMerged {
267+
parent: parent_chunk_group,
268+
merge_tag: ECMASCRIPT_CLIENT_REFERENCE_MERGE_TAG_CLIENT.clone(),
269+
entries: client_modules,
270+
},
252271
module_graph,
253272
Value::new(current_client_availability_info),
254273
))

crates/next-core/src/next_client_reference/ecmascript_client_reference/ecmascript_client_reference_module.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{io::Write, iter::once};
22

33
use anyhow::{bail, Context, Result};
44
use indoc::writedoc;
5+
use once_cell::sync::Lazy;
56
use turbo_rcstr::RcStr;
67
use turbo_tasks::{ResolvedVc, Value, ValueToString, Vc};
78
use turbo_tasks_fs::File;
@@ -191,6 +192,10 @@ fn ecmascript_client_reference_ssr_ref_modifier() -> Vc<RcStr> {
191192
Vc::cell("ecmascript client reference to ssr".into())
192193
}
193194

195+
pub static ECMASCRIPT_CLIENT_REFERENCE_MERGE_TAG_CLIENT: Lazy<RcStr> =
196+
Lazy::new(|| "client".into());
197+
pub static ECMASCRIPT_CLIENT_REFERENCE_MERGE_TAG_SSR: Lazy<RcStr> = Lazy::new(|| "ssr".into());
198+
194199
#[turbo_tasks::value_impl]
195200
impl Module for EcmascriptClientReferenceModule {
196201
#[turbo_tasks::function]
@@ -216,7 +221,7 @@ impl Module for EcmascriptClientReferenceModule {
216221
EcmascriptClientReference::new(
217222
*ResolvedVc::upcast(*client_module),
218223
ChunkGroupType::Evaluated,
219-
Some("client".into()),
224+
Some(ECMASCRIPT_CLIENT_REFERENCE_MERGE_TAG_CLIENT.clone()),
220225
ecmascript_client_reference_client_ref_modifier(),
221226
)
222227
.to_resolved()
@@ -226,7 +231,7 @@ impl Module for EcmascriptClientReferenceModule {
226231
EcmascriptClientReference::new(
227232
*ResolvedVc::upcast(*ssr_module),
228233
ChunkGroupType::Entry,
229-
Some("ssr".into()),
234+
Some(ECMASCRIPT_CLIENT_REFERENCE_MERGE_TAG_SSR.clone()),
230235
ecmascript_client_reference_ssr_ref_modifier(),
231236
)
232237
.to_resolved()

0 commit comments

Comments
 (0)