Unverified Commit d9751eaa authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: use ChunkGroupEntry::Shared, part 2 (#91279)

Followup to https://github.com/vercel/next.js/pull/90978, which fixed the `ChunkGroup`s  passed to `.chunk_group()`, forgot to update the the module graph entry types, which didn't match up.

The `ChunkGroupEntry::Shared(ResolvedVc::upcast(*server_component))` one was more involved:
- We used to have `layout.js [app-rsc] (Next.js Server Component)` --ChunkingType::Shared--> `layout.js [app-rsc]` (the actual module)
- But this meant that the actual chunk group was `Shared(server_component.await?.module)` (the inner one)
- This wasn't possible to use for the module graph construction though, as the client reference transform needs to see the `layout.js [app-rsc] (Next.js Server Component)` module during the traversal, to infer the parent server component of the given subgraph.

So instead, move to
- `loader-tree in the template`
- --ChunkingType::Shared-->
- `layout.js [app-rsc] (Next.js Server Component)`
- --ChunkingType::Parallel-->
- `layout.js [app-rsc]`

The reason for that `ChunkingType::Shared` is so that we can chunk arbitrary modules that are loaded by `ChunkGroup::Entry`. Meaning that we can do `chunk_group(Shared(root_layout))).concat(chunk_group(Shared(layout))).concat(chunk_group(Entry(page)))` and rely on turbotask caching to deduplicate layout chunking across pages.
parent 1ab53492
...@@ -930,9 +930,7 @@ impl AppProject { ...@@ -930,9 +930,7 @@ impl AppProject {
.take(server_component_entries.len().saturating_sub(1)) .take(server_component_entries.len().saturating_sub(1))
{ {
let graph = SingleModuleGraph::new_with_entries_visited_intern( let graph = SingleModuleGraph::new_with_entries_visited_intern(
// This should really be ChunkGroupEntry::Shared(module.await?.module), vec![ChunkGroupEntry::Shared(ResolvedVc::upcast(*module))],
// but that breaks everything for some reason.
vec![ChunkGroupEntry::Entry(vec![ResolvedVc::upcast(*module)])],
visited_modules, visited_modules,
should_trace, should_trace,
should_read_binding_usage, should_read_binding_usage,
...@@ -1895,9 +1893,7 @@ impl AppEndpoint { ...@@ -1895,9 +1893,7 @@ impl AppEndpoint {
async { async {
let chunk_group = chunking_context.chunk_group( let chunk_group = chunking_context.chunk_group(
server_component.ident(), server_component.ident(),
ChunkGroup::Shared(ResolvedVc::upcast( ChunkGroup::Shared(ResolvedVc::upcast(server_component)),
server_component.await?.module,
)),
module_graph, module_graph,
current_chunk_group.await?.availability_info, current_chunk_group.await?.availability_info,
); );
...@@ -2157,9 +2153,9 @@ impl Endpoint for AppEndpoint { ...@@ -2157,9 +2153,9 @@ impl Endpoint for AppEndpoint {
.await?, .await?,
); );
Ok(Vc::cell(vec![ChunkGroupEntry::Entry(vec![ Ok(Vc::cell(vec![ChunkGroupEntry::Shared(
server_actions_loader, server_actions_loader,
])])) )]))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -187,9 +187,13 @@ impl AppPageLoaderTreeBuilder { ...@@ -187,9 +187,13 @@ impl AppPageLoaderTreeBuilder {
// This should use the same importing mechanism as create_module_tuple_code, so that // This should use the same importing mechanism as create_module_tuple_code, so that
// the relative order of items is retained (which isn't the case // the relative order of items is retained (which isn't the case
// when mixing ESM imports and requires). // when mixing ESM imports and requires).
self.base self.base.imports.push(
.imports format!(
.push(format!("const {identifier} = require(\"{inner_module_id}\");").into()); "const {identifier} = require(/*turbopackChunkingType: \
shared*/\"{inner_module_id}\");"
)
.into(),
);
let source = dynamic_image_metadata_source( let source = dynamic_image_metadata_source(
*ResolvedVc::upcast(self.base.module_asset_context), *ResolvedVc::upcast(self.base.module_asset_context),
...@@ -234,9 +238,13 @@ impl AppPageLoaderTreeBuilder { ...@@ -234,9 +238,13 @@ impl AppPageLoaderTreeBuilder {
// This should use the same importing mechanism as create_module_tuple_code, so that the // This should use the same importing mechanism as create_module_tuple_code, so that the
// relative order of items is retained (which isn't the case when mixing ESM imports and // relative order of items is retained (which isn't the case when mixing ESM imports and
// requires). // requires).
self.base self.base.imports.push(
.imports format!(
.push(format!("const {identifier} = require(\"{inner_module_id}\");").into()); "const {identifier} = require(/*turbopackChunkingType: \
shared*/\"{inner_module_id}\");"
)
.into(),
);
let module = StructuredImageModuleType::create_module( let module = StructuredImageModuleType::create_module(
Vc::upcast(FileSource::new(path.clone())), Vc::upcast(FileSource::new(path.clone())),
BlurPlaceholderMode::None, BlurPlaceholderMode::None,
...@@ -295,9 +303,13 @@ impl AppPageLoaderTreeBuilder { ...@@ -295,9 +303,13 @@ impl AppPageLoaderTreeBuilder {
// This should use the same importing mechanism as create_module_tuple_code, so that the // This should use the same importing mechanism as create_module_tuple_code, so that the
// relative order of items is retained (which isn't the case when mixing ESM imports and // relative order of items is retained (which isn't the case when mixing ESM imports and
// requires). // requires).
self.base self.base.imports.push(
.imports format!(
.push(format!("const {identifier} = require(\"{inner_module_id}\");").into()); "const {identifier} = require(/*turbopackChunkingType: \
shared*/\"{inner_module_id}\");"
)
.into(),
);
let module = self let module = self
.base .base
......
...@@ -99,7 +99,7 @@ impl BaseLoaderTreeBuilder { ...@@ -99,7 +99,7 @@ impl BaseLoaderTreeBuilder {
self.imports.push( self.imports.push(
formatdoc!( formatdoc!(
r#" r#"
const {} = () => require("MODULE_{}"); const {} = () => require(/*turbopackChunkingType: shared*/"MODULE_{}");
"#, "#,
identifier, identifier,
i i
......
...@@ -178,9 +178,7 @@ pub async fn get_app_client_references_chunks( ...@@ -178,9 +178,7 @@ pub async fn get_app_client_references_chunks(
client_references_by_server_component.into_iter() client_references_by_server_component.into_iter()
{ {
let parent_chunk_group = *chunk_group_info let parent_chunk_group = *chunk_group_info
.get_index_of(ChunkGroup::Shared(ResolvedVc::upcast( .get_index_of(ChunkGroup::Shared(ResolvedVc::upcast(server_component)))
server_component.await?.module,
)))
.await?; .await?;
let base_ident = server_component.ident(); let base_ident = server_component.ident();
......
use anyhow::Result; use anyhow::Result;
use indoc::formatdoc; use indoc::formatdoc;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{ use turbopack_core::{
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt}, chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
...@@ -36,20 +36,12 @@ impl NextDynamicEntryModule { ...@@ -36,20 +36,12 @@ impl NextDynamicEntryModule {
} }
} }
fn dynamic_ref_description() -> RcStr {
rcstr!("next/dynamic reference")
}
impl NextDynamicEntryModule { impl NextDynamicEntryModule {
async fn module_reference(&self) -> Result<ResolvedVc<Box<dyn ModuleReference>>> { fn module_reference(&self) -> Vc<Box<dyn ModuleReference>> {
Ok(ResolvedVc::upcast( Vc::upcast(SingleChunkableModuleReference::new(
SingleChunkableModuleReference::new( Vc::upcast(*self.module),
Vc::upcast(*self.module), rcstr!("next/dynamic reference"),
dynamic_ref_description(), ExportUsage::all(),
ExportUsage::all(),
)
.to_resolved()
.await?,
)) ))
} }
} }
...@@ -70,8 +62,9 @@ impl Module for NextDynamicEntryModule { ...@@ -70,8 +62,9 @@ impl Module for NextDynamicEntryModule {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn references(&self) -> Result<Vc<ModuleReferences>> { async fn references(&self) -> Result<Vc<ModuleReferences>> {
Ok(Vc::cell(vec![self.module_reference().await?])) Ok(Vc::cell(vec![self.module_reference().to_resolved().await?]))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects> { fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects> {
// This just exports another import // This just exports another import
...@@ -95,12 +88,7 @@ impl ChunkableModule for NextDynamicEntryModule { ...@@ -95,12 +88,7 @@ impl ChunkableModule for NextDynamicEntryModule {
impl EcmascriptChunkPlaceable for NextDynamicEntryModule { impl EcmascriptChunkPlaceable for NextDynamicEntryModule {
#[turbo_tasks::function] #[turbo_tasks::function]
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
let module_reference: Vc<Box<dyn ModuleReference>> = let module_reference = self.module_reference();
Vc::upcast(SingleChunkableModuleReference::new(
Vc::upcast(*self.module),
dynamic_ref_description(),
ExportUsage::all(),
));
EsmExports::reexport_including_default(module_reference) EsmExports::reexport_including_default(module_reference)
} }
......
pub mod server_component_module; pub mod server_component_module;
pub(crate) mod server_component_reference;
pub(crate) mod server_component_transition; pub(crate) mod server_component_transition;
pub use server_component_transition::NextServerComponentTransition; pub use server_component_transition::NextServerComponentTransition;
...@@ -8,7 +8,8 @@ use turbopack_core::{ ...@@ -8,7 +8,8 @@ use turbopack_core::{
ident::AssetIdent, ident::AssetIdent,
module::{Module, ModuleSideEffects}, module::{Module, ModuleSideEffects},
module_graph::ModuleGraph, module_graph::ModuleGraph,
reference::{ModuleReference, ModuleReferences}, reference::{ModuleReference, ModuleReferences, SingleChunkableModuleReference},
resolve::ExportUsage,
source::OptionSource, source::OptionSource,
}; };
use turbopack_ecmascript::{ use turbopack_ecmascript::{
...@@ -21,8 +22,6 @@ use turbopack_ecmascript::{ ...@@ -21,8 +22,6 @@ use turbopack_ecmascript::{
utils::StringifyJs, utils::StringifyJs,
}; };
use super::server_component_reference::NextServerComponentModuleReference;
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
pub struct NextServerComponentModule { pub struct NextServerComponentModule {
pub module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>, pub module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
...@@ -62,11 +61,11 @@ impl NextServerComponentModule { ...@@ -62,11 +61,11 @@ impl NextServerComponentModule {
} }
impl NextServerComponentModule { impl NextServerComponentModule {
async fn module_reference(&self) -> Result<ResolvedVc<Box<dyn ModuleReference>>> { fn module_reference(&self) -> Vc<Box<dyn ModuleReference>> {
Ok(ResolvedVc::upcast( Vc::upcast(SingleChunkableModuleReference::new(
NextServerComponentModuleReference::new(Vc::upcast(*self.module)) Vc::upcast(*self.module),
.to_resolved() rcstr!("Next.js Server Component"),
.await?, ExportUsage::all(),
)) ))
} }
} }
...@@ -87,8 +86,9 @@ impl Module for NextServerComponentModule { ...@@ -87,8 +86,9 @@ impl Module for NextServerComponentModule {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn references(&self) -> Result<Vc<ModuleReferences>> { async fn references(&self) -> Result<Vc<ModuleReferences>> {
Ok(Vc::cell(vec![self.module_reference().await?])) Ok(Vc::cell(vec![self.module_reference().to_resolved().await?]))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects> { fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects> {
// This just exports another import // This just exports another import
...@@ -112,9 +112,7 @@ impl ChunkableModule for NextServerComponentModule { ...@@ -112,9 +112,7 @@ impl ChunkableModule for NextServerComponentModule {
impl EcmascriptChunkPlaceable for NextServerComponentModule { impl EcmascriptChunkPlaceable for NextServerComponentModule {
#[turbo_tasks::function] #[turbo_tasks::function]
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
let module_reference: Vc<Box<dyn ModuleReference>> = Vc::upcast( let module_reference = self.module_reference();
NextServerComponentModuleReference::new(Vc::upcast(*self.module)),
);
EsmExports::reexport_including_default(module_reference) EsmExports::reexport_including_default(module_reference)
} }
......
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbopack_core::{
chunk::ChunkingType, module::Module, reference::ModuleReference, resolve::ModuleResolveResult,
};
#[turbo_tasks::value]
#[derive(ValueToString)]
#[value_to_string("Next.js Server Component {}", self.asset.ident())]
pub struct NextServerComponentModuleReference {
asset: ResolvedVc<Box<dyn Module>>,
}
#[turbo_tasks::value_impl]
impl NextServerComponentModuleReference {
#[turbo_tasks::function]
pub fn new(asset: ResolvedVc<Box<dyn Module>>) -> Vc<Self> {
NextServerComponentModuleReference { asset }.cell()
}
}
#[turbo_tasks::value_impl]
impl ModuleReference for NextServerComponentModuleReference {
#[turbo_tasks::function]
fn resolve_reference(&self) -> Vc<ModuleResolveResult> {
*ModuleResolveResult::module(self.asset)
}
fn chunking_type(&self) -> Option<ChunkingType> {
Some(ChunkingType::Shared {
inherit_async: true,
merge_tag: None,
})
}
}
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