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 {
.take(server_component_entries.len().saturating_sub(1))
{
let graph = SingleModuleGraph::new_with_entries_visited_intern(
// This should really be ChunkGroupEntry::Shared(module.await?.module),
// but that breaks everything for some reason.
vec![ChunkGroupEntry::Entry(vec![ResolvedVc::upcast(*module)])],
vec![ChunkGroupEntry::Shared(ResolvedVc::upcast(*module))],
visited_modules,
should_trace,
should_read_binding_usage,
......@@ -1895,9 +1893,7 @@ impl AppEndpoint {
async {
let chunk_group = chunking_context.chunk_group(
server_component.ident(),
ChunkGroup::Shared(ResolvedVc::upcast(
server_component.await?.module,
)),
ChunkGroup::Shared(ResolvedVc::upcast(server_component)),
module_graph,
current_chunk_group.await?.availability_info,
);
......@@ -2157,9 +2153,9 @@ impl Endpoint for AppEndpoint {
.await?,
);
Ok(Vc::cell(vec![ChunkGroupEntry::Entry(vec![
Ok(Vc::cell(vec![ChunkGroupEntry::Shared(
server_actions_loader,
])]))
)]))
}
#[turbo_tasks::function]
......
......@@ -187,9 +187,13 @@ impl AppPageLoaderTreeBuilder {
// 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 requires).
self.base
.imports
.push(format!("const {identifier} = require(\"{inner_module_id}\");").into());
self.base.imports.push(
format!(
"const {identifier} = require(/*turbopackChunkingType: \
shared*/\"{inner_module_id}\");"
)
.into(),
);
let source = dynamic_image_metadata_source(
*ResolvedVc::upcast(self.base.module_asset_context),
......@@ -234,9 +238,13 @@ impl AppPageLoaderTreeBuilder {
// 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
// requires).
self.base
.imports
.push(format!("const {identifier} = require(\"{inner_module_id}\");").into());
self.base.imports.push(
format!(
"const {identifier} = require(/*turbopackChunkingType: \
shared*/\"{inner_module_id}\");"
)
.into(),
);
let module = StructuredImageModuleType::create_module(
Vc::upcast(FileSource::new(path.clone())),
BlurPlaceholderMode::None,
......@@ -295,9 +303,13 @@ impl AppPageLoaderTreeBuilder {
// 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
// requires).
self.base
.imports
.push(format!("const {identifier} = require(\"{inner_module_id}\");").into());
self.base.imports.push(
format!(
"const {identifier} = require(/*turbopackChunkingType: \
shared*/\"{inner_module_id}\");"
)
.into(),
);
let module = self
.base
......
......@@ -99,7 +99,7 @@ impl BaseLoaderTreeBuilder {
self.imports.push(
formatdoc!(
r#"
const {} = () => require("MODULE_{}");
const {} = () => require(/*turbopackChunkingType: shared*/"MODULE_{}");
"#,
identifier,
i
......
......@@ -178,9 +178,7 @@ pub async fn get_app_client_references_chunks(
client_references_by_server_component.into_iter()
{
let parent_chunk_group = *chunk_group_info
.get_index_of(ChunkGroup::Shared(ResolvedVc::upcast(
server_component.await?.module,
)))
.get_index_of(ChunkGroup::Shared(ResolvedVc::upcast(server_component)))
.await?;
let base_ident = server_component.ident();
......
use anyhow::Result;
use indoc::formatdoc;
use turbo_rcstr::{RcStr, rcstr};
use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, Vc};
use turbopack_core::{
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
......@@ -36,20 +36,12 @@ impl NextDynamicEntryModule {
}
}
fn dynamic_ref_description() -> RcStr {
rcstr!("next/dynamic reference")
}
impl NextDynamicEntryModule {
async fn module_reference(&self) -> Result<ResolvedVc<Box<dyn ModuleReference>>> {
Ok(ResolvedVc::upcast(
SingleChunkableModuleReference::new(
Vc::upcast(*self.module),
dynamic_ref_description(),
ExportUsage::all(),
)
.to_resolved()
.await?,
fn module_reference(&self) -> Vc<Box<dyn ModuleReference>> {
Vc::upcast(SingleChunkableModuleReference::new(
Vc::upcast(*self.module),
rcstr!("next/dynamic reference"),
ExportUsage::all(),
))
}
}
......@@ -70,8 +62,9 @@ impl Module for NextDynamicEntryModule {
#[turbo_tasks::function]
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]
fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects> {
// This just exports another import
......@@ -95,12 +88,7 @@ impl ChunkableModule for NextDynamicEntryModule {
impl EcmascriptChunkPlaceable for NextDynamicEntryModule {
#[turbo_tasks::function]
fn get_exports(&self) -> Vc<EcmascriptExports> {
let module_reference: Vc<Box<dyn ModuleReference>> =
Vc::upcast(SingleChunkableModuleReference::new(
Vc::upcast(*self.module),
dynamic_ref_description(),
ExportUsage::all(),
));
let module_reference = self.module_reference();
EsmExports::reexport_including_default(module_reference)
}
......
pub mod server_component_module;
pub(crate) mod server_component_reference;
pub(crate) mod server_component_transition;
pub use server_component_transition::NextServerComponentTransition;
......@@ -8,7 +8,8 @@ use turbopack_core::{
ident::AssetIdent,
module::{Module, ModuleSideEffects},
module_graph::ModuleGraph,
reference::{ModuleReference, ModuleReferences},
reference::{ModuleReference, ModuleReferences, SingleChunkableModuleReference},
resolve::ExportUsage,
source::OptionSource,
};
use turbopack_ecmascript::{
......@@ -21,8 +22,6 @@ use turbopack_ecmascript::{
utils::StringifyJs,
};
use super::server_component_reference::NextServerComponentModuleReference;
#[turbo_tasks::value(shared)]
pub struct NextServerComponentModule {
pub module: ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>,
......@@ -62,11 +61,11 @@ impl NextServerComponentModule {
}
impl NextServerComponentModule {
async fn module_reference(&self) -> Result<ResolvedVc<Box<dyn ModuleReference>>> {
Ok(ResolvedVc::upcast(
NextServerComponentModuleReference::new(Vc::upcast(*self.module))
.to_resolved()
.await?,
fn module_reference(&self) -> Vc<Box<dyn ModuleReference>> {
Vc::upcast(SingleChunkableModuleReference::new(
Vc::upcast(*self.module),
rcstr!("Next.js Server Component"),
ExportUsage::all(),
))
}
}
......@@ -87,8 +86,9 @@ impl Module for NextServerComponentModule {
#[turbo_tasks::function]
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]
fn side_effects(self: Vc<Self>) -> Vc<ModuleSideEffects> {
// This just exports another import
......@@ -112,9 +112,7 @@ impl ChunkableModule for NextServerComponentModule {
impl EcmascriptChunkPlaceable for NextServerComponentModule {
#[turbo_tasks::function]
fn get_exports(&self) -> Vc<EcmascriptExports> {
let module_reference: Vc<Box<dyn ModuleReference>> = Vc::upcast(
NextServerComponentModuleReference::new(Vc::upcast(*self.module)),
);
let module_reference = self.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