Skip to content

Commit 0c2e325

Browse files
committed
Turbopack: accept ChunkGroup in chunking context
1 parent 7076218 commit 0c2e325

File tree

13 files changed

+214
-458
lines changed

13 files changed

+214
-458
lines changed

crates/next-api/src/app.rs

Lines changed: 19 additions & 15 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, ChunkableModule, ChunkableModules,
57-
ChunkingContext, 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},
@@ -1661,20 +1663,17 @@ impl AppEndpoint {
16611663
let client_references = client_references.await?;
16621664
let span = tracing::trace_span!("server utils");
16631665
async {
1664-
let server_utils = client_references
1665-
.server_utils
1666-
.iter()
1667-
.map(|m| async move {
1668-
Ok(*ResolvedVc::try_downcast::<Box<dyn ChunkableModule>>(*m)
1669-
.context("Expected server utils to be chunkable")?)
1670-
})
1671-
.try_join()
1672-
.await?;
1666+
let server_utils = client_references.server_utils.clone();
16731667
let chunk_group = chunking_context
1674-
.chunk_group_multiple(
1668+
.chunk_group(
16751669
AssetIdent::from_path(this.app_project.project().project_path())
16761670
.with_modifier(server_utils_modifier()),
1677-
ChunkableModules::interned(server_utils),
1671+
// TODO this should be ChunkGroup::Shared
1672+
ChunkGroup::Entry {
1673+
entries: server_utils,
1674+
ty: ChunkGroupType::Entry,
1675+
}
1676+
.cell(),
16781677
module_graph,
16791678
Value::new(current_availability_info),
16801679
)
@@ -1709,7 +1708,12 @@ impl AppEndpoint {
17091708
let chunk_group = chunking_context
17101709
.chunk_group(
17111710
server_component.ident(),
1712-
*ResolvedVc::upcast(server_component),
1711+
// TODO this should be ChunkGroup::Shared
1712+
ChunkGroup::Entry {
1713+
entries: vec![ResolvedVc::upcast(server_component)],
1714+
ty: ChunkGroupType::Entry,
1715+
}
1716+
.cell(),
17131717
module_graph,
17141718
Value::new(current_availability_info),
17151719
)

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

Lines changed: 102 additions & 89 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?;
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?;
8285

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?;
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?;
97100

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?;
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?;
114117

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?;
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?;
128131

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())
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();
@@ -212,9 +215,14 @@ pub async fn get_app_client_references_chunks(
212215
)
213216
.entered();
214217

215-
ssr_chunking_context.chunk_group_multiple(
218+
ssr_chunking_context.chunk_group(
216219
base_ident.with_modifier(ssr_modules_modifier()),
217-
ChunkableModules::interned(ssr_modules),
220+
// TODO use correct parameters here, and sort the modules?
221+
ChunkGroup::isolated_merged_interned(
222+
0,
223+
ECMASCRIPT_CLIENT_REFERENCE_MERGE_TAG_SSR.clone(),
224+
ssr_modules,
225+
),
218226
module_graph,
219227
Value::new(current_ssr_availability_info),
220228
)
@@ -246,9 +254,14 @@ pub async fn get_app_client_references_chunks(
246254
)
247255
.entered();
248256

249-
Some(client_chunking_context.chunk_group_multiple(
257+
Some(client_chunking_context.chunk_group(
250258
base_ident.with_modifier(client_modules_modifier()),
251-
ChunkableModules::interned(client_modules),
259+
// TODO use correct parameters here, and sort the modules?
260+
ChunkGroup::isolated_merged_interned(
261+
0,
262+
ECMASCRIPT_CLIENT_REFERENCE_MERGE_TAG_CLIENT.clone(),
263+
client_modules,
264+
),
252265
module_graph,
253266
Value::new(current_client_availability_info),
254267
))

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()

turbopack/crates/turbopack-browser/src/chunking_context.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use turbopack_core::{
99
availability_info::AvailabilityInfo,
1010
chunk_group::{make_chunk_group, MakeChunkGroupResult},
1111
module_id_strategies::{DevModuleIdStrategy, ModuleIdStrategy},
12-
Chunk, ChunkGroupResult, ChunkItem, ChunkableModule, ChunkableModules, ChunkingConfig,
13-
ChunkingConfigs, ChunkingContext, EntryChunkGroupResult, EvaluatableAssets, MinifyType,
14-
ModuleId, SourceMapsType,
12+
Chunk, ChunkGroupResult, ChunkItem, ChunkableModule, ChunkingConfig, ChunkingConfigs,
13+
ChunkingContext, EntryChunkGroupResult, EvaluatableAssets, MinifyType, ModuleId,
14+
SourceMapsType,
1515
},
1616
environment::Environment,
1717
ident::AssetIdent,
1818
module::Module,
19-
module_graph::ModuleGraph,
19+
module_graph::{chunk_group_info::ChunkGroup, ModuleGraph},
2020
output::{OutputAsset, OutputAssets},
2121
};
2222
use turbopack_ecmascript::{
@@ -415,39 +415,24 @@ impl ChunkingContext for BrowserChunkingContext {
415415
}
416416

417417
#[turbo_tasks::function]
418-
fn chunk_group(
419-
self: Vc<Self>,
420-
ident: Vc<AssetIdent>,
421-
module: ResolvedVc<Box<dyn ChunkableModule>>,
422-
module_graph: Vc<ModuleGraph>,
423-
availability_info: Value<AvailabilityInfo>,
424-
) -> Vc<ChunkGroupResult> {
425-
self.chunk_group_multiple(
426-
ident,
427-
Vc::cell(vec![module]),
428-
module_graph,
429-
availability_info,
430-
)
431-
}
432-
433-
#[turbo_tasks::function]
434-
async fn chunk_group_multiple(
418+
async fn chunk_group(
435419
self: ResolvedVc<Self>,
436420
ident: Vc<AssetIdent>,
437-
modules: Vc<ChunkableModules>,
421+
chunk_group: Vc<ChunkGroup>,
438422
module_graph: Vc<ModuleGraph>,
439423
availability_info: Value<AvailabilityInfo>,
440424
) -> Result<Vc<ChunkGroupResult>> {
441425
let span = tracing::info_span!("chunking", ident = ident.to_string().await?.to_string());
442426
async move {
443427
let this = self.await?;
444-
let modules = modules.await?;
428+
let chunk_group = chunk_group.await?;
429+
let modules = chunk_group.entries();
445430
let input_availability_info = availability_info.into_value();
446431
let MakeChunkGroupResult {
447432
chunks,
448433
availability_info,
449434
} = make_chunk_group(
450-
modules.iter().copied().map(ResolvedVc::upcast),
435+
modules,
451436
module_graph,
452437
ResolvedVc::upcast(self),
453438
input_availability_info,

0 commit comments

Comments
 (0)