Skip to content

Commit

Permalink
[Turbopack] pass inner modules to chunking to match chunking types (#…
Browse files Browse the repository at this point in the history
…76227)

### What?

run chunking for the inner modules of the wrapper modules
  • Loading branch information
sokra authored Feb 20, 2025
1 parent 78b7422 commit 9a41b9e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
13 changes: 5 additions & 8 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ use turbopack::{
use turbopack_core::{
asset::AssetContent,
chunk::{
availability_info::AvailabilityInfo, ChunkableModule, ChunkableModules, ChunkingContext,
ChunkingContextExt, EvaluatableAsset, EvaluatableAssets,
availability_info::AvailabilityInfo, ChunkableModules, ChunkingContext, ChunkingContextExt,
EvaluatableAsset, EvaluatableAssets,
},
file_source::FileSource,
ident::AssetIdent,
Expand Down Expand Up @@ -833,7 +833,7 @@ impl AppProject {
let graph = SingleModuleGraph::new_with_entries_visited(
server_utils
.iter()
.map(|m| **m)
.map(|m| Vc::upcast(**m))
.chain(extra_entries)
.collect(),
VisitedModules::empty(),
Expand Down Expand Up @@ -1663,10 +1663,7 @@ impl AppEndpoint {
let server_utils = client_references
.server_utils
.iter()
.map(|m| async move {
Ok(*ResolvedVc::try_downcast::<Box<dyn ChunkableModule>>(*m)
.context("Expected server utils to be chunkable")?)
})
.map(async |m| Ok(Vc::upcast(*m.await?.module)))
.try_join()
.await?;
let chunk_group = chunking_context
Expand Down Expand Up @@ -1708,7 +1705,7 @@ impl AppEndpoint {
let chunk_group = chunking_context
.chunk_group(
server_component.ident(),
*ResolvedVc::upcast(server_component),
Vc::upcast(*server_component.await?.module),
module_graph,
Value::new(current_availability_info),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub struct ClientReferenceGraphResult {
pub client_references_by_server_component:
FxIndexMap<Option<ResolvedVc<NextServerComponentModule>>, Vec<ResolvedVc<Box<dyn Module>>>>,
pub server_component_entries: Vec<ResolvedVc<NextServerComponentModule>>,
pub server_utils: Vec<ResolvedVc<Box<dyn Module>>>,
pub server_utils: Vec<ResolvedVc<NextServerUtilityModule>>,
pub visited_nodes: ResolvedVc<VisitedClientReferenceGraphNodes>,
}

Expand Down Expand Up @@ -147,7 +147,7 @@ impl ClientReferenceGraphResult {
#[derive(Clone, Debug)]
pub struct ServerEntries {
pub server_component_entries: Vec<ResolvedVc<NextServerComponentModule>>,
pub server_utils: Vec<ResolvedVc<Box<dyn Module>>>,
pub server_utils: Vec<ResolvedVc<NextServerUtilityModule>>,
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -268,7 +268,7 @@ impl VisitClientReferenceNodeState {
enum VisitClientReferenceNodeType {
ClientReference(ClientReference, ReadRef<RcStr>),
ServerComponentEntry(ResolvedVc<NextServerComponentModule>, ReadRef<RcStr>),
ServerUtilEntry(ResolvedVc<Box<dyn Module>>, ReadRef<RcStr>),
ServerUtilEntry(ResolvedVc<NextServerUtilityModule>, ReadRef<RcStr>),
Internal(ResolvedVc<Box<dyn Module>>, ReadRef<RcStr>),
}

Expand Down Expand Up @@ -306,7 +306,9 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
// nodes' edges.
VisitClientReferenceNodeType::ClientReference(..) => return Ok(vec![]),
VisitClientReferenceNodeType::Internal(module, _) => module,
VisitClientReferenceNodeType::ServerUtilEntry(module, _) => module,
VisitClientReferenceNodeType::ServerUtilEntry(module, _) => {
ResolvedVc::upcast(module)
}
VisitClientReferenceNodeType::ServerComponentEntry(module, _) => {
ResolvedVc::upcast(module)
}
Expand Down Expand Up @@ -370,11 +372,13 @@ impl Visit<VisitClientReferenceNode> for VisitClientReference {
});
}

if ResolvedVc::try_downcast_type::<NextServerUtilityModule>(*module).is_some() {
if let Some(server_util_module) =
ResolvedVc::try_downcast_type::<NextServerUtilityModule>(*module)
{
return Ok(VisitClientReferenceNode {
state: VisitClientReferenceNodeState::InServerUtil,
ty: VisitClientReferenceNodeType::ServerUtilEntry(
*module,
server_util_module,
module.ident().to_string().await?,
),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn modifier() -> Vc<RcStr> {

#[turbo_tasks::value(shared)]
pub struct NextServerComponentModule {
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
pub module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
}

#[turbo_tasks::value_impl]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn modifier() -> Vc<RcStr> {

#[turbo_tasks::value(shared)]
pub struct NextServerUtilityModule {
module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
pub module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
}

#[turbo_tasks::value_impl]
Expand Down

0 comments on commit 9a41b9e

Please sign in to comment.