Skip to content

Commit

Permalink
Turbopack: allow specified AssetIdents for naming chunk groups
Browse files Browse the repository at this point in the history
Previously, in a number of situations, we would implicitly use the entry module’s ident to name the chunk group. This adds the ability to specify a different name.

Now, for evaluated chunk groups for pages, we use the page source’s ident. This prevents changes in loader chunk groups from changing the chunk name, which breaks HMR.

We still use the entry module’s ident for most other cases now, but it is now explicitly provided.

Test Plan: `TURBOPACK=1 pnpm test-dev test/development/acceptance/ReactRefreshModule.test.ts` with #68698 applied
  • Loading branch information
wbinnssmith authored and sokra committed Sep 9, 2024
1 parent 34845e3 commit 1cae581
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ impl PageEndpoint {
let client_chunking_context = this.pages_project.project().client_chunking_context();

let client_chunks = client_chunking_context.evaluated_chunk_group_assets(
client_module.ident(),
self.source().ident(),
this.pages_project
.client_runtime_entries()
.with_entry(client_main_module)
Expand Down
2 changes: 2 additions & 0 deletions crates/next-core/src/next_app/app_client_references_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ pub async fn get_app_client_references_chunks(
ssr_modules,
);
ssr_chunking_context.chunk_group(
ssr_entry_module.ident(),
Vc::upcast(ssr_entry_module),
Value::new(current_ssr_availability_info),
)
Expand Down Expand Up @@ -241,6 +242,7 @@ pub async fn get_app_client_references_chunks(
client_modules,
);
Some(client_chunking_context.chunk_group(
client_entry_module.ident(),
Vc::upcast(client_entry_module),
Value::new(current_client_availability_info),
))
Expand Down
8 changes: 3 additions & 5 deletions turbopack/crates/turbopack-browser/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,11 @@ impl ChunkingContext for BrowserChunkingContext {
#[turbo_tasks::function]
async fn chunk_group(
self: Vc<Self>,
ident: Vc<AssetIdent>,
module: Vc<Box<dyn ChunkableModule>>,
availability_info: Value<AvailabilityInfo>,
) -> Result<Vc<ChunkGroupResult>> {
let span = tracing::info_span!(
"chunking",
module = module.ident().to_string().await?.to_string()
);
let span = tracing::info_span!("chunking", ident = ident.to_string().await?.to_string());
async move {
let this = self.await?;
let input_availability_info = availability_info.into_value();
Expand All @@ -378,7 +376,7 @@ impl ChunkingContext for BrowserChunkingContext {
.collect();

if this.enable_hot_module_replacement {
let mut ident = module.ident();
let mut ident = ident;
match input_availability_info {
AvailabilityInfo::Root => {}
AvailabilityInfo::Untracked => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{ChunkingContext, EvaluatableAssets},
ident::AssetIdent,
module::Module,
output::{OutputAsset, OutputAssets},
version::VersionedContent,
};
Expand Down Expand Up @@ -92,10 +91,6 @@ impl OutputAsset for EcmascriptDevChunkList {
#[turbo_tasks::function]
async fn ident(&self) -> Result<Vc<AssetIdent>> {
let mut ident = self.ident.await?.clone_value();
for &evaluatable_asset in self.evaluatable_assets.await?.iter() {
ident.add_asset(Vc::<RcStr>::default(), evaluatable_asset.ident());
}

ident.add_modifier(modifier());

match self.source {
Expand Down
5 changes: 3 additions & 2 deletions turbopack/crates/turbopack-core/src/chunk/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub trait ChunkingContext {

fn chunk_group(
self: Vc<Self>,
ident: Vc<AssetIdent>,
module: Vc<Box<dyn ChunkableModule>>,
availability_info: Value<AvailabilityInfo>,
) -> Vc<ChunkGroupResult>;
Expand Down Expand Up @@ -189,7 +190,7 @@ impl<T: ChunkingContext + Send + Upcast<Box<dyn ChunkingContext>>> ChunkingConte
self: Vc<Self>,
module: Vc<Box<dyn ChunkableModule>>,
) -> Vc<ChunkGroupResult> {
self.chunk_group(module, Value::new(AvailabilityInfo::Root))
self.chunk_group(module.ident(), module, Value::new(AvailabilityInfo::Root))
}

fn root_chunk_group_assets(
Expand Down Expand Up @@ -309,7 +310,7 @@ async fn chunk_group_assets(
availability_info: Value<AvailabilityInfo>,
) -> Result<Vc<OutputAssets>> {
Ok(chunking_context
.chunk_group(module, availability_info)
.chunk_group(module.ident(), module, availability_info)
.await?
.assets)
}
1 change: 1 addition & 0 deletions turbopack/crates/turbopack-nodejs/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ impl ChunkingContext for NodeJsChunkingContext {
#[turbo_tasks::function]
async fn chunk_group(
self: Vc<Self>,
_ident: Vc<AssetIdent>,
module: Vc<Box<dyn ChunkableModule>>,
availability_info: Value<AvailabilityInfo>,
) -> Result<Vc<ChunkGroupResult>> {
Expand Down

0 comments on commit 1cae581

Please sign in to comment.