Unverified Commit 9bbacd4d authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

refactor(turbopack): chunk chunk groups by key (#99750)

### What?

Chunking APIs take chunk group keys instead of chunk groups:

- `ChunkingContext::chunk_group` takes `chunk_groups:
SmallVec<[ChunkGroupKey; 1]>`.
- `evaluated_chunk_group`, `entry_chunk_group` and all
`ChunkingContextExt` helpers take `chunk_group: ChunkGroupKey`.
- The browser and Node.js chunking contexts resolve each key with the
new `ChunkGroupInfo::get_chunk_group(key)` and chunk the registered
group, using its entries for evaluation and bootstrapping.
- Callers pass keys: `ChunkGroupKey::{Entry, Async, Isolated, Shared}`,
or the `.key()` of a graph-derived merged group.

### Why?

Chunking already resolved every group by key and failed if the group
wasn't registered, so the entries a caller passed in were redundant at
best and inconsistent with the graph at worst. With key-only APIs,
`ChunkGroupInfo` is the single source of a group's contents, and callers
can no longer construct a group that disagrees with the module graph.

This is PR 4 of a six-PR stack, based on `canary` (#98733, #99686 and
#99749 are already merged):

1. #98823 — Pages Router dev client graph chaining
2. #99910 — regression test for Pages Router `_app` availability
(#99789)
3. #98734 — use graph-derived merged groups
4. **This PR** — chunking takes a `ChunkGroupKey`
5. #98685 — chunk-group availability bitmaps
6. #99050 — keyed chunk-group index lookups

### Verification

- The tree of this PR is identical to the previous #98734 head, which
passed:
- Workspace `cargo clippy --all-targets -D warnings`, per-layer `cargo
fmt` and workspace check
  - Turbopack snapshots: 132 passed, no snapshot changes
- Turbopack production and dev `404-page-router`,
`app-dir/actions/app-action` and `app-dir/initial-css-order`; production
`edge-pages-support`, `app-dir/css-order` and
`layout-segment-chunk-sharing`

<!-- NEXT_JS_LLM -->


<!-- fleet 4f67c6f7-bbe2-454b-9a17-822a60ef8f7e -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 5f4692ea
......@@ -59,7 +59,7 @@ use turbopack_core::{
module_graph::{
GraphEntries, ModuleGraph, SingleModuleGraph, VisitedModules,
binding_usage_info::compute_binding_usage_info,
chunk_group_info::{ChunkGroup, ChunkGroupEntry, EntryHeuristics},
chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics},
},
output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced},
reference::all_assets_from_entries,
......@@ -1916,7 +1916,7 @@ impl AppEndpoint {
NextRuntime::Edge => {
let chunk_group1 = chunking_context.chunk_group(
server_action_manifest_loader.ident(),
smallvec![ChunkGroup::Shared(ResolvedVc::upcast(
smallvec![ChunkGroupKey::Shared(ResolvedVc::upcast(
server_action_manifest_loader,
))],
module_graph,
......@@ -1925,7 +1925,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]),
ChunkGroupKey::Entry(vec![app_entry.rsc_entry]),
module_graph,
OutputAssets::empty(),
chunk_group1.await?.availability_info,
......@@ -1939,7 +1939,7 @@ 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 = ChunkGroupKey::Entry(vec![app_entry.rsc_entry]);
let client_references = client_references.await?;
......@@ -1970,7 +1970,7 @@ impl AppEndpoint {
None
} else {
let entry_index = *chunk_group_info
.get_index_of(entry_chunk_group.key())
.get_index_of(entry_chunk_group.clone())
.await?;
let merged = chunk_group_info
.get_shared_merged_chunk_group(
......@@ -1978,7 +1978,7 @@ impl AppEndpoint {
NEXT_SERVER_UTILITY_MERGE_TAG,
)
.await?;
merged.as_ref().map(|group| group.chunk_group.clone())
merged.as_ref().map(|group| group.chunk_group.key())
};
ensure!(
......@@ -2033,7 +2033,9 @@ impl AppEndpoint {
async {
let chunk_group = chunking_context.chunk_group(
server_component.ident(),
smallvec![ChunkGroup::Shared(ResolvedVc::upcast(server_component))],
smallvec![ChunkGroupKey::Shared(ResolvedVc::upcast(
server_component
))],
module_graph,
current_chunk_group.await?.availability_info,
);
......@@ -2052,7 +2054,7 @@ impl AppEndpoint {
{
let chunk_group = chunking_context.chunk_group(
server_action_manifest_loader.ident(),
smallvec![ChunkGroup::Shared(ResolvedVc::upcast(
smallvec![ChunkGroupKey::Shared(ResolvedVc::upcast(
server_action_manifest_loader,
))],
module_graph,
......
......@@ -18,7 +18,7 @@ use turbopack_core::{
module::Module,
module_graph::{
GraphEntries,
chunk_group_info::{ChunkGroup, ChunkGroupEntry, EntryHeuristics},
chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics},
},
output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced},
reference_type::{EntryReferenceSubType, ReferenceType},
......@@ -107,7 +107,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]),
ChunkGroupKey::Entry(vec![module]),
module_graph,
OutputAssets::empty(),
AvailabilityInfo::root(),
......@@ -132,7 +132,7 @@ impl InstrumentationEndpoint {
.node_root()
.await?
.join("server/instrumentation.js")?,
ChunkGroup::Entry(vec![userland_module]),
ChunkGroupKey::Entry(vec![userland_module]),
module_graph,
OutputAssets::empty(),
OutputAssets::empty(),
......
......@@ -20,7 +20,7 @@ use turbopack_core::{
module::Module,
module_graph::{
GraphEntries,
chunk_group_info::{ChunkGroup, ChunkGroupEntry, EntryHeuristics},
chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics},
},
output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced},
reference_type::{EntryReferenceSubType, ReferenceType},
......@@ -118,7 +118,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]),
ChunkGroupKey::Entry(vec![module]),
module_graph,
OutputAssets::empty(),
AvailabilityInfo::root(),
......@@ -144,7 +144,7 @@ impl MiddlewareEndpoint {
.node_root()
.await?
.join("server/middleware.js")?,
ChunkGroup::Entry(vec![userland_module]),
ChunkGroupKey::Entry(vec![userland_module]),
module_graph,
OutputAssets::empty(),
OutputAssets::empty(),
......
......@@ -58,7 +58,7 @@ use turbopack_core::{
module_graph::{
GraphEntries, ModuleGraph, SingleModuleGraph, VisitedModules,
binding_usage_info::compute_binding_usage_info,
chunk_group_info::{ChunkGroup, ChunkGroupEntry, EntryHeuristics},
chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics},
},
output::{OptionOutputAsset, OutputAsset, OutputAssets},
reference::all_assets_from_entries,
......@@ -956,7 +956,7 @@ impl PageEndpoint {
};
let client_chunk_group = client_chunking_context.evaluated_chunk_group(
AssetIdent::from_path(this.page.await?.base_path.clone()).into_vc(),
ChunkGroup::Entry(evaluatable_assets),
ChunkGroupKey::Entry(evaluatable_assets),
module_graph,
OutputAssets::empty(),
availability_info,
......@@ -1159,7 +1159,7 @@ impl PageEndpoint {
// Registered as an entry group by `layout_chunk_group_entry`.
let chunk_group = chunking_context.chunk_group(
layout.ident(),
smallvec![ChunkGroup::Entry(vec![layout])],
smallvec![ChunkGroupKey::Entry(vec![layout])],
ssr_module_graph,
current_chunk_group.await?.availability_info,
);
......@@ -1179,7 +1179,7 @@ impl PageEndpoint {
if is_edge {
let chunk_assets = edge_chunking_context.evaluated_chunk_group_assets(
ssr_module.ident(),
ChunkGroup::Entry(vec![ssr_module]),
ChunkGroupKey::Entry(vec![ssr_module]),
ssr_module_graph,
OutputAssets::empty(),
current_chunk_group.await?.availability_info,
......@@ -1208,7 +1208,7 @@ impl PageEndpoint {
let ssr_entry_chunk = node_chunking_context
.entry_chunk_group_asset(
ssr_entry_chunk_path,
ChunkGroup::Entry(vec![ssr_module]),
ChunkGroupKey::Entry(vec![ssr_module]),
ssr_module_graph,
current_chunk_group.primary_assets(),
current_chunk_group.referenced_assets(),
......@@ -1790,7 +1790,7 @@ pub struct InternalSsrChunkModule {
/// A layout is an entry group of its own, like the standalone `/_document` and `/_app`
/// endpoints register their module, so the graph never has to choose between a shared and an
/// entry group for the same module and `internal_ssr_chunk` can always chunk it as
/// `ChunkGroup::Entry(vec![layout])`. Default heuristics keep a page's clusters off the layout
/// `ChunkGroupKey::Entry(vec![layout])`. Default heuristics keep a page's clusters off the layout
/// groups shared between pages.
fn layout_chunk_group_entry(layout: ResolvedVc<Box<dyn Module>>) -> ChunkGroupEntry {
ChunkGroupEntry::Entry {
......
......@@ -49,7 +49,7 @@ use turbopack_core::{
module::{Module, Modules},
module_graph::{
GraphTraversalAction, ModuleGraph, ModuleGraphLayer, async_module_info::AsyncModulesInfo,
binding_usage_info::ModuleExportUsage, chunk_group_info::ChunkGroup,
binding_usage_info::ModuleExportUsage, chunk_group_info::ChunkGroupKey,
},
output::{OutputAsset, OutputAssets, OutputAssetsReference},
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
......@@ -1240,7 +1240,7 @@ impl ChunkingContext for UseCacheContentHashChunkingContext {
async fn chunk_group(
self: Vc<Self>,
_ident: Vc<AssetIdent>,
_chunk_groups: SmallVec<[ChunkGroup; 1]>,
_chunk_groups: SmallVec<[ChunkGroupKey; 1]>,
_module_graph: ResolvedVc<ModuleGraph>,
_availability_info: AvailabilityInfo,
) -> Result<Vc<ChunkGroupResult>> {
......@@ -1251,7 +1251,7 @@ impl ChunkingContext for UseCacheContentHashChunkingContext {
async fn evaluated_chunk_group(
self: Vc<Self>,
_ident: Vc<AssetIdent>,
_chunk_group: ChunkGroup,
_chunk_group: ChunkGroupKey,
_module_graph: ResolvedVc<ModuleGraph>,
_extra_chunks: Vc<OutputAssets>,
_input_availability_info: AvailabilityInfo,
......@@ -1273,7 +1273,7 @@ impl ChunkingContext for UseCacheContentHashChunkingContext {
async fn entry_chunk_group(
self: Vc<Self>,
_path: FileSystemPath,
_chunk_group: ChunkGroup,
_chunk_group: ChunkGroupKey,
_module_graph: ResolvedVc<ModuleGraph>,
_extra_chunks: Vc<OutputAssets>,
_extra_referenced_assets: Vc<OutputAssets>,
......
......@@ -11,7 +11,7 @@ use turbopack_core::{
module::Module,
module_graph::{
ModuleGraph, SingleModuleGraph,
chunk_group_info::{ChunkGroup, ChunkGroupEntry, EntryHeuristics},
chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics},
},
output::{OutputAsset, OutputAssets},
reference_type::{EntryReferenceSubType, ReferenceType},
......@@ -126,7 +126,7 @@ async fn service_worker_chunk(
.join("static")?
.join("service-worker")?
.join(&filename)?,
ChunkGroup::Entry(vec![module]),
ChunkGroupKey::Entry(vec![module]),
own_graph,
/* extra_chunks */ OutputAssets::empty(),
/* extra_referenced_chunks */ OutputAssets::empty(),
......
......@@ -11,7 +11,7 @@ use turbopack_core::{
module::Module,
module_graph::{
ModuleGraph,
chunk_group_info::{ChunkGroup, ChunkGroupInfo},
chunk_group_info::{ChunkGroup, ChunkGroupInfo, ChunkGroupKey},
},
output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced},
};
......@@ -80,13 +80,13 @@ struct ClientReferenceWithContext {
server_component: Option<ResolvedVc<Box<dyn Module>>>,
}
/// Returns the graph-derived isolated merged groups (tagged `merge_tag`) that chunk `references`,
/// with their entries as derived by the module graph.
/// Returns the keys of the graph-derived isolated merged groups (tagged `merge_tag`) that chunk
/// `references`. Chunking looks the groups up by key, so their entries come from the graph.
async fn derived_isolated_merged_groups(
chunk_group_info: Vc<ChunkGroupInfo>,
references: &[ClientReferenceWithContext],
merge_tag: &RcStr,
) -> Result<SmallVec<[ChunkGroup; 1]>> {
) -> Result<SmallVec<[ChunkGroupKey; 1]>> {
let mut groups = FxIndexMap::default();
for &ClientReferenceWithContext {
......@@ -124,7 +124,7 @@ async fn derived_isolated_merged_groups(
found = true;
groups
.entry(group.index)
.or_insert_with(|| group.chunk_group.clone());
.or_insert_with(|| group.chunk_group.key());
}
}
if !found {
......
......@@ -6,7 +6,7 @@ use turbopack_core::{
ChunkGroupResult, ChunkingContext, EvaluatableAssets, availability_info::AvailabilityInfo,
},
ident::AssetIdent,
module_graph::{ModuleGraph, chunk_group_info::ChunkGroup},
module_graph::{ModuleGraph, chunk_group_info::ChunkGroupKey},
output::OutputAssets,
};
......@@ -26,7 +26,7 @@ pub async fn get_app_client_shared_chunk_group(
client_chunking_context
.evaluated_chunk_group(
ident,
ChunkGroup::Entry(
ChunkGroupKey::Entry(
app_client_runtime_entries
.await?
.iter()
......
......@@ -27,7 +27,7 @@ use turbopack_core::{
module_graph::{
ModuleGraph,
binding_usage_info::{BindingUsageInfo, ModuleExportUsage},
chunk_group_info::ChunkGroup,
chunk_group_info::{ChunkGroup, ChunkGroupKey},
},
output::{ExpandOutputAssetsInput, OutputAsset, OutputAssets, expand_output_assets},
};
......@@ -878,19 +878,24 @@ impl ChunkingContext for BrowserChunkingContext {
async fn chunk_group(
self: ResolvedVc<Self>,
ident: Vc<AssetIdent>,
chunk_groups: SmallVec<[ChunkGroup; 1]>,
chunk_groups: SmallVec<[ChunkGroupKey; 1]>,
module_graph: ResolvedVc<ModuleGraph>,
availability_info: AvailabilityInfo,
) -> Result<Vc<ChunkGroupResult>> {
let span = tracing::info_span!("chunking", name = display(ident.to_string().await?));
async move {
let chunk_group_info = module_graph.chunk_group_info();
let mut groups = SmallVec::with_capacity(chunk_groups.len());
for key in chunk_groups {
groups.push(chunk_group_info.get_chunk_group(key).await?);
}
let input_availability_info = availability_info;
let MakeChunkGroupResult {
chunks,
references,
availability_info,
} = make_chunk_group(
chunk_groups,
groups,
module_graph,
ResolvedVc::upcast(self),
input_availability_info,
......@@ -922,7 +927,7 @@ impl ChunkingContext for BrowserChunkingContext {
async fn evaluated_chunk_group(
self: ResolvedVc<Self>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: ResolvedVc<ModuleGraph>,
// Extra chunks to include in the HMR chunk list beyond what is reachable from this chunk
// group. Used to cover RSC client reference chunks that are built separately.
......@@ -936,6 +941,10 @@ impl ChunkingContext for BrowserChunkingContext {
);
async move {
let this = self.await?;
let chunk_group = module_graph
.chunk_group_info()
.get_chunk_group(chunk_group)
.await?;
let MakeChunkGroupResult {
chunks,
references,
......@@ -1106,7 +1115,7 @@ impl ChunkingContext for BrowserChunkingContext {
async fn entry_chunk_group(
self: ResolvedVc<Self>,
path: FileSystemPath,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: ResolvedVc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
extra_referenced_assets: Vc<OutputAssets>,
......@@ -1126,6 +1135,10 @@ impl ChunkingContext for BrowserChunkingContext {
chunking_type = "single-chunk entry",
);
async move {
let chunk_group = module_graph
.chunk_group_info()
.get_chunk_group(chunk_group)
.await?;
let MakeChunkGroupResult {
chunks,
references,
......
......@@ -36,7 +36,7 @@ use turbopack_core::{
module_graph::{
GraphEntries, ModuleGraph, SingleModuleGraph,
binding_usage_info::compute_binding_usage_info,
chunk_group_info::{ChunkGroup, ChunkGroupEntry, EntryHeuristics},
chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics},
},
output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced},
reference_type::{EntryReferenceSubType, ReferenceType},
......@@ -457,7 +457,7 @@ async fn build_internal(
.with_extension("entry.js"),
)
.into_vc(),
ChunkGroup::Entry(
ChunkGroupKey::Entry(
[ResolvedVc::upcast(ecmascript)].into_iter().collect(),
),
module_graph,
......@@ -478,7 +478,9 @@ async fn build_internal(
.unwrap(),
)?
.with_extension("entry.js"),
ChunkGroup::Entry(vec![ResolvedVc::upcast(ecmascript)]),
ChunkGroupKey::Entry(vec![ResolvedVc::upcast(
ecmascript,
)]),
module_graph,
OutputAssets::empty(),
OutputAssets::empty(),
......
......@@ -19,7 +19,7 @@ use crate::{
json::RcJsonRawValue,
module::Module,
module_graph::{
ModuleGraph, binding_usage_info::ModuleExportUsage, chunk_group_info::ChunkGroup,
ModuleGraph, binding_usage_info::ModuleExportUsage, chunk_group_info::ChunkGroupKey,
module_batches::BatchingConfig, style_groups::StyleGroupsAlgorithm,
},
output::{
......@@ -475,15 +475,19 @@ pub trait ChunkingContext {
chunk_item: ResolvedVc<Box<dyn ChunkItem>>,
) -> Vc<Box<dyn OutputAsset>>;
/// Chunks `chunk_groups` together as one chunk group, in a single task.
/// Chunks the chunk groups identified by `chunk_groups` together as one chunk group, in a
/// single task.
///
/// The groups must be registered in `module_graph`'s
/// [`ChunkGroupInfo`](crate::module_graph::chunk_group_info::ChunkGroupInfo); their contents
/// (e.g. the entries of a merged group) are looked up there, so callers only name the groups.
/// The modules of all groups are collected once each and chunked together, and the resulting
/// availability includes every group. `chunk_groups` must not be empty.
#[turbo_tasks::function]
fn chunk_group(
self: Vc<Self>,
ident: Vc<AssetIdent>,
chunk_groups: SmallVec<[ChunkGroup; 1]>,
chunk_groups: SmallVec<[ChunkGroupKey; 1]>,
module_graph: Vc<ModuleGraph>,
availability_info: AvailabilityInfo,
) -> Vc<ChunkGroupResult>;
......@@ -500,7 +504,7 @@ pub trait ChunkingContext {
fn evaluated_chunk_group(
self: Vc<Self>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
availability_info: AvailabilityInfo,
......@@ -529,7 +533,7 @@ pub trait ChunkingContext {
fn entry_chunk_group(
self: Vc<Self>,
path: FileSystemPath,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
extra_referenced_assets: Vc<OutputAssets>,
......@@ -569,7 +573,7 @@ pub trait ChunkingContextExt {
fn root_chunk_group(
self: Vc<Self>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
) -> Vc<ChunkGroupResult>
where
......@@ -578,7 +582,7 @@ pub trait ChunkingContextExt {
fn root_chunk_group_assets(
self: Vc<Self>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
) -> Vc<OutputAssetsWithReferenced>
where
......@@ -587,7 +591,7 @@ pub trait ChunkingContextExt {
fn evaluated_chunk_group_assets(
self: Vc<Self>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
availability_info: AvailabilityInfo,
......@@ -598,7 +602,7 @@ pub trait ChunkingContextExt {
fn entry_chunk_group_asset(
self: Vc<Self>,
path: FileSystemPath,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
extra_referenced_assets: Vc<OutputAssets>,
......@@ -610,7 +614,7 @@ pub trait ChunkingContextExt {
fn root_entry_chunk_group(
self: Vc<Self>,
path: FileSystemPath,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
extra_referenced_assets: Vc<OutputAssets>,
......@@ -621,7 +625,7 @@ pub trait ChunkingContextExt {
fn root_entry_chunk_group_asset(
self: Vc<Self>,
path: FileSystemPath,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
extra_referenced_assets: Vc<OutputAssets>,
......@@ -632,7 +636,7 @@ pub trait ChunkingContextExt {
fn chunk_group_assets(
self: Vc<Self>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
availability_info: AvailabilityInfo,
) -> Vc<OutputAssetsWithReferenced>
......@@ -651,7 +655,7 @@ impl<T: ChunkingContext + Send + Upcast<Box<dyn ChunkingContext>>> ChunkingConte
fn root_chunk_group(
self: Vc<Self>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
) -> Vc<ChunkGroupResult> {
self.chunk_group(
......@@ -665,7 +669,7 @@ impl<T: ChunkingContext + Send + Upcast<Box<dyn ChunkingContext>>> ChunkingConte
fn root_chunk_group_assets(
self: Vc<Self>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
) -> Vc<OutputAssetsWithReferenced> {
root_chunk_group_assets(
......@@ -679,7 +683,7 @@ impl<T: ChunkingContext + Send + Upcast<Box<dyn ChunkingContext>>> ChunkingConte
fn evaluated_chunk_group_assets(
self: Vc<Self>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
availability_info: AvailabilityInfo,
......@@ -697,7 +701,7 @@ impl<T: ChunkingContext + Send + Upcast<Box<dyn ChunkingContext>>> ChunkingConte
fn entry_chunk_group_asset(
self: Vc<Self>,
path: FileSystemPath,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
extra_referenced_assets: Vc<OutputAssets>,
......@@ -717,7 +721,7 @@ impl<T: ChunkingContext + Send + Upcast<Box<dyn ChunkingContext>>> ChunkingConte
fn root_entry_chunk_group(
self: Vc<Self>,
path: FileSystemPath,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
extra_referenced_assets: Vc<OutputAssets>,
......@@ -735,7 +739,7 @@ impl<T: ChunkingContext + Send + Upcast<Box<dyn ChunkingContext>>> ChunkingConte
fn root_entry_chunk_group_asset(
self: Vc<Self>,
path: FileSystemPath,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
extra_referenced_assets: Vc<OutputAssets>,
......@@ -754,7 +758,7 @@ impl<T: ChunkingContext + Send + Upcast<Box<dyn ChunkingContext>>> ChunkingConte
fn chunk_group_assets(
self: Vc<Self>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
availability_info: AvailabilityInfo,
) -> Vc<OutputAssetsWithReferenced> {
......@@ -817,7 +821,7 @@ async fn relative_path_from_chunk_root_to_project_root(
fn root_chunk_group_assets(
chunking_context: Vc<Box<dyn ChunkingContext>>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
) -> Vc<OutputAssetsWithReferenced> {
chunking_context
......@@ -829,7 +833,7 @@ fn root_chunk_group_assets(
fn evaluated_chunk_group_assets(
chunking_context: Vc<Box<dyn ChunkingContext>>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
availability_info: AvailabilityInfo,
......@@ -849,7 +853,7 @@ fn evaluated_chunk_group_assets(
async fn entry_chunk_group_asset(
chunking_context: Vc<Box<dyn ChunkingContext>>,
path: FileSystemPath,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
extra_referenced_assets: Vc<OutputAssets>,
......@@ -872,7 +876,7 @@ async fn entry_chunk_group_asset(
fn chunk_group_assets(
chunking_context: Vc<Box<dyn ChunkingContext>>,
ident: Vc<AssetIdent>,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: Vc<ModuleGraph>,
availability_info: AvailabilityInfo,
) -> Vc<OutputAssetsWithReferenced> {
......
......@@ -204,6 +204,17 @@ impl ChunkGroupInfo {
}
}
impl ChunkGroupInfo {
/// Returns the registered chunk group identified by `key`.
///
/// Chunking takes a [`ChunkGroupKey`] and resolves the group here, so a group's contents (e.g.
/// the entries of a merged group) always come from the module graph rather than the caller.
pub async fn get_chunk_group(self: Vc<Self>, key: ChunkGroupKey) -> Result<ChunkGroup> {
let index = *self.get_index_of(key).await?;
Ok(self.await?.chunk_groups[index].clone())
}
}
/// Per-entry chunking heuristics.
#[turbo_tasks::task_input]
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Encode, Decode)]
......
......@@ -12,7 +12,7 @@ use turbopack_core::{
availability_info::AvailabilityInfo,
},
module::Module,
module_graph::{ModuleGraph, chunk_group_info::ChunkGroup},
module_graph::{ModuleGraph, chunk_group_info::ChunkGroupKey},
output::{OutputAsset, OutputAssets, OutputAssetsReference, OutputAssetsWithReferenced},
version::{Version, VersionedContent},
};
......@@ -152,7 +152,7 @@ impl DevHtmlAsset {
chunking_context
.evaluated_chunk_group_assets(
chunkable_module.ident(),
ChunkGroup::Entry(
ChunkGroupKey::Entry(
runtime_entries
.await?
.iter()
......@@ -168,7 +168,7 @@ impl DevHtmlAsset {
chunking_context
.root_chunk_group_assets(
chunkable_module.ident(),
ChunkGroup::Entry(vec![ResolvedVc::upcast(chunkable_module)]),
ChunkGroupKey::Entry(vec![ResolvedVc::upcast(chunkable_module)]),
*module_graph,
)
.await?
......
......@@ -11,7 +11,7 @@ use turbopack_core::{
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::{
ModuleGraph, chunk_group_info::ChunkGroup, module_batch::ChunkableModuleOrBatch,
ModuleGraph, chunk_group_info::ChunkGroupKey, module_batch::ChunkableModuleOrBatch,
},
output::OutputAssetsWithReferenced,
reference::ModuleReferences,
......@@ -85,7 +85,7 @@ impl AsyncLoaderModule {
}
Ok(self.chunking_context.chunk_group_assets(
self.inner.ident(),
ChunkGroup::Async(ResolvedVc::upcast(self.inner)),
ChunkGroupKey::Async(ResolvedVc::upcast(self.inner)),
module_graph,
self.availability_info,
))
......
......@@ -10,7 +10,7 @@ use turbopack_core::{
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::{
ModuleGraph, chunk_group_info::ChunkGroup, module_batch::ChunkableModuleOrBatch,
ModuleGraph, chunk_group_info::ChunkGroupKey, module_batch::ChunkableModuleOrBatch,
},
output::{OutputAssets, OutputAssetsWithReferenced},
};
......@@ -65,7 +65,7 @@ impl ManifestAsyncModule {
pub(super) fn chunk_group(&self) -> Vc<OutputAssetsWithReferenced> {
self.chunking_context.chunk_group_assets(
self.inner.ident(),
ChunkGroup::Async(ResolvedVc::upcast(self.inner)),
ChunkGroupKey::Async(ResolvedVc::upcast(self.inner)),
*self.module_graph,
self.availability_info,
)
......
......@@ -13,7 +13,7 @@ use turbopack_core::{
file_source::FileSource,
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::{ModuleGraph, chunk_group_info::ChunkGroup},
module_graph::{ModuleGraph, chunk_group_info::ChunkGroupKey},
output::{OutputAsset, OutputAssets, OutputAssetsWithReferenced},
reference::{ModuleReference, ModuleReferences, SingleChunkableModuleReference},
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
......@@ -75,7 +75,7 @@ impl WorkerLoaderModule {
.into_vc();
chunking_context.evaluated_chunk_group_assets(
ident,
ChunkGroup::Isolated(ResolvedVc::upcast(this.inner)),
ChunkGroupKey::Isolated(ResolvedVc::upcast(this.inner)),
module_graph,
OutputAssets::empty(),
AvailabilityInfo::root(),
......@@ -98,7 +98,7 @@ impl WorkerLoaderModule {
let entry_result = chunking_context
.root_entry_chunk_group(
worker_path,
ChunkGroup::Isolated(ResolvedVc::upcast(evaluatable)),
ChunkGroupKey::Isolated(ResolvedVc::upcast(evaluatable)),
module_graph,
OutputAssets::empty(),
OutputAssets::empty(),
......
......@@ -26,7 +26,7 @@ use turbopack_core::{
module::Module,
module_graph::{
GraphEntries, ModuleGraph,
chunk_group_info::{ChunkGroup, ChunkGroupEntry, EntryHeuristics},
chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics},
},
output::{OutputAsset, OutputAssets},
reference_type::{InnerAssets, ReferenceType},
......@@ -162,7 +162,7 @@ async fn emit_evaluate_pool_assets_operation(
let bootstrap = chunking_context.root_entry_chunk_group_asset(
entrypoint.clone(),
ChunkGroup::Entry(entries.iter().cloned().map(ResolvedVc::upcast).collect()),
ChunkGroupKey::Entry(entries.iter().cloned().map(ResolvedVc::upcast).collect()),
*module_graph,
OutputAssets::empty(),
OutputAssets::empty(),
......
......@@ -34,7 +34,7 @@ use turbopack_core::{
module::Module,
module_graph::{
ModuleGraph, SingleModuleGraph,
chunk_group_info::{ChunkGroup, ChunkGroupEntry, EntryHeuristics},
chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics},
},
output::{ExpandOutputAssetsInput, OutputAsset, OutputAssets, expand_output_assets},
reference_type::{EcmaScriptModulesReferenceSubType, InnerAssets, ReferenceType},
......@@ -996,7 +996,7 @@ impl EvaluateContext for WebpackLoaderContext {
let bootstrap = self.chunking_context.root_entry_chunk_group_asset(
entry_path.clone(),
ChunkGroup::Entry(vec![ResolvedVc::upcast(evaluatable)]),
ChunkGroupKey::Entry(vec![ResolvedVc::upcast(evaluatable)]),
*import_module_graph,
OutputAssets::empty(),
OutputAssets::empty(),
......
......@@ -25,7 +25,7 @@ use turbopack_core::{
module_graph::{
ModuleGraph,
binding_usage_info::{BindingUsageInfo, ModuleExportUsage},
chunk_group_info::ChunkGroup,
chunk_group_info::ChunkGroupKey,
},
output::{OutputAsset, OutputAssets},
};
......@@ -566,18 +566,23 @@ impl ChunkingContext for NodeJsChunkingContext {
async fn chunk_group(
self: ResolvedVc<Self>,
ident: Vc<AssetIdent>,
chunk_groups: SmallVec<[ChunkGroup; 1]>,
chunk_groups: SmallVec<[ChunkGroupKey; 1]>,
module_graph: ResolvedVc<ModuleGraph>,
availability_info: AvailabilityInfo,
) -> Result<Vc<ChunkGroupResult>> {
let span = tracing::info_span!("chunking", name = display(ident.to_string().await?));
async move {
let chunk_group_info = module_graph.chunk_group_info();
let mut groups = SmallVec::with_capacity(chunk_groups.len());
for key in chunk_groups {
groups.push(chunk_group_info.get_chunk_group(key).await?);
}
let MakeChunkGroupResult {
chunks,
references,
availability_info,
} = make_chunk_group(
chunk_groups,
groups,
module_graph,
ResolvedVc::upcast(self),
availability_info,
......@@ -609,7 +614,7 @@ impl ChunkingContext for NodeJsChunkingContext {
pub async fn entry_chunk_group(
self: ResolvedVc<Self>,
path: FileSystemPath,
chunk_group: ChunkGroup,
chunk_group: ChunkGroupKey,
module_graph: ResolvedVc<ModuleGraph>,
extra_chunks: Vc<OutputAssets>,
extra_referenced_assets: Vc<OutputAssets>,
......@@ -621,6 +626,10 @@ impl ChunkingContext for NodeJsChunkingContext {
chunking_type = "entry",
);
async move {
let chunk_group = module_graph
.chunk_group_info()
.get_chunk_group(chunk_group)
.await?;
let MakeChunkGroupResult {
chunks,
references,
......@@ -693,7 +702,7 @@ impl ChunkingContext for NodeJsChunkingContext {
fn evaluated_chunk_group(
self: Vc<Self>,
_ident: Vc<AssetIdent>,
_chunk_group: ChunkGroup,
_chunk_group: ChunkGroupKey,
_module_graph: Vc<ModuleGraph>,
_extra_chunks: Vc<OutputAssets>,
_availability_info: AvailabilityInfo,
......
......@@ -49,7 +49,7 @@ use turbopack_core::{
module_graph::{
GraphEntries, ModuleGraph, SingleModuleGraph,
binding_usage_info::compute_binding_usage_info,
chunk_group_info::{ChunkGroup, ChunkGroupEntry, EntryHeuristics},
chunk_group_info::{ChunkGroupEntry, ChunkGroupKey, EntryHeuristics},
},
output::{OutputAsset, OutputAssets, OutputAssetsReference, OutputAssetsWithReferenced},
reference_type::{EntryReferenceSubType, ReferenceType, ReferenceTypeCondition},
......@@ -641,7 +641,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
chunk_root_path
.join(entry_module.ident().await?.path.file_stem().unwrap())?
.with_extension("entry.js"),
ChunkGroup::Entry(entry_modules),
ChunkGroupKey::Entry(entry_modules),
module_graph,
OutputAssets::empty(),
OutputAssets::empty(),
......@@ -656,7 +656,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
.cell(),
Runtime::Browser => chunking_context.evaluated_chunk_group_assets(
entry_module.ident(),
ChunkGroup::Entry(entry_modules.into_iter().collect()),
ChunkGroupKey::Entry(entry_modules.into_iter().collect()),
module_graph,
OutputAssets::empty(),
AvailabilityInfo::root(),
......@@ -670,7 +670,7 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
chunk_root_path
.join(entry_module.ident().await?.path.file_stem().unwrap())?
.with_extension("entry.js"),
ChunkGroup::Entry(entry_modules),
ChunkGroupKey::Entry(entry_modules),
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