From 46bb9b755ec1661d3dea5986572576f7db9280c8 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 18 Jul 2023 13:41:01 +0200 Subject: [PATCH] move references() to specific traits (#5555) ### Description preparation for making different typed references next.js PR: https://github.com/vercel/next.js/pull/52822 --- .../src/ecmascript/node/chunk.rs | 6 +- .../src/ecmascript/node/entry/chunk.rs | 6 +- .../src/ecmascript/node/entry/runtime.rs | 6 +- crates/turbopack-core/src/asset.rs | 10 +-- crates/turbopack-core/src/changed.rs | 51 +++++++++++--- .../src/chunk/available_assets.rs | 1 - crates/turbopack-core/src/chunk/mod.rs | 7 ++ crates/turbopack-core/src/file_source.rs | 8 --- crates/turbopack-core/src/introspect/asset.rs | 22 +++++- crates/turbopack-core/src/module.rs | 11 ++- crates/turbopack-core/src/output.rs | 8 ++- crates/turbopack-core/src/proxied_asset.rs | 14 ++-- crates/turbopack-core/src/reference/mod.rs | 67 +------------------ crates/turbopack-css/src/asset.rs | 16 ++--- crates/turbopack-css/src/chunk/mod.rs | 23 ++++--- .../src/chunk/single_item_chunk/chunk.rs | 22 +++--- crates/turbopack-css/src/global_asset.rs | 14 ++-- crates/turbopack-css/src/module_asset.rs | 16 ++--- crates/turbopack-dev-server/src/html.rs | 16 ++--- crates/turbopack-dev/src/ecmascript/chunk.rs | 6 +- .../src/ecmascript/evaluate/chunk.rs | 6 +- .../src/ecmascript/list/asset.rs | 6 +- crates/turbopack-ecmascript/src/chunk/mod.rs | 36 +++++----- .../src/chunk_group_files_asset.rs | 16 ++--- crates/turbopack-ecmascript/src/lib.rs | 10 +-- .../src/manifest/chunk_asset.rs | 16 ++--- .../src/manifest/chunk_item.rs | 1 - .../src/references/require_context.rs | 16 ++--- .../src/tree_shake/asset.rs | 20 +++--- .../src/tree_shake/chunk_item.rs | 1 - .../src/typescript/mod.rs | 16 ++--- .../turbopack-ecmascript/src/webpack/mod.rs | 10 +-- crates/turbopack-mdx/src/lib.rs | 10 +-- crates/turbopack-node/src/bootstrap.rs | 26 +++---- .../src/render/rendered_source.rs | 1 - .../turbopack-node/src/transforms/postcss.rs | 6 +- crates/turbopack-tests/tests/snapshot.rs | 4 +- crates/turbopack/src/rebase/mod.rs | 16 ++--- 38 files changed, 269 insertions(+), 278 deletions(-) diff --git a/crates/turbopack-build/src/ecmascript/node/chunk.rs b/crates/turbopack-build/src/ecmascript/node/chunk.rs index fe6ec5a7d3d49..a02d464e01d1f 100644 --- a/crates/turbopack-build/src/ecmascript/node/chunk.rs +++ b/crates/turbopack-build/src/ecmascript/node/chunk.rs @@ -68,10 +68,7 @@ impl OutputAsset for EcmascriptBuildNodeChunk { let ident = self.chunk.ident().with_modifier(modifier()); AssetIdent::from_path(self.chunking_context.chunk_path(ident, ".js".to_string())) } -} -#[turbo_tasks::value_impl] -impl Asset for EcmascriptBuildNodeChunk { #[turbo_tasks::function] async fn references(self: Vc) -> Result> { let this = self.await?; @@ -92,7 +89,10 @@ impl Asset for EcmascriptBuildNodeChunk { Ok(Vc::cell(references)) } +} +#[turbo_tasks::value_impl] +impl Asset for EcmascriptBuildNodeChunk { #[turbo_tasks::function] fn content(self: Vc) -> Vc { self.own_content().content() diff --git a/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs b/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs index 7dc325a12988e..f2dac40bb83cc 100644 --- a/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs +++ b/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs @@ -185,10 +185,7 @@ impl OutputAsset for EcmascriptBuildNodeEntryChunk { fn ident(&self) -> Vc { AssetIdent::from_path(self.path) } -} -#[turbo_tasks::value_impl] -impl Asset for EcmascriptBuildNodeEntryChunk { #[turbo_tasks::function] async fn references(self: Vc) -> Result> { let this = self.await?; @@ -212,7 +209,10 @@ impl Asset for EcmascriptBuildNodeEntryChunk { Ok(Vc::cell(references)) } +} +#[turbo_tasks::value_impl] +impl Asset for EcmascriptBuildNodeEntryChunk { #[turbo_tasks::function] async fn content(self: Vc) -> Result> { let code = self.code().await?; diff --git a/crates/turbopack-build/src/ecmascript/node/entry/runtime.rs b/crates/turbopack-build/src/ecmascript/node/entry/runtime.rs index 6f4d2ecb333e4..1c36bfb73fbf2 100644 --- a/crates/turbopack-build/src/ecmascript/node/entry/runtime.rs +++ b/crates/turbopack-build/src/ecmascript/node/entry/runtime.rs @@ -97,10 +97,7 @@ impl OutputAsset for EcmascriptBuildNodeRuntimeChunk { AssetIdent::from_path(self.chunking_context.chunk_path(ident, ".js".to_string())) } -} -#[turbo_tasks::value_impl] -impl Asset for EcmascriptBuildNodeRuntimeChunk { #[turbo_tasks::function] async fn references(self: Vc) -> Result> { let this = self.await?; @@ -116,7 +113,10 @@ impl Asset for EcmascriptBuildNodeRuntimeChunk { Ok(Vc::cell(references)) } +} +#[turbo_tasks::value_impl] +impl Asset for EcmascriptBuildNodeRuntimeChunk { #[turbo_tasks::function] async fn content(self: Vc) -> Result> { let code = self.code().await?; diff --git a/crates/turbopack-core/src/asset.rs b/crates/turbopack-core/src/asset.rs index f548ab8ed640a..e2fbdc7da12ea 100644 --- a/crates/turbopack-core/src/asset.rs +++ b/crates/turbopack-core/src/asset.rs @@ -5,10 +5,7 @@ use turbo_tasks_fs::{ FileContent, FileJsonContent, FileLinesContent, FileSystemPath, LinkContent, LinkType, }; -use crate::{ - reference::AssetReferences, - version::{VersionedAssetContent, VersionedContent}, -}; +use crate::version::{VersionedAssetContent, VersionedContent}; /// A list of [Asset]s #[turbo_tasks::value(transparent)] @@ -34,11 +31,6 @@ pub trait Asset { /// The content of the [Asset]. fn content(self: Vc) -> Vc; - /// Other things (most likely [Asset]s) referenced from this [Asset]. - fn references(self: Vc) -> Vc { - AssetReferences::empty() - } - /// The content of the [Asset] alongside its version. async fn versioned_content(self: Vc) -> Result>> { Ok(Vc::upcast(VersionedAssetContent::new(self.content()))) diff --git a/crates/turbopack-core/src/changed.rs b/crates/turbopack-core/src/changed.rs index ed3cd6557e869..b541eba648265 100644 --- a/crates/turbopack-core/src/changed.rs +++ b/crates/turbopack-core/src/changed.rs @@ -4,12 +4,26 @@ use turbo_tasks::{ Completion, Completions, Vc, }; -use crate::{asset::Asset, output::OutputAssets, reference::all_referenced_assets}; +use crate::{ + asset::Asset, + module::Module, + output::{OutputAsset, OutputAssets}, + reference::{all_referenced_modules, all_referenced_output_assets}, +}; + +async fn get_referenced_output_assets( + parent: Vc>, +) -> Result>> + Send> { + Ok(all_referenced_output_assets(parent) + .await? + .clone_value() + .into_iter()) +} -async fn get_referenced_assets( - parent: Vc>, -) -> Result>> + Send> { - Ok(all_referenced_assets(parent) +async fn get_referenced_modules( + parent: Vc>, +) -> Result>> + Send> { + Ok(all_referenced_modules(parent) .await? .clone_value() .into_iter()) @@ -18,15 +32,34 @@ async fn get_referenced_assets( /// Returns a completion that changes when any content of any asset in the whole /// asset graph changes. #[turbo_tasks::function] -pub async fn any_content_changed(root: Vc>) -> Result> { +pub async fn any_content_changed_of_module(root: Vc>) -> Result> { + let completions = NonDeterministic::new() + .skip_duplicates() + .visit([root], get_referenced_modules) + .await + .completed()? + .into_inner() + .into_iter() + .map(|m| content_changed(Vc::upcast(m))) + .collect(); + + Ok(Vc::::cell(completions).completed()) +} + +/// Returns a completion that changes when any content of any asset in the whole +/// asset graph changes. +#[turbo_tasks::function] +pub async fn any_content_changed_of_output_asset( + root: Vc>, +) -> Result> { let completions = NonDeterministic::new() .skip_duplicates() - .visit([root], get_referenced_assets) + .visit([root], get_referenced_output_assets) .await .completed()? .into_inner() .into_iter() - .map(content_changed) + .map(|m| content_changed(Vc::upcast(m))) .collect(); Ok(Vc::::cell(completions).completed()) @@ -42,7 +75,7 @@ pub async fn any_content_changed_of_output_assets( roots .await? .iter() - .map(|&a| any_content_changed(Vc::upcast(a))) + .map(|&a| any_content_changed_of_output_asset(a)) .collect(), ) .completed()) diff --git a/crates/turbopack-core/src/chunk/available_assets.rs b/crates/turbopack-core/src/chunk/available_assets.rs index df1352e6c1b3c..55b660b751613 100644 --- a/crates/turbopack-core/src/chunk/available_assets.rs +++ b/crates/turbopack-core/src/chunk/available_assets.rs @@ -10,7 +10,6 @@ use turbo_tasks_hash::Xxh3Hash64Hasher; use super::{ChunkableModuleReference, ChunkingType}; use crate::{ - asset::Asset, module::{Module, ModulesSet}, reference::AssetReference, }; diff --git a/crates/turbopack-core/src/chunk/mod.rs b/crates/turbopack-core/src/chunk/mod.rs index a30042f082573..7a2c28a551ce8 100644 --- a/crates/turbopack-core/src/chunk/mod.rs +++ b/crates/turbopack-core/src/chunk/mod.rs @@ -126,11 +126,18 @@ pub trait Chunk: Asset { fn path(self: Vc) -> Vc { self.ident().path() } + /// Returns a list of chunks that should be loaded in parallel to this /// chunk. fn parallel_chunks(self: Vc) -> Vc { Chunks::empty() } + + /// Other things (most likely [Asset]s) referenced from this [Chunk]. + // TODO refactor this to ensure that only [OutputAsset]s can be referenced + fn references(self: Vc) -> Vc { + AssetReferences::empty() + } } /// Aggregated information about a chunk content that can be used by the runtime diff --git a/crates/turbopack-core/src/file_source.rs b/crates/turbopack-core/src/file_source.rs index 6dc899d9366ce..c46891e8d1250 100644 --- a/crates/turbopack-core/src/file_source.rs +++ b/crates/turbopack-core/src/file_source.rs @@ -5,7 +5,6 @@ use turbo_tasks_fs::{FileContent, FileSystemEntryType, FileSystemPath, LinkConte use crate::{ asset::{Asset, AssetContent}, ident::AssetIdent, - reference::AssetReferences, source::Source, }; @@ -53,11 +52,4 @@ impl Asset for FileSource { _ => Err(anyhow::anyhow!("Invalid file type {:?}", file_type)), } } - - #[turbo_tasks::function] - fn references(&self) -> Vc { - // TODO: build input sourcemaps via language specific sourceMappingURL comment - // or parse. - AssetReferences::empty() - } } diff --git a/crates/turbopack-core/src/introspect/asset.rs b/crates/turbopack-core/src/introspect/asset.rs index 9a0d4f3664370..3a888b4b8e37b 100644 --- a/crates/turbopack-core/src/introspect/asset.rs +++ b/crates/turbopack-core/src/introspect/asset.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{bail, Result}; use indexmap::IndexSet; use turbo_tasks::{ValueToString, Vc}; use turbo_tasks_fs::FileContent; @@ -93,8 +93,24 @@ impl Introspectable for IntrospectableAsset { } #[turbo_tasks::function] - fn children(&self) -> Vc { - children_from_asset_references(self.0.references()) + async fn children(&self) -> Result> { + let asset = self.0.resolve().await?; + Ok( + if Vc::try_resolve_downcast::>(asset) + .await? + .is_some() + { + Vc::cell(Default::default()) + } else if let Some(module) = Vc::try_resolve_downcast::>(asset).await? { + children_from_asset_references(module.references()) + } else if let Some(output_asset) = + Vc::try_resolve_downcast::>(asset).await? + { + children_from_asset_references(output_asset.references()) + } else { + bail!("unknown type") + }, + ) } } diff --git a/crates/turbopack-core/src/module.rs b/crates/turbopack-core/src/module.rs index cdfb067f72004..40ca4210d51d9 100644 --- a/crates/turbopack-core/src/module.rs +++ b/crates/turbopack-core/src/module.rs @@ -2,7 +2,10 @@ use anyhow::{bail, Result}; use indexmap::IndexSet; use turbo_tasks::Vc; -use crate::{asset::Asset, ident::AssetIdent, raw_module::RawModule, source::Source}; +use crate::{ + asset::Asset, ident::AssetIdent, raw_module::RawModule, reference::AssetReferences, + source::Source, +}; /// A module. This usually represents parsed source code, which has references /// to other modules. @@ -11,6 +14,12 @@ pub trait Module: Asset { /// The identifier of the [Module]. It's expected to be unique and capture /// all properties of the [Module]. fn ident(&self) -> Vc; + + /// Other things (most likely [Asset]s) referenced from this [Module]. + // TODO refactor this to ensure that only [Module]s can be referenced + fn references(self: Vc) -> Vc { + AssetReferences::empty() + } } #[turbo_tasks::value(transparent)] diff --git a/crates/turbopack-core/src/output.rs b/crates/turbopack-core/src/output.rs index d326a69f1cbd9..051cbce344b2a 100644 --- a/crates/turbopack-core/src/output.rs +++ b/crates/turbopack-core/src/output.rs @@ -2,7 +2,7 @@ use anyhow::{Context, Result}; use indexmap::IndexSet; use turbo_tasks::Vc; -use crate::{asset::Asset, ident::AssetIdent}; +use crate::{asset::Asset, ident::AssetIdent, reference::AssetReferences}; /// An asset that should be outputted, e. g. written to disk or served from a /// server. @@ -12,6 +12,12 @@ pub trait OutputAsset: Asset { /// The identifier of the [OutputAsset]. It's expected to be unique and /// capture all properties of the [OutputAsset]. Only path must be used. fn ident(&self) -> Vc; + + /// Other things (most likely [Asset]s) referenced from this [OutputAsset]. + // TODO refactor this to ensure that only [OutputAsset]s can be referenced + fn references(self: Vc) -> Vc { + AssetReferences::empty() + } } #[turbo_tasks::value(transparent)] diff --git a/crates/turbopack-core/src/proxied_asset.rs b/crates/turbopack-core/src/proxied_asset.rs index 691a9af2bf22d..247e728c7b7ac 100644 --- a/crates/turbopack-core/src/proxied_asset.rs +++ b/crates/turbopack-core/src/proxied_asset.rs @@ -15,7 +15,7 @@ use crate::{ /// Next.js apps. #[turbo_tasks::value] pub struct ProxiedAsset { - asset: Vc>, + asset: Vc>, path: Vc, } @@ -23,7 +23,7 @@ pub struct ProxiedAsset { impl ProxiedAsset { /// Creates a new [`ProxiedAsset`] from an [`Asset`] and a path. #[turbo_tasks::function] - pub fn new(asset: Vc>, path: Vc) -> Vc { + pub fn new(asset: Vc>, path: Vc) -> Vc { ProxiedAsset { asset, path }.cell() } } @@ -34,6 +34,11 @@ impl OutputAsset for ProxiedAsset { fn ident(&self) -> Vc { AssetIdent::from_path(self.path) } + + #[turbo_tasks::function] + fn references(&self) -> Vc { + self.asset.references() + } } #[turbo_tasks::value_impl] @@ -43,11 +48,6 @@ impl Asset for ProxiedAsset { self.asset.content() } - #[turbo_tasks::function] - fn references(&self) -> Vc { - self.asset.references() - } - #[turbo_tasks::function] fn versioned_content(&self) -> Vc> { self.asset.versioned_content() diff --git a/crates/turbopack-core/src/reference/mod.rs b/crates/turbopack-core/src/reference/mod.rs index fa612a40f26cc..88a0c2002fec6 100644 --- a/crates/turbopack-core/src/reference/mod.rs +++ b/crates/turbopack-core/src/reference/mod.rs @@ -4,7 +4,7 @@ use anyhow::Result; use turbo_tasks::{TryJoinIterExt, ValueToString, Vc}; use crate::{ - asset::{Asset, Assets}, + asset::Asset, issue::IssueContextExt, module::{convert_asset_to_module, Module, Modules}, output::{OutputAsset, OutputAssets}, @@ -86,39 +86,6 @@ impl SingleAssetReference { } } -/// Aggregates all [Asset]s referenced by an [Asset]. [AssetReference] -/// This does not include transitively references [Asset]s, but it includes -/// primary and secondary [Asset]s referenced. -/// -/// [Asset]: crate::asset::Asset -#[turbo_tasks::function] -pub async fn all_referenced_assets(asset: Vc>) -> Result> { - let references_set = asset.references().await?; - let mut assets = Vec::new(); - let mut queue = VecDeque::with_capacity(32); - for reference in references_set.iter() { - queue.push_back(reference.resolve_reference()); - } - // that would be non-deterministic: - // while let Some(result) = race_pop(&mut queue).await { - // match &*result? { - while let Some(resolve_result) = queue.pop_front() { - let ResolveResult { - primary, - references, - } = &*resolve_result.await?; - for result in primary { - if let PrimaryResolveResult::Asset(asset) = *result { - assets.push(asset); - } - } - for reference in references { - queue.push_back(reference.resolve_reference()); - } - } - Ok(Vc::cell(assets)) -} - /// Aggregates all [Asset]s referenced by an [Asset]. [AssetReference] /// This does not include transitively references [Asset]s, but it includes /// primary and secondary [Asset]s referenced. @@ -278,38 +245,6 @@ pub async fn primary_referenced_output_assets( Ok(Vc::cell(output_assets)) } -/// Aggregates all primary [Asset]s referenced by an [Asset]. [AssetReference] -/// This does not include transitively references [Asset]s, only includes -/// primary [Asset]s referenced. -/// -/// [Asset]: crate::asset::Asset -#[turbo_tasks::function] -pub async fn primary_referenced_assets(asset: Vc>) -> Result> { - let assets = asset - .references() - .await? - .iter() - .map(|reference| async { - let ResolveResult { primary, .. } = &*reference.resolve_reference().await?; - Ok(primary - .iter() - .filter_map(|result| { - if let PrimaryResolveResult::Asset(asset) = *result { - Some(asset) - } else { - None - } - }) - .collect::>()) - }) - .try_join() - .await? - .into_iter() - .flatten() - .collect(); - Ok(Vc::cell(assets)) -} - /// Aggregates all [Module]s referenced by an [Module] including transitively /// referenced [Module]s. This basically gives all [Module]s in a subgraph /// starting from the passed [Module]. diff --git a/crates/turbopack-css/src/asset.rs b/crates/turbopack-css/src/asset.rs index c0ed045eeb641..90add35589b09 100644 --- a/crates/turbopack-css/src/asset.rs +++ b/crates/turbopack-css/src/asset.rs @@ -87,14 +87,6 @@ impl Module for CssModuleAsset { fn ident(&self) -> Vc { self.source.ident().with_modifier(modifier()) } -} - -#[turbo_tasks::value_impl] -impl Asset for CssModuleAsset { - #[turbo_tasks::function] - fn content(&self) -> Vc { - self.source.content() - } #[turbo_tasks::function] async fn references(self: Vc) -> Result> { @@ -109,6 +101,14 @@ impl Asset for CssModuleAsset { } } +#[turbo_tasks::value_impl] +impl Asset for CssModuleAsset { + #[turbo_tasks::function] + fn content(&self) -> Vc { + self.source.content() + } +} + #[turbo_tasks::value_impl] impl ChunkableModule for CssModuleAsset { #[turbo_tasks::function] diff --git a/crates/turbopack-css/src/chunk/mod.rs b/crates/turbopack-css/src/chunk/mod.rs index 1506fedec56c5..87f2a2357acf0 100644 --- a/crates/turbopack-css/src/chunk/mod.rs +++ b/crates/turbopack-css/src/chunk/mod.rs @@ -295,6 +295,11 @@ impl Chunk for CssChunk { } Ok(Vc::cell(chunks)) } + + #[turbo_tasks::function] + fn references(self: Vc) -> Vc { + OutputAsset::references(self) + } } #[turbo_tasks::value_impl] @@ -385,14 +390,6 @@ impl OutputAsset for CssChunk { this.context.chunk_path(ident, ".css".to_string()), )) } -} - -#[turbo_tasks::value_impl] -impl Asset for CssChunk { - #[turbo_tasks::function] - fn content(self: Vc) -> Vc { - self.chunk_content().content() - } #[turbo_tasks::function] async fn references(self: Vc) -> Result> { @@ -434,6 +431,14 @@ impl Asset for CssChunk { } } +#[turbo_tasks::value_impl] +impl Asset for CssChunk { + #[turbo_tasks::function] + fn content(self: Vc) -> Vc { + self.chunk_content().content() + } +} + #[turbo_tasks::value_impl] impl GenerateSourceMap for CssChunk { #[turbo_tasks::function] @@ -570,7 +575,7 @@ impl Introspectable for CssChunk { #[turbo_tasks::function] async fn children(self: Vc) -> Result> { - let mut children = children_from_asset_references(self.references()) + let mut children = children_from_asset_references(OutputAsset::references(self)) .await? .clone_value(); for &entry in &*self.await?.main_entries.await? { diff --git a/crates/turbopack-css/src/chunk/single_item_chunk/chunk.rs b/crates/turbopack-css/src/chunk/single_item_chunk/chunk.rs index 0fa2c7a00ea1d..a525cda460bb0 100644 --- a/crates/turbopack-css/src/chunk/single_item_chunk/chunk.rs +++ b/crates/turbopack-css/src/chunk/single_item_chunk/chunk.rs @@ -101,17 +101,6 @@ impl OutputAsset for SingleItemCssChunk { ), )) } -} - -#[turbo_tasks::value_impl] -impl Asset for SingleItemCssChunk { - #[turbo_tasks::function] - async fn content(self: Vc) -> Result> { - let code = self.code().await?; - Ok(AssetContent::file( - File::from(code.source_code().clone()).into(), - )) - } #[turbo_tasks::function] async fn references(self: Vc) -> Result> { @@ -130,6 +119,17 @@ impl Asset for SingleItemCssChunk { } } +#[turbo_tasks::value_impl] +impl Asset for SingleItemCssChunk { + #[turbo_tasks::function] + async fn content(self: Vc) -> Result> { + let code = self.code().await?; + Ok(AssetContent::file( + File::from(code.source_code().clone()).into(), + )) + } +} + #[turbo_tasks::value_impl] impl GenerateSourceMap for SingleItemCssChunk { #[turbo_tasks::function] diff --git a/crates/turbopack-css/src/global_asset.rs b/crates/turbopack-css/src/global_asset.rs index 770c232be0a1e..6531ce1452d60 100644 --- a/crates/turbopack-css/src/global_asset.rs +++ b/crates/turbopack-css/src/global_asset.rs @@ -51,6 +51,13 @@ impl Module for GlobalCssAsset { fn ident(&self) -> Vc { self.source.ident().with_modifier(modifier()) } + + #[turbo_tasks::function] + fn references(self: Vc) -> Vc { + Vc::cell(vec![Vc::upcast(InternalCssAssetReference::new( + self.inner(), + ))]) + } } #[turbo_tasks::value_impl] @@ -59,13 +66,6 @@ impl Asset for GlobalCssAsset { fn content(&self) -> Result> { bail!("CSS global asset has no contents") } - - #[turbo_tasks::function] - fn references(self: Vc) -> Vc { - Vc::cell(vec![Vc::upcast(InternalCssAssetReference::new( - self.inner(), - ))]) - } } #[turbo_tasks::function] diff --git a/crates/turbopack-css/src/module_asset.rs b/crates/turbopack-css/src/module_asset.rs index 21f7063d68955..a80b79512ba1e 100644 --- a/crates/turbopack-css/src/module_asset.rs +++ b/crates/turbopack-css/src/module_asset.rs @@ -66,14 +66,6 @@ impl Module for ModuleCssAsset { fn ident(&self) -> Vc { self.source.ident().with_modifier(modifier()) } -} - -#[turbo_tasks::value_impl] -impl Asset for ModuleCssAsset { - #[turbo_tasks::function] - fn content(&self) -> Result> { - bail!("CSS module asset has no contents") - } #[turbo_tasks::function] async fn references(self: Vc) -> Result> { @@ -90,6 +82,14 @@ impl Asset for ModuleCssAsset { } } +#[turbo_tasks::value_impl] +impl Asset for ModuleCssAsset { + #[turbo_tasks::function] + fn content(&self) -> Result> { + bail!("CSS module asset has no contents") + } +} + /// A CSS class that is exported from a CSS module. /// /// See [`ModuleCssClasses`] for more information. diff --git a/crates/turbopack-dev-server/src/html.rs b/crates/turbopack-dev-server/src/html.rs index 4461afb6c689e..17a2a82b19c7b 100644 --- a/crates/turbopack-dev-server/src/html.rs +++ b/crates/turbopack-dev-server/src/html.rs @@ -42,14 +42,6 @@ impl OutputAsset for DevHtmlAsset { fn ident(&self) -> Vc { AssetIdent::from_path(self.path) } -} - -#[turbo_tasks::value_impl] -impl Asset for DevHtmlAsset { - #[turbo_tasks::function] - fn content(self: Vc) -> Vc { - self.html_content().content() - } #[turbo_tasks::function] async fn references(self: Vc) -> Result> { @@ -62,6 +54,14 @@ impl Asset for DevHtmlAsset { } Ok(Vc::cell(references)) } +} + +#[turbo_tasks::value_impl] +impl Asset for DevHtmlAsset { + #[turbo_tasks::function] + fn content(self: Vc) -> Vc { + self.html_content().content() + } #[turbo_tasks::function] fn versioned_content(self: Vc) -> Vc> { diff --git a/crates/turbopack-dev/src/ecmascript/chunk.rs b/crates/turbopack-dev/src/ecmascript/chunk.rs index fc67f02c33ba8..fe62db1ac0048 100644 --- a/crates/turbopack-dev/src/ecmascript/chunk.rs +++ b/crates/turbopack-dev/src/ecmascript/chunk.rs @@ -80,10 +80,7 @@ impl OutputAsset for EcmascriptDevChunk { let ident = self.chunk.ident().with_modifier(modifier()); AssetIdent::from_path(self.chunking_context.chunk_path(ident, ".js".to_string())) } -} -#[turbo_tasks::value_impl] -impl Asset for EcmascriptDevChunk { #[turbo_tasks::function] async fn references(self: Vc) -> Result> { let this = self.await?; @@ -104,7 +101,10 @@ impl Asset for EcmascriptDevChunk { Ok(Vc::cell(references)) } +} +#[turbo_tasks::value_impl] +impl Asset for EcmascriptDevChunk { #[turbo_tasks::function] fn content(self: Vc) -> Vc { self.own_content().content() diff --git a/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs b/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs index 4566c21d3c1c4..8f1e86113f933 100644 --- a/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs +++ b/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs @@ -199,10 +199,7 @@ impl OutputAsset for EcmascriptDevEvaluateChunk { self.chunking_context.chunk_path(ident, ".js".to_string()), )) } -} -#[turbo_tasks::value_impl] -impl Asset for EcmascriptDevEvaluateChunk { #[turbo_tasks::function] async fn references(self: Vc) -> Result> { let this = self.await?; @@ -222,7 +219,10 @@ impl Asset for EcmascriptDevEvaluateChunk { Ok(Vc::cell(references)) } +} +#[turbo_tasks::value_impl] +impl Asset for EcmascriptDevEvaluateChunk { #[turbo_tasks::function] async fn content(self: Vc) -> Result> { let code = self.code().await?; diff --git a/crates/turbopack-dev/src/ecmascript/list/asset.rs b/crates/turbopack-dev/src/ecmascript/list/asset.rs index 2518c84d9f3aa..936e0cd9b0b86 100644 --- a/crates/turbopack-dev/src/ecmascript/list/asset.rs +++ b/crates/turbopack-dev/src/ecmascript/list/asset.rs @@ -92,10 +92,7 @@ impl OutputAsset for EcmascriptDevChunkList { self.chunking_context.chunk_path(ident, ".js".to_string()), )) } -} -#[turbo_tasks::value_impl] -impl Asset for EcmascriptDevChunkList { #[turbo_tasks::function] async fn references(&self) -> Result> { Ok(Vc::cell( @@ -111,7 +108,10 @@ impl Asset for EcmascriptDevChunkList { .collect(), )) } +} +#[turbo_tasks::value_impl] +impl Asset for EcmascriptDevChunkList { #[turbo_tasks::function] fn content(self: Vc) -> Vc { self.own_content().content() diff --git a/crates/turbopack-ecmascript/src/chunk/mod.rs b/crates/turbopack-ecmascript/src/chunk/mod.rs index fb7e2797d0e48..296ae0e756474 100644 --- a/crates/turbopack-ecmascript/src/chunk/mod.rs +++ b/crates/turbopack-ecmascript/src/chunk/mod.rs @@ -311,6 +311,24 @@ impl Chunk for EcmascriptChunk { } Ok(Vc::cell(chunks)) } + + #[turbo_tasks::function] + async fn references(self: Vc) -> Result> { + let this = self.await?; + let content = ecmascript_chunk_content( + this.context, + this.main_entries, + this.omit_entries, + Value::new(this.availability_info), + ) + .await?; + let mut references = Vec::new(); + for r in content.external_asset_references.iter() { + references.push(*r); + } + + Ok(Vc::cell(references)) + } } #[turbo_tasks::value_impl] @@ -375,24 +393,6 @@ impl Asset for EcmascriptChunk { fn content(self: Vc) -> Result> { bail!("EcmascriptChunk::content() is not implemented") } - - #[turbo_tasks::function] - async fn references(self: Vc) -> Result> { - let this = self.await?; - let content = ecmascript_chunk_content( - this.context, - this.main_entries, - this.omit_entries, - Value::new(this.availability_info), - ) - .await?; - let mut references = Vec::new(); - for r in content.external_asset_references.iter() { - references.push(*r); - } - - Ok(Vc::cell(references)) - } } #[turbo_tasks::function] diff --git a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs index 5167044df5e74..bc98b41e4ec68 100644 --- a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs +++ b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs @@ -58,14 +58,6 @@ impl Module for ChunkGroupFilesAsset { fn ident(&self) -> Vc { self.module.ident().with_modifier(modifier()) } -} - -#[turbo_tasks::value_impl] -impl Asset for ChunkGroupFilesAsset { - #[turbo_tasks::function] - fn content(&self) -> Vc { - AssetContent::file(File::from("// Chunking only content".to_string()).into()) - } #[turbo_tasks::function] async fn references(&self) -> Result> { @@ -87,6 +79,14 @@ impl Asset for ChunkGroupFilesAsset { } } +#[turbo_tasks::value_impl] +impl Asset for ChunkGroupFilesAsset { + #[turbo_tasks::function] + fn content(&self) -> Vc { + AssetContent::file(File::from("// Chunking only content".to_string()).into()) + } +} + #[turbo_tasks::value_impl] impl ChunkableModule for ChunkGroupFilesAsset { #[turbo_tasks::function] diff --git a/crates/turbopack-ecmascript/src/lib.rs b/crates/turbopack-ecmascript/src/lib.rs index ddbef65e1936f..f2219c1a04f31 100644 --- a/crates/turbopack-ecmascript/src/lib.rs +++ b/crates/turbopack-ecmascript/src/lib.rs @@ -390,6 +390,11 @@ impl Module for EcmascriptModuleAsset { Ok(self.source.ident().with_modifier(modifier())) } } + + #[turbo_tasks::function] + async fn references(self: Vc) -> Result> { + Ok(self.failsafe_analyze().await?.references) + } } #[turbo_tasks::value_impl] @@ -398,11 +403,6 @@ impl Asset for EcmascriptModuleAsset { fn content(&self) -> Vc { self.source.content() } - - #[turbo_tasks::function] - async fn references(self: Vc) -> Result> { - Ok(self.failsafe_analyze().await?.references) - } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs index dc969b6446d2e..0e929bd23fa5f 100644 --- a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs +++ b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs @@ -88,14 +88,6 @@ impl Module for ManifestChunkAsset { fn ident(&self) -> Vc { self.asset.ident().with_modifier(modifier()) } -} - -#[turbo_tasks::value_impl] -impl Asset for ManifestChunkAsset { - #[turbo_tasks::function] - fn content(&self) -> Vc { - todo!() - } #[turbo_tasks::function] async fn references(self: Vc) -> Result> { @@ -117,6 +109,14 @@ impl Asset for ManifestChunkAsset { } } +#[turbo_tasks::value_impl] +impl Asset for ManifestChunkAsset { + #[turbo_tasks::function] + fn content(&self) -> Vc { + todo!() + } +} + #[turbo_tasks::value_impl] impl ChunkableModule for ManifestChunkAsset { #[turbo_tasks::function] diff --git a/crates/turbopack-ecmascript/src/manifest/chunk_item.rs b/crates/turbopack-ecmascript/src/manifest/chunk_item.rs index 992fd3403527b..4a0bf6272d8f8 100644 --- a/crates/turbopack-ecmascript/src/manifest/chunk_item.rs +++ b/crates/turbopack-ecmascript/src/manifest/chunk_item.rs @@ -2,7 +2,6 @@ use anyhow::Result; use indoc::formatdoc; use turbo_tasks::{TryJoinIterExt, Vc}; use turbopack_core::{ - asset::Asset, chunk::{ChunkData, ChunkItem, ChunkingContext, ChunksData}, ident::AssetIdent, module::Module, diff --git a/crates/turbopack-ecmascript/src/references/require_context.rs b/crates/turbopack-ecmascript/src/references/require_context.rs index e2eba505a7b6b..6586800bc8639 100644 --- a/crates/turbopack-ecmascript/src/references/require_context.rs +++ b/crates/turbopack-ecmascript/src/references/require_context.rs @@ -351,14 +351,6 @@ impl Module for RequireContextAsset { .ident() .with_modifier(modifier(self.dir.clone(), self.include_subdirs)) } -} - -#[turbo_tasks::value_impl] -impl Asset for RequireContextAsset { - #[turbo_tasks::function] - fn content(&self) -> Vc { - unimplemented!() - } #[turbo_tasks::function] async fn references(&self) -> Result> { @@ -372,6 +364,14 @@ impl Asset for RequireContextAsset { } } +#[turbo_tasks::value_impl] +impl Asset for RequireContextAsset { + #[turbo_tasks::function] + fn content(&self) -> Vc { + unimplemented!() + } +} + #[turbo_tasks::value_impl] impl ChunkableModule for RequireContextAsset { #[turbo_tasks::function] diff --git a/crates/turbopack-ecmascript/src/tree_shake/asset.rs b/crates/turbopack-ecmascript/src/tree_shake/asset.rs index d281ba38d2d0d..f55605727c332 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/asset.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/asset.rs @@ -51,16 +51,6 @@ impl Module for EcmascriptModulePartAsset { Ok(inner.with_part(self.part)) } -} - -#[turbo_tasks::value_impl] -impl Asset for EcmascriptModulePartAsset { - #[turbo_tasks::function] - fn content(&self) -> Vc { - // This is not reachable because EcmascriptModulePartAsset implements - // ChunkableModule and ChunkableModule::as_chunk is called instead. - todo!("EcmascriptModulePartAsset::content is not implemented") - } #[turbo_tasks::function] async fn references(&self) -> Result> { @@ -106,6 +96,16 @@ impl Asset for EcmascriptModulePartAsset { } } +#[turbo_tasks::value_impl] +impl Asset for EcmascriptModulePartAsset { + #[turbo_tasks::function] + fn content(&self) -> Vc { + // This is not reachable because EcmascriptModulePartAsset implements + // ChunkableModule and ChunkableModule::as_chunk is called instead. + todo!("EcmascriptModulePartAsset::content is not implemented") + } +} + #[turbo_tasks::value_impl] impl EcmascriptChunkPlaceable for EcmascriptModulePartAsset { #[turbo_tasks::function] diff --git a/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs b/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs index a3f08b173e48e..e9f9606be71a8 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs @@ -1,7 +1,6 @@ use anyhow::Result; use turbo_tasks::{Value, Vc}; use turbopack_core::{ - asset::Asset, chunk::{availability_info::AvailabilityInfo, ChunkItem}, ident::AssetIdent, module::Module, diff --git a/crates/turbopack-ecmascript/src/typescript/mod.rs b/crates/turbopack-ecmascript/src/typescript/mod.rs index 841df81fa2f1a..e06008d5d93d6 100644 --- a/crates/turbopack-ecmascript/src/typescript/mod.rs +++ b/crates/turbopack-ecmascript/src/typescript/mod.rs @@ -44,14 +44,6 @@ impl Module for TsConfigModuleAsset { fn ident(&self) -> Vc { self.source.ident() } -} - -#[turbo_tasks::value_impl] -impl Asset for TsConfigModuleAsset { - #[turbo_tasks::function] - fn content(&self) -> Vc { - self.source.content() - } #[turbo_tasks::function] async fn references(&self) -> Result> { @@ -157,6 +149,14 @@ impl Asset for TsConfigModuleAsset { } } +#[turbo_tasks::value_impl] +impl Asset for TsConfigModuleAsset { + #[turbo_tasks::function] + fn content(&self) -> Vc { + self.source.content() + } +} + #[turbo_tasks::value] #[derive(Hash, Debug)] pub struct CompilerReference { diff --git a/crates/turbopack-ecmascript/src/webpack/mod.rs b/crates/turbopack-ecmascript/src/webpack/mod.rs index 4c65daff8d6f8..27d1fb37fb118 100644 --- a/crates/turbopack-ecmascript/src/webpack/mod.rs +++ b/crates/turbopack-ecmascript/src/webpack/mod.rs @@ -57,6 +57,11 @@ impl Module for WebpackModuleAsset { fn ident(&self) -> Vc { self.source.ident().with_modifier(modifier()) } + + #[turbo_tasks::function] + fn references(&self) -> Vc { + module_references(self.source, self.runtime, self.transforms) + } } #[turbo_tasks::value_impl] @@ -65,11 +70,6 @@ impl Asset for WebpackModuleAsset { fn content(&self) -> Vc { self.source.content() } - - #[turbo_tasks::function] - fn references(&self) -> Vc { - module_references(self.source, self.runtime, self.transforms) - } } #[turbo_tasks::value(shared)] diff --git a/crates/turbopack-mdx/src/lib.rs b/crates/turbopack-mdx/src/lib.rs index e5a4c064fad81..5acf2221bfe6f 100644 --- a/crates/turbopack-mdx/src/lib.rs +++ b/crates/turbopack-mdx/src/lib.rs @@ -171,6 +171,11 @@ impl Module for MdxModuleAsset { fn ident(&self) -> Vc { self.source.ident().with_modifier(modifier()) } + + #[turbo_tasks::function] + async fn references(self: Vc) -> Result> { + Ok(self.failsafe_analyze().await?.references) + } } #[turbo_tasks::value_impl] @@ -179,11 +184,6 @@ impl Asset for MdxModuleAsset { fn content(&self) -> Vc { self.source.content() } - - #[turbo_tasks::function] - async fn references(self: Vc) -> Result> { - Ok(self.failsafe_analyze().await?.references) - } } #[turbo_tasks::value_impl] diff --git a/crates/turbopack-node/src/bootstrap.rs b/crates/turbopack-node/src/bootstrap.rs index a31ac20947591..ed076799251ec 100644 --- a/crates/turbopack-node/src/bootstrap.rs +++ b/crates/turbopack-node/src/bootstrap.rs @@ -38,6 +38,19 @@ impl OutputAsset for NodeJsBootstrapAsset { fn ident(&self) -> Vc { AssetIdent::from_path(self.path) } + + #[turbo_tasks::function] + async fn references(&self) -> Result> { + let chunks = self.chunks().await?; + let mut references = Vec::new(); + for &chunk in chunks.iter() { + references.push(Vc::upcast(SingleAssetReference::new( + Vc::upcast(chunk), + node_js_bootstrap_chunk_reference_description(), + ))); + } + Ok(Vc::cell(references)) + } } #[turbo_tasks::value_impl] @@ -61,17 +74,4 @@ impl Asset for NodeJsBootstrapAsset { Ok(AssetContent::file(File::from(output).into())) } - - #[turbo_tasks::function] - async fn references(&self) -> Result> { - let chunks = self.chunks().await?; - let mut references = Vec::new(); - for &chunk in chunks.iter() { - references.push(Vc::upcast(SingleAssetReference::new( - Vc::upcast(chunk), - node_js_bootstrap_chunk_reference_description(), - ))); - } - Ok(Vc::cell(references)) - } } diff --git a/crates/turbopack-node/src/render/rendered_source.rs b/crates/turbopack-node/src/render/rendered_source.rs index a0be20b2612ca..596511a8ccdbd 100644 --- a/crates/turbopack-node/src/render/rendered_source.rs +++ b/crates/turbopack-node/src/render/rendered_source.rs @@ -5,7 +5,6 @@ use turbo_tasks::{TryJoinIterExt, Value, Vc}; use turbo_tasks_env::ProcessEnv; use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ - asset::Asset, introspect::{asset::IntrospectableAsset, Introspectable, IntrospectableChildren}, issue::IssueContextExt, module::Module, diff --git a/crates/turbopack-node/src/transforms/postcss.rs b/crates/turbopack-node/src/transforms/postcss.rs index 82aedac95381a..c563e9244d279 100644 --- a/crates/turbopack-node/src/transforms/postcss.rs +++ b/crates/turbopack-node/src/transforms/postcss.rs @@ -8,7 +8,7 @@ use turbo_tasks_fs::{ }; use turbopack_core::{ asset::{Asset, AssetContent}, - changed::any_content_changed, + changed::any_content_changed_of_module, context::AssetContext, file_source::FileSource, ident::AssetIdent, @@ -149,10 +149,10 @@ async fn extra_configs( .map(|path| async move { Ok( matches!(&*path.get_type().await?, FileSystemEntryType::File).then(|| { - any_content_changed(Vc::upcast(context.process( + any_content_changed_of_module(context.process( Vc::upcast(FileSource::new(path)), Value::new(ReferenceType::Internal(InnerAssets::empty())), - ))) + )) }), ) }) diff --git a/crates/turbopack-tests/tests/snapshot.rs b/crates/turbopack-tests/tests/snapshot.rs index dce09b346c44e..f0b8834c8cef4 100644 --- a/crates/turbopack-tests/tests/snapshot.rs +++ b/crates/turbopack-tests/tests/snapshot.rs @@ -39,7 +39,7 @@ use turbopack_core::{ issue::{Issue, IssueContextExt}, module::Module, output::OutputAsset, - reference::all_referenced_assets, + reference::all_referenced_output_assets, reference_type::{EntryReferenceSubType, ReferenceType}, source::Source, }; @@ -409,7 +409,7 @@ async fn walk_asset( } queue.extend( - all_referenced_assets(Vc::upcast(asset)) + all_referenced_output_assets(asset) .await? .iter() .copied() diff --git a/crates/turbopack/src/rebase/mod.rs b/crates/turbopack/src/rebase/mod.rs index 1cf960eaf4b21..a5864ad42241d 100644 --- a/crates/turbopack/src/rebase/mod.rs +++ b/crates/turbopack/src/rebase/mod.rs @@ -48,14 +48,6 @@ impl OutputAsset for RebasedAsset { self.output_dir, )) } -} - -#[turbo_tasks::value_impl] -impl Asset for RebasedAsset { - #[turbo_tasks::function] - fn content(&self) -> Vc { - self.source.content() - } #[turbo_tasks::function] async fn references(&self) -> Result> { @@ -75,6 +67,14 @@ impl Asset for RebasedAsset { } } +#[turbo_tasks::value_impl] +impl Asset for RebasedAsset { + #[turbo_tasks::function] + fn content(&self) -> Vc { + self.source.content() + } +} + #[turbo_tasks::value(shared)] struct RebasedAssetReference { reference: Vc>,