Skip to content

Commit

Permalink
Make ChunkItems reference only OutputAssets
Browse files Browse the repository at this point in the history
This:

- Changes `ChunkItem`’s `references` implementation to only allow items to reference `OutputAssets`
- Implements a default `references` that returns an empty set of output assets
- Updates logic for using these output assets in `make_chunk_group`
- [ ] Moves logic for using the inner module’s references into areas that need it
  • Loading branch information
wbinnssmith committed Dec 12, 2024
1 parent 1832c83 commit 3222e22
Show file tree
Hide file tree
Showing 24 changed files with 83 additions and 271 deletions.
5 changes: 0 additions & 5 deletions crates/next-core/src/hmr_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,6 @@ impl ChunkItem for HmrEntryChunkItem {
self.module.ident()
}

#[turbo_tasks::function]
fn references(&self) -> Vc<ModuleReferences> {
self.module.references()
}

#[turbo_tasks::function]
fn ty(&self) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(Vc::<EcmascriptChunkType>::default())
Expand Down
5 changes: 0 additions & 5 deletions crates/next-core/src/next_app/include_modules_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ impl ChunkItem for IncludeModulesChunkItem {
self.module.ident()
}

#[turbo_tasks::function]
fn references(&self) -> Vc<ModuleReferences> {
self.module.references()
}

#[turbo_tasks::function]
fn ty(&self) -> Vc<Box<dyn ChunkType>> {
Vc::upcast(Vc::<EcmascriptChunkType>::default())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,6 @@ impl ChunkItem for ProxyModuleChunkItem {
self.client_proxy_asset.ident()
}

#[turbo_tasks::function]
fn references(&self) -> Vc<ModuleReferences> {
self.client_proxy_asset.references()
}

#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
Vc::upcast(*self.chunking_context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,6 @@ impl ChunkItem for NextServerComponentChunkItem {
self.inner.ident()
}

#[turbo_tasks::function]
fn references(&self) -> Vc<ModuleReferences> {
self.inner.references()
}

#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ impl ChunkItem for NextServerComponentChunkItem {
self.inner.ident()
}

#[turbo_tasks::function]
fn references(&self) -> Vc<ModuleReferences> {
self.inner.references()
}

#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*self.chunking_context
Expand Down
14 changes: 8 additions & 6 deletions turbopack/crates/turbopack-core/src/chunk/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,13 @@ pub async fn make_chunk_group(
.map(|&loader| loader.references())
.try_join()
.await?;
let async_loader_external_module_references = async_loader_references
.iter()
.flat_map(|references| references.iter().copied())
.map(|v| *v)
.collect();
let async_loader_external_module_references = OutputAssets::new(
async_loader_references
.iter()
.flat_map(|references| references.iter().copied())
.map(|v| *v)
.collect(),
);

let mut referenced_output_assets = references_to_output_assets(external_module_references)
.await?
Expand Down Expand Up @@ -185,7 +187,7 @@ pub async fn make_chunk_group(
chunking_context,
Vc::cell(async_loader_chunk_items.into_iter().collect()),
"async-loader-".into(),
references_to_output_assets(async_loader_external_module_references).await?,
async_loader_external_module_references,
)
.await?;

Expand Down
29 changes: 13 additions & 16 deletions turbopack/crates/turbopack-core/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ pub use self::{
evaluate::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets},
};
use crate::{
asset::Asset,
environment::ChunkLoading,
ident::AssetIdent,
module::Module,
output::OutputAssets,
reference::{ModuleReference, ModuleReferences},
asset::Asset, environment::ChunkLoading, ident::AssetIdent, module::Module,
output::OutputAssets, reference::ModuleReference,
};

/// A module id, which can be a number or string
Expand Down Expand Up @@ -361,13 +357,15 @@ async fn graph_node_to_referenced_nodes(
node: ChunkGraphNodeToReferences,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Vc<ChunkGraphEdges>> {
let (parent, references) = match &node {
ChunkGraphNodeToReferences::PassthroughChunkItem(item) => (None, item.references()),
ChunkGraphNodeToReferences::ChunkItem(item) => (Some(*item), item.references()),
let (parent, module_references) = match &node {
ChunkGraphNodeToReferences::PassthroughChunkItem(item) => {
(None, item.module().references())
}
ChunkGraphNodeToReferences::ChunkItem(item) => (Some(*item), item.module().references()),
};

let references = references.await?;
let graph_nodes = references
let module_references = module_references.await?;
let graph_nodes = module_references
.iter()
.map(|reference| async {
let reference = *reference;
Expand Down Expand Up @@ -732,11 +730,10 @@ pub trait ChunkItem {
fn content_ident(self: Vc<Self>) -> Vc<AssetIdent> {
self.asset_ident()
}
/// A [ChunkItem] can describe different `references` than its original
/// [Module].
/// TODO(alexkirsz) This should have a default impl that returns empty
/// references.
fn references(self: Vc<Self>) -> Vc<ModuleReferences>;
/// A [ChunkItem] can reference OutputAssets, unlike [Module]s referencing other [Module]s.
fn references(self: Vc<Self>) -> Vc<OutputAssets> {
OutputAssets::empty()
}

/// The type of chunk this item should be assembled into.
fn ty(self: Vc<Self>) -> Vc<Box<dyn ChunkType>>;
Expand Down
5 changes: 0 additions & 5 deletions turbopack/crates/turbopack-css/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,6 @@ impl ChunkItem for CssModuleChunkItem {
self.module.ident()
}

#[turbo_tasks::function]
fn references(&self) -> Vc<ModuleReferences> {
self.module.references()
}

#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
Vc::upcast(*self.chunking_context)
Expand Down
5 changes: 0 additions & 5 deletions turbopack/crates/turbopack-css/src/module_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,6 @@ impl ChunkItem for ModuleChunkItem {
self.module.ident()
}

#[turbo_tasks::function]
fn references(&self) -> Vc<ModuleReferences> {
self.module.references()
}

#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
Vc::upcast(*self.chunking_context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use turbopack_core::{
ident::AssetIdent,
module::Module,
output::OutputAssets,
reference::{ModuleReferences, SingleOutputAssetReference},
};

use crate::{
Expand Down Expand Up @@ -178,24 +177,8 @@ impl ChunkItem for AsyncLoaderChunkItem {
}

#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<ModuleReferences>> {
let chunks = self.chunks();

Ok(Vc::cell(
chunks
.await?
.iter()
.copied()
.map(|chunk| async move {
Ok(ResolvedVc::upcast(
SingleOutputAssetReference::new(*chunk, chunk_reference_description())
.to_resolved()
.await?,
))
})
.try_join()
.await?,
))
async fn references(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
Ok(self.chunks())
}

#[turbo_tasks::function]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use turbopack_core::{
},
module::Module,
output::{OutputAsset, OutputAssets},
reference::{
ModuleReference, ModuleReferences, SingleModuleReference, SingleOutputAssetReference,
},
reference::{ModuleReference, ModuleReferences, SingleModuleReference},
};

use crate::{
Expand Down Expand Up @@ -209,27 +207,8 @@ impl ChunkItem for ChunkGroupFilesChunkItem {
}

#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<ModuleReferences>> {
let chunks = self.chunks();

Ok(Vc::cell(
chunks
.await?
.iter()
.copied()
.map(|chunk| async move {
Ok(ResolvedVc::upcast(
SingleOutputAssetReference::new(
*chunk,
chunk_group_chunk_reference_description(),
)
.to_resolved()
.await?,
))
})
.try_join()
.await?,
))
async fn references(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
Ok(self.chunks())
}

#[turbo_tasks::function]
Expand Down
5 changes: 0 additions & 5 deletions turbopack/crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,6 @@ impl ChunkItem for ModuleChunkItem {
self.module.ident()
}

#[turbo_tasks::function]
fn references(&self) -> Vc<ModuleReferences> {
self.module.references()
}

#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn ChunkingContext>> {
*ResolvedVc::upcast(self.chunking_context)
Expand Down
26 changes: 4 additions & 22 deletions turbopack/crates/turbopack-ecmascript/src/manifest/chunk_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use turbopack_core::{
chunk::{ChunkData, ChunkItem, ChunkType, ChunkingContext, ChunksData},
ident::AssetIdent,
module::Module,
reference::{ModuleReferences, SingleOutputAssetReference},
output::OutputAssets,
};

use super::chunk_asset::ManifestAsyncModule;
Expand Down Expand Up @@ -78,28 +78,10 @@ impl ChunkItem for ManifestChunkItem {
}

#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<ModuleReferences>> {
let this = self.await?;
let mut references = this.manifest.references().await?.clone_value();

let key = Vc::cell("chunk data reference".into());

async fn references(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
let mut references = vec![];
for chunk_data in &*self.chunks_data().await? {
references.extend(
chunk_data
.references()
.await?
.iter()
.map(|&output_asset| async move {
Ok(ResolvedVc::upcast(
SingleOutputAssetReference::new(*output_asset, key)
.to_resolved()
.await?,
))
})
.try_join()
.await?,
);
references.extend(chunk_data.references().await?.iter());
}

Ok(Vc::cell(references))
Expand Down
43 changes: 4 additions & 39 deletions turbopack/crates/turbopack-ecmascript/src/manifest/loader_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use turbopack_core::{
},
ident::AssetIdent,
module::Module,
reference::{ModuleReference, ModuleReferences, SingleOutputAssetReference},
output::OutputAssets,
};

use super::chunk_asset::ManifestAsyncModule;
Expand Down Expand Up @@ -95,46 +95,11 @@ impl ChunkItem for ManifestLoaderChunkItem {
}

#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<ModuleReferences>> {
async fn references(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
let this = self.await?;

let chunks = this.manifest.manifest_chunks();

let mut references: Vec<ResolvedVc<Box<dyn ModuleReference>>> = chunks
.await?
.iter()
.map(|&chunk| async move {
Ok(ResolvedVc::upcast(
SingleOutputAssetReference::new(
*chunk,
manifest_loader_chunk_reference_description(),
)
.to_resolved()
.await?,
))
})
.try_join()
.await?;

let mut references = (*this.manifest.manifest_chunks().await?).clone();
for chunk_data in &*self.chunks_data().await? {
references.extend(
chunk_data
.references()
.await?
.iter()
.map(|&output_asset| async move {
Ok(ResolvedVc::upcast(
SingleOutputAssetReference::new(
*output_asset,
chunk_data_reference_description(),
)
.to_resolved()
.await?,
))
})
.try_join()
.await?,
);
references.extend(chunk_data.references().await?);
}

Ok(Vc::cell(references))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use turbopack_core::{
chunk::{AsyncModuleInfo, ChunkItem, ChunkType, ChunkableModule, ChunkingContext},
ident::AssetIdent,
module::Module,
reference::{ModuleReference, ModuleReferences},
reference::ModuleReference,
};

use crate::{
Expand Down Expand Up @@ -210,17 +210,17 @@ impl ChunkItem for CachedExternalModuleChunkItem {
self.module.ident()
}

#[turbo_tasks::function]
async fn references(&self) -> Result<Vc<ModuleReferences>> {
let additional_references = &self.module.await?.additional_references;
if !additional_references.is_empty() {
let mut module_references = self.module.references().await?.clone_value();
module_references.extend(additional_references.iter().copied());
Ok(Vc::cell(module_references))
} else {
Ok(self.module.references())
}
}
// #[turbo_tasks::function]
// async fn references(&self) -> Result<Vc<ModuleReferences>> {
// let additional_references = &self.module.await?.additional_references;
// if !additional_references.is_empty() {
// let mut module_references = self.module.references().await?.clone_value();
// module_references.extend(additional_references.iter().copied());
// Ok(Vc::cell(module_references))
// } else {
// Ok(self.module.references())
// }
// }

#[turbo_tasks::function]
fn ty(self: Vc<Self>) -> Vc<Box<dyn ChunkType>> {
Expand Down
Loading

0 comments on commit 3222e22

Please sign in to comment.