diff --git a/crates/node-file-trace/src/lib.rs b/crates/node-file-trace/src/lib.rs index 9279d094b679c..8861b1d2c9c53 100644 --- a/crates/node-file-trace/src/lib.rs +++ b/crates/node-file-trace/src/lib.rs @@ -40,13 +40,14 @@ use turbopack::{ }; use turbopack_cli_utils::issue::{ConsoleUi, IssueSeverityCliOption, LogOptions}; use turbopack_core::{ - asset::{Asset, Assets}, compile_time_info::CompileTimeInfo, context::AssetContext, environment::{Environment, ExecutionEnvironment, NodeJsEnvironment}, file_source::FileSource, issue::{IssueContextExt, IssueReporter, IssueSeverity}, - reference::all_assets, + module::{Module, Modules}, + output::OutputAsset, + reference::all_modules, resolve::options::{ImportMapping, ResolvedMap}, }; @@ -207,23 +208,23 @@ async fn create_fs(name: &str, context: &str, watch: bool) -> Result>, result: Vc, - list: &mut Vec>>, + list: &mut Vec>>, ) -> Result<()> { let result = result.await?; for entry in result.results.values() { if let DirectoryEntry::File(path) = entry { let source = Vc::upcast(FileSource::new(*path)); - list.push(Vc::upcast(context.process( + list.push(context.process( source, Value::new(turbopack_core::reference_type::ReferenceType::Undefined), - ))); + )); } } for result in result.inner.values() { fn recurse<'a>( context: Vc>, result: Vc, - list: &'a mut Vec>>, + list: &'a mut Vec>>, ) -> Pin> + Send + 'a>> { Box::pin(add_glob_results(context, result, list)) } @@ -242,7 +243,7 @@ async fn input_to_modules( context: String, module_options: TransientInstance, resolve_options: TransientInstance, -) -> Result> { +) -> Result> { let root = fs.root(); let process_cwd = process_cwd .clone() @@ -259,13 +260,9 @@ async fn input_to_modules( for input in input { if exact { let source = Vc::upcast(FileSource::new(root.join(input))); - list.push(Vc::upcast( - context - .process( - source, - Value::new(turbopack_core::reference_type::ReferenceType::Undefined), - ) - .into(), + list.push(context.process( + source, + Value::new(turbopack_core::reference_type::ReferenceType::Undefined), )); } else { let glob = Glob::new(input); @@ -564,7 +561,7 @@ async fn main_operation( ) .await?; for module in modules.iter() { - let set = all_assets(*module) + let set = all_modules(*module) .issue_context(module.ident().path(), "gathering list of assets") .await?; for asset in set.await?.iter() { diff --git a/crates/node-file-trace/src/nft_json.rs b/crates/node-file-trace/src/nft_json.rs index b07f599bc38db..474d988bca631 100644 --- a/crates/node-file-trace/src/nft_json.rs +++ b/crates/node-file-trace/src/nft_json.rs @@ -5,28 +5,26 @@ use turbo_tasks_fs::{File, FileSystem}; use turbopack_core::{ asset::{Asset, AssetContent}, ident::AssetIdent, + module::Module, output::OutputAsset, - reference::all_assets, + reference::all_modules, }; #[turbo_tasks::value(shared)] pub struct NftJsonAsset { - entry: Vc>, + entry: Vc>, } #[turbo_tasks::value_impl] impl NftJsonAsset { #[turbo_tasks::function] - pub fn new(entry: Vc>) -> Vc { - Self::cell(NftJsonAsset { entry }) + pub fn new(entry: Vc>) -> Vc { + NftJsonAsset { entry }.cell() } } #[turbo_tasks::value_impl] -impl OutputAsset for NftJsonAsset {} - -#[turbo_tasks::value_impl] -impl Asset for NftJsonAsset { +impl OutputAsset for NftJsonAsset { #[turbo_tasks::function] async fn ident(&self) -> Result> { let path = self.entry.ident().path().await?; @@ -34,7 +32,10 @@ impl Asset for NftJsonAsset { path.fs.root().join(format!("{}.nft.json", path.path)), )) } +} +#[turbo_tasks::value_impl] +impl Asset for NftJsonAsset { #[turbo_tasks::function] async fn content(&self) -> Result> { let context = self.entry.ident().path().parent().await?; @@ -42,7 +43,7 @@ impl Asset for NftJsonAsset { let entry_path = &*self.entry.ident().path().await?; let mut result = Vec::new(); if let Some(self_path) = context.get_relative_path_to(entry_path) { - let set = all_assets(self.entry); + let set = all_modules(self.entry); for asset in set.await?.iter() { let path = asset.ident().path().await?; if let Some(rel_path) = context.get_relative_path_to(&path) { diff --git a/crates/turbopack-build/src/chunking_context.rs b/crates/turbopack-build/src/chunking_context.rs index afefc701c3670..6da253d2d63e9 100644 --- a/crates/turbopack-build/src/chunking_context.rs +++ b/crates/turbopack-build/src/chunking_context.rs @@ -6,10 +6,10 @@ use turbo_tasks::{ }; use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ - asset::Asset, chunk::{Chunk, ChunkableModule, ChunkingContext, Chunks, EvaluatableAssets}, environment::Environment, ident::AssetIdent, + module::Module, output::{OutputAsset, OutputAssets}, }; use turbopack_css::chunk::CssChunk; @@ -225,15 +225,15 @@ impl ChunkingContext for BuildChunkingContext { } #[turbo_tasks::function] - fn reference_chunk_source_maps(&self, _chunk: Vc>) -> Vc { + fn reference_chunk_source_maps(&self, _chunk: Vc>) -> Vc { Vc::cell(true) } #[turbo_tasks::function] async fn can_be_in_same_chunk( &self, - asset_a: Vc>, - asset_b: Vc>, + asset_a: Vc>, + asset_b: Vc>, ) -> Result> { let parent_dir = asset_a.ident().path().parent().await?; diff --git a/crates/turbopack-build/src/ecmascript/node/chunk.rs b/crates/turbopack-build/src/ecmascript/node/chunk.rs index d666a1cc2d28c..fe6ec5a7d3d49 100644 --- a/crates/turbopack-build/src/ecmascript/node/chunk.rs +++ b/crates/turbopack-build/src/ecmascript/node/chunk.rs @@ -3,7 +3,7 @@ use indexmap::IndexSet; use turbo_tasks::{ValueToString, Vc}; use turbopack_core::{ asset::{Asset, AssetContent}, - chunk::ChunkingContext, + chunk::{Chunk, ChunkingContext}, ident::AssetIdent, introspect::{Introspectable, IntrospectableChildren}, output::OutputAsset, @@ -62,16 +62,16 @@ impl EcmascriptBuildNodeChunk { } #[turbo_tasks::value_impl] -impl OutputAsset for EcmascriptBuildNodeChunk {} - -#[turbo_tasks::value_impl] -impl Asset for EcmascriptBuildNodeChunk { +impl OutputAsset for EcmascriptBuildNodeChunk { #[turbo_tasks::function] fn ident(&self) -> Vc { 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?; diff --git a/crates/turbopack-build/src/ecmascript/node/content.rs b/crates/turbopack-build/src/ecmascript/node/content.rs index c589e256627ae..73f6276069444 100644 --- a/crates/turbopack-build/src/ecmascript/node/content.rs +++ b/crates/turbopack-build/src/ecmascript/node/content.rs @@ -5,8 +5,9 @@ use indoc::writedoc; use turbo_tasks::{TryJoinIterExt, Value, Vc}; use turbo_tasks_fs::File; use turbopack_core::{ - asset::{Asset, AssetContent}, + asset::AssetContent, code_builder::{Code, CodeBuilder}, + output::OutputAsset, source_map::{GenerateSourceMap, OptionSourceMap}, }; use turbopack_ecmascript::{ diff --git a/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs b/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs index 4ba41816ceb6d..7dc325a12988e 100644 --- a/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs +++ b/crates/turbopack-build/src/ecmascript/node/entry/chunk.rs @@ -180,15 +180,15 @@ fn chunk_reference_description() -> Vc { } #[turbo_tasks::value_impl] -impl OutputAsset for EcmascriptBuildNodeEntryChunk {} - -#[turbo_tasks::value_impl] -impl Asset for EcmascriptBuildNodeEntryChunk { +impl OutputAsset for EcmascriptBuildNodeEntryChunk { #[turbo_tasks::function] 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?; diff --git a/crates/turbopack-build/src/ecmascript/node/entry/runtime.rs b/crates/turbopack-build/src/ecmascript/node/entry/runtime.rs index 26e7c3454bd8c..6f4d2ecb333e4 100644 --- a/crates/turbopack-build/src/ecmascript/node/entry/runtime.rs +++ b/crates/turbopack-build/src/ecmascript/node/entry/runtime.rs @@ -86,10 +86,7 @@ impl ValueToString for EcmascriptBuildNodeRuntimeChunk { } #[turbo_tasks::value_impl] -impl OutputAsset for EcmascriptBuildNodeRuntimeChunk {} - -#[turbo_tasks::value_impl] -impl Asset for EcmascriptBuildNodeRuntimeChunk { +impl OutputAsset for EcmascriptBuildNodeRuntimeChunk { #[turbo_tasks::function] fn ident(&self) -> Vc { let ident = AssetIdent::from_path( @@ -100,7 +97,10 @@ impl Asset 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?; diff --git a/crates/turbopack-cli-utils/src/runtime_entry.rs b/crates/turbopack-cli-utils/src/runtime_entry.rs index f77bda9c1394d..e68ce76640631 100644 --- a/crates/turbopack-cli-utils/src/runtime_entry.rs +++ b/crates/turbopack-cli-utils/src/runtime_entry.rs @@ -2,10 +2,10 @@ use anyhow::{bail, Result}; use turbo_tasks::{ValueToString, Vc}; use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ - asset::Asset, chunk::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets}, context::AssetContext, issue::{IssueSeverity, OptionIssueSource}, + module::{convert_asset_to_module, Module}, resolve::{origin::PlainResolveOrigin, parse::Request}, source::Source, }; @@ -43,12 +43,13 @@ impl RuntimeEntry { .await?; let mut runtime_entries = Vec::with_capacity(assets.len()); - for asset in &assets { + for &asset in &assets { if let Some(entry) = - Vc::try_resolve_sidecast::>(*asset).await? + Vc::try_resolve_sidecast::>(asset).await? { runtime_entries.push(entry); } else { + let asset = convert_asset_to_module(asset); bail!( "runtime reference resolved to an asset ({}) that cannot be evaluated", asset.ident().to_string().await? diff --git a/crates/turbopack-core/src/asset.rs b/crates/turbopack-core/src/asset.rs index b75785c906f62..f548ab8ed640a 100644 --- a/crates/turbopack-core/src/asset.rs +++ b/crates/turbopack-core/src/asset.rs @@ -6,7 +6,6 @@ use turbo_tasks_fs::{ }; use crate::{ - ident::AssetIdent, reference::AssetReferences, version::{VersionedAssetContent, VersionedContent}, }; @@ -32,10 +31,6 @@ impl Assets { /// An asset. It also forms a graph when following [Asset::references]. #[turbo_tasks::value_trait] pub trait Asset { - /// The identifier of the [Asset]. It's expected to be unique and capture - /// all properties of the [Asset]. - fn ident(self: Vc) -> Vc; - /// The content of the [Asset]. fn content(self: Vc) -> Vc; diff --git a/crates/turbopack-core/src/chunk/availability_info.rs b/crates/turbopack-core/src/chunk/availability_info.rs index e073f2072874b..93dd59dbac407 100644 --- a/crates/turbopack-core/src/chunk/availability_info.rs +++ b/crates/turbopack-core/src/chunk/availability_info.rs @@ -1,23 +1,23 @@ use turbo_tasks::Vc; use super::available_assets::AvailableAssets; -use crate::asset::Asset; +use crate::module::Module; #[turbo_tasks::value(serialization = "auto_for_input")] #[derive(PartialOrd, Ord, Hash, Clone, Copy, Debug)] pub enum AvailabilityInfo { Untracked, Root { - current_availability_root: Vc>, + current_availability_root: Vc>, }, Inner { available_assets: Vc, - current_availability_root: Vc>, + current_availability_root: Vc>, }, } impl AvailabilityInfo { - pub fn current_availability_root(&self) -> Option>> { + pub fn current_availability_root(&self) -> Option>> { match self { Self::Untracked => None, Self::Root { diff --git a/crates/turbopack-core/src/chunk/available_assets.rs b/crates/turbopack-core/src/chunk/available_assets.rs index 9a593ef1666d8..df1352e6c1b3c 100644 --- a/crates/turbopack-core/src/chunk/available_assets.rs +++ b/crates/turbopack-core/src/chunk/available_assets.rs @@ -10,7 +10,8 @@ use turbo_tasks_hash::Xxh3Hash64Hasher; use super::{ChunkableModuleReference, ChunkingType}; use crate::{ - asset::{Asset, AssetsSet}, + asset::Asset, + module::{Module, ModulesSet}, reference::AssetReference, }; @@ -20,7 +21,7 @@ use crate::{ #[turbo_tasks::value] pub struct AvailableAssets { parent: Option>, - roots: Vec>>, + roots: Vec>>, } #[turbo_tasks::value_impl] @@ -28,18 +29,18 @@ impl AvailableAssets { #[turbo_tasks::function] fn new_normalized( parent: Option>, - roots: Vec>>, + roots: Vec>>, ) -> Vc { AvailableAssets { parent, roots }.cell() } #[turbo_tasks::function] - pub fn new(roots: Vec>>) -> Vc { + pub fn new(roots: Vec>>) -> Vc { Self::new_normalized(None, roots) } #[turbo_tasks::function] - pub async fn with_roots(self: Vc, roots: Vec>>) -> Result> { + pub async fn with_roots(self: Vc, roots: Vec>>) -> Result> { let roots = roots .into_iter() .map(|root| async move { Ok((self.includes(root).await?, root)) }) @@ -67,7 +68,7 @@ impl AvailableAssets { } #[turbo_tasks::function] - pub async fn includes(self: Vc, asset: Vc>) -> Result> { + pub async fn includes(self: Vc, asset: Vc>) -> Result> { let this = self.await?; if let Some(parent) = this.parent { if *parent.includes(asset).await? { @@ -84,10 +85,10 @@ impl AvailableAssets { } #[turbo_tasks::function] -async fn chunkable_assets_set(root: Vc>) -> Result> { +async fn chunkable_assets_set(root: Vc>) -> Result> { let assets = AdjacencyMap::new() .skip_duplicates() - .visit(once(root), |&asset: &Vc>| async move { + .visit(once(root), |&asset: &Vc>| async move { Ok(asset .references() .await? @@ -106,15 +107,19 @@ async fn chunkable_assets_set(root: Vc>) -> Result> | ChunkingType::Placed ) ) { - return chunkable + return Ok(chunkable .resolve_reference() .primary_assets() .await? .iter() - .copied() - .map(|asset| asset.resolve()) + .map(|&asset| async move { + Ok(Vc::try_resolve_downcast::>(asset).await?) + }) .try_join() - .await; + .await? + .into_iter() + .flatten() + .collect()); } } Ok(Vec::new()) diff --git a/crates/turbopack-core/src/chunk/chunking_context.rs b/crates/turbopack-core/src/chunk/chunking_context.rs index 80f66c6ec6211..a67e0ad6603ca 100644 --- a/crates/turbopack-core/src/chunk/chunking_context.rs +++ b/crates/turbopack-core/src/chunk/chunking_context.rs @@ -2,7 +2,12 @@ use turbo_tasks::Vc; use turbo_tasks_fs::FileSystemPath; use super::{Chunk, EvaluatableAssets}; -use crate::{asset::Asset, environment::Environment, ident::AssetIdent, output::OutputAssets}; +use crate::{ + environment::Environment, + ident::AssetIdent, + module::Module, + output::{OutputAsset, OutputAssets}, +}; /// A context for the chunking that influences the way chunks are created #[turbo_tasks::value_trait] @@ -22,12 +27,12 @@ pub trait ChunkingContext { // TODO(alexkirsz) Remove this from the chunking context. /// Reference Source Map Assets for chunks - fn reference_chunk_source_maps(self: Vc, chunk: Vc>) -> Vc; + fn reference_chunk_source_maps(self: Vc, chunk: Vc>) -> Vc; fn can_be_in_same_chunk( self: Vc, - asset_a: Vc>, - asset_b: Vc>, + asset_a: Vc>, + asset_b: Vc>, ) -> Vc; fn asset_path( diff --git a/crates/turbopack-core/src/chunk/data.rs b/crates/turbopack-core/src/chunk/data.rs index ed19a75bcc6ed..03becef4c3333 100644 --- a/crates/turbopack-core/src/chunk/data.rs +++ b/crates/turbopack-core/src/chunk/data.rs @@ -3,7 +3,6 @@ use turbo_tasks::{ReadRef, TryJoinIterExt, Vc}; use turbo_tasks_fs::FileSystemPath; use crate::{ - asset::Asset, chunk::{ModuleId, OutputChunk, OutputChunkRuntimeInfo}, output::{OutputAsset, OutputAssets}, reference::{AssetReferences, SingleAssetReference}, @@ -97,7 +96,7 @@ impl ChunkData { ( path.to_owned(), Vc::upcast(SingleAssetReference::new( - chunk, + Vc::upcast(chunk), module_chunk_reference_description(), )), ) diff --git a/crates/turbopack-core/src/chunk/mod.rs b/crates/turbopack-core/src/chunk/mod.rs index b34dd81322795..a30042f082573 100644 --- a/crates/turbopack-core/src/chunk/mod.rs +++ b/crates/turbopack-core/src/chunk/mod.rs @@ -35,9 +35,9 @@ pub use self::{ passthrough_asset::PassthroughAsset, }; use crate::{ - asset::{Asset, Assets}, + asset::Asset, ident::AssetIdent, - module::Module, + module::{Module, Modules}, output::OutputAssets, reference::{AssetReference, AssetReferences}, resolve::{PrimaryResolveResult, ResolveResult}, @@ -117,6 +117,7 @@ impl Chunks { /// It usually contains multiple chunk items. #[turbo_tasks::value_trait] pub trait Chunk: Asset { + fn ident(self: Vc) -> Vc; fn chunking_context(self: Vc) -> Vc>; // TODO Once output assets have their own trait, this path() method will move // into that trait and ident() will be removed from that. Assets on the @@ -142,7 +143,7 @@ pub struct OutputChunkRuntimeInfo { /// List of paths of chunks containing individual modules that are part of /// this chunk. This is useful for selectively loading modules from a chunk /// without loading the whole chunk. - pub module_chunks: Option>, + pub module_chunks: Option>, pub placeholder_for_future_extensions: (), } @@ -253,7 +254,7 @@ pub struct ChunkContentResult { pub trait FromChunkableModule: ChunkItem { async fn from_asset( context: Vc>, - asset: Vc>, + asset: Vc>, ) -> Result>>; async fn from_async_asset( context: Vc>, @@ -264,8 +265,8 @@ pub trait FromChunkableModule: ChunkItem { pub async fn chunk_content_split( context: Vc>, - entry: Vc>, - additional_entries: Option>, + entry: Vc>, + additional_entries: Option>, availability_info: Value, ) -> Result>> where @@ -278,8 +279,8 @@ where pub async fn chunk_content( context: Vc>, - entry: Vc>, - additional_entries: Option>, + entry: Vc>, + additional_entries: Option>, availability_info: Value, ) -> Result>>> where @@ -293,11 +294,11 @@ where enum ChunkContentGraphNode { // An asset not placed in the current chunk, but whose references we will // follow to find more graph nodes. - PassthroughAsset { asset: Vc> }, + PassthroughAsset { asset: Vc> }, // Chunk items that are placed into the current chunk ChunkItem { item: I, ident: ReadRef }, // Asset that is already available and doesn't need to be included - AvailableAsset(Vc>), + AvailableAsset(Vc>), // Chunks that are loaded in parallel to the current chunk Chunk(Vc>), ExternalAssetReference(Vc>), @@ -306,7 +307,7 @@ enum ChunkContentGraphNode { #[derive(Clone, Copy)] struct ChunkContentContext { chunking_context: Vc>, - entry: Vc>, + entry: Vc>, availability_info: Value, split: bool, } @@ -316,7 +317,7 @@ async fn reference_to_graph_nodes( reference: Vc>, ) -> Result< Vec<( - Option<(Vc>, ChunkingType)>, + Option<(Vc>, ChunkingType)>, ChunkContentGraphNode>, )>, > @@ -353,6 +354,10 @@ where let mut graph_nodes = vec![]; for asset in assets { + let asset = asset.resolve().await?; + let Some(asset) = Vc::try_resolve_downcast::>(asset).await? else { + continue; + }; if let Some(available_assets) = context.availability_info.available_assets() { if *available_assets.includes(asset).await? { graph_nodes.push(( @@ -479,13 +484,13 @@ const MAX_CHUNK_ITEMS_COUNT: usize = 5000; struct ChunkContentVisit { context: ChunkContentContext, chunk_items_count: usize, - processed_assets: HashSet<(ChunkingType, Vc>)>, + processed_assets: HashSet<(ChunkingType, Vc>)>, _phantom: PhantomData, } type ChunkItemToGraphNodesEdges = impl Iterator< Item = ( - Option<(Vc>, ChunkingType)>, + Option<(Vc>, ChunkingType)>, ChunkContentGraphNode>, ), >; @@ -498,7 +503,7 @@ where I: FromChunkableModule, { type Edge = ( - Option<(Vc>, ChunkingType)>, + Option<(Vc>, ChunkingType)>, ChunkContentGraphNode>, ); type EdgesIntoIter = ChunkItemToGraphNodesEdges; @@ -507,7 +512,7 @@ where fn visit( &mut self, (option_key, node): ( - Option<(Vc>, ChunkingType)>, + Option<(Vc>, ChunkingType)>, ChunkContentGraphNode>, ), ) -> VisitControlFlow>, ()> { @@ -570,8 +575,8 @@ where async fn chunk_content_internal_parallel( chunking_context: Vc>, - entry: Vc>, - additional_entries: Option>, + entry: Vc>, + additional_entries: Option>, availability_info: Value, split: bool, ) -> Result>>> diff --git a/crates/turbopack-core/src/chunk/passthrough_asset.rs b/crates/turbopack-core/src/chunk/passthrough_asset.rs index 996aee09a70d8..ee42f7a2555ee 100644 --- a/crates/turbopack-core/src/chunk/passthrough_asset.rs +++ b/crates/turbopack-core/src/chunk/passthrough_asset.rs @@ -1,6 +1,6 @@ -use crate::asset::Asset; +use crate::{asset::Asset, module::Module}; /// An [Asset] that should never be placed into a chunk, but whose references /// should still be followed. #[turbo_tasks::value_trait] -pub trait PassthroughAsset: Asset {} +pub trait PassthroughAsset: Module + Asset {} diff --git a/crates/turbopack-core/src/file_source.rs b/crates/turbopack-core/src/file_source.rs index a1b31ece580c4..6dc899d9366ce 100644 --- a/crates/turbopack-core/src/file_source.rs +++ b/crates/turbopack-core/src/file_source.rs @@ -25,15 +25,15 @@ impl FileSource { } #[turbo_tasks::value_impl] -impl Source for FileSource {} - -#[turbo_tasks::value_impl] -impl Asset for FileSource { +impl Source for FileSource { #[turbo_tasks::function] fn ident(&self) -> Vc { AssetIdent::from_path(self.path) } +} +#[turbo_tasks::value_impl] +impl Asset for FileSource { #[turbo_tasks::function] async fn content(&self) -> Result> { let file_type = &*self.path.get_type().await?; diff --git a/crates/turbopack-core/src/introspect/asset.rs b/crates/turbopack-core/src/introspect/asset.rs index e0ed75f1a72d3..9a0d4f3664370 100644 --- a/crates/turbopack-core/src/introspect/asset.rs +++ b/crates/turbopack-core/src/introspect/asset.rs @@ -7,8 +7,11 @@ use super::{Introspectable, IntrospectableChildren}; use crate::{ asset::{Asset, AssetContent}, chunk::{ChunkableModuleReference, ChunkingType}, + module::Module, + output::OutputAsset, reference::{AssetReference, AssetReferences}, resolve::PrimaryResolveResult, + source::Source, }; #[turbo_tasks::value] @@ -67,8 +70,21 @@ impl Introspectable for IntrospectableAsset { } #[turbo_tasks::function] - fn title(&self) -> Vc { - self.0.ident().to_string() + async fn title(&self) -> Result> { + let asset = self.0.resolve().await?; + Ok( + if let Some(source) = Vc::try_resolve_downcast::>(asset).await? { + source.ident().to_string() + } else if let Some(module) = Vc::try_resolve_downcast::>(asset).await? { + module.ident().to_string() + } else if let Some(output_asset) = + Vc::try_resolve_downcast::>(asset).await? + { + output_asset.ident().to_string() + } else { + Vc::cell("unknown type".to_string()) + }, + ) } #[turbo_tasks::function] diff --git a/crates/turbopack-core/src/issue/mod.rs b/crates/turbopack-core/src/issue/mod.rs index 5a4b4dbd94b58..e2d46c7845951 100644 --- a/crates/turbopack-core/src/issue/mod.rs +++ b/crates/turbopack-core/src/issue/mod.rs @@ -21,6 +21,7 @@ use turbo_tasks_hash::{DeterministicHash, Xxh3Hash64Hasher}; use crate::{ asset::{Asset, AssetContent}, + source::Source, source_pos::SourcePos, }; @@ -398,7 +399,7 @@ impl CapturedIssues { #[turbo_tasks::value] #[derive(Clone)] pub struct IssueSource { - pub source: Vc>, + pub source: Vc>, pub start: SourcePos, pub end: SourcePos, } @@ -407,7 +408,7 @@ pub struct IssueSource { impl IssueSource { #[turbo_tasks::function] pub async fn from_byte_offset( - source: Vc>, + source: Vc>, start: usize, end: usize, ) -> Result> { @@ -545,7 +546,7 @@ impl PlainIssue { #[turbo_tasks::value(serialization = "none")] #[derive(Clone, Debug)] pub struct PlainIssueSource { - pub asset: ReadRef, + pub asset: ReadRef, pub start: SourcePos, pub end: SourcePos, } @@ -556,7 +557,7 @@ impl IssueSource { pub async fn into_plain(self: Vc) -> Result> { let this = self.await?; Ok(PlainIssueSource { - asset: PlainAsset::from_asset(this.source).await?, + asset: PlainSource::from_source(this.source).await?, start: this.start, end: this.end, } @@ -566,23 +567,23 @@ impl IssueSource { #[turbo_tasks::value(serialization = "none")] #[derive(Clone, Debug)] -pub struct PlainAsset { +pub struct PlainSource { pub ident: ReadRef, #[turbo_tasks(debug_ignore)] pub content: ReadRef, } #[turbo_tasks::value_impl] -impl PlainAsset { +impl PlainSource { #[turbo_tasks::function] - pub async fn from_asset(asset: Vc>) -> Result> { + pub async fn from_source(asset: Vc>) -> Result> { let asset_content = asset.content().await?; let content = match *asset_content { AssetContent::File(file_content) => file_content.await?, AssetContent::Redirect { .. } => ReadRef::new(Arc::new(FileContent::NotFound)), }; - Ok(PlainAsset { + Ok(PlainSource { ident: asset.ident().to_string().await?, content, } diff --git a/crates/turbopack-core/src/lib.rs b/crates/turbopack-core/src/lib.rs index 29ab17f37b7e8..6c74a5de2d808 100644 --- a/crates/turbopack-core/src/lib.rs +++ b/crates/turbopack-core/src/lib.rs @@ -36,6 +36,7 @@ pub mod source_transform; pub mod target; mod utils; pub mod version; +pub mod virtual_output; pub mod virtual_source; pub mod virtual_fs { diff --git a/crates/turbopack-core/src/module.rs b/crates/turbopack-core/src/module.rs index 0d7c9b0539d77..cdfb067f72004 100644 --- a/crates/turbopack-core/src/module.rs +++ b/crates/turbopack-core/src/module.rs @@ -1,11 +1,50 @@ +use anyhow::{bail, Result}; +use indexmap::IndexSet; use turbo_tasks::Vc; -use crate::asset::Asset; +use crate::{asset::Asset, ident::AssetIdent, raw_module::RawModule, source::Source}; /// A module. This usually represents parsed source code, which has references /// to other modules. #[turbo_tasks::value_trait] -pub trait Module: Asset {} +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; +} #[turbo_tasks::value(transparent)] pub struct OptionModule(Option>>); + +#[turbo_tasks::value(transparent)] +pub struct Modules(Vec>>); + +#[turbo_tasks::value_impl] +impl Modules { + #[turbo_tasks::function] + pub fn empty() -> Vc { + Vc::cell(Vec::new()) + } +} + +/// A set of [Module]s +#[turbo_tasks::value(transparent)] +pub struct ModulesSet(IndexSet>>); + +/// This is a temporary function that should be removed once the [Module] +/// trait completely replaces the [Asset] trait. +/// It converts an [Asset] into a [Module], but either casting it or wrapping it +/// in a [RawModule]. +// TODO make this function unnecessary, it should never be a Source +#[turbo_tasks::function] +pub async fn convert_asset_to_module(asset: Vc>) -> Result>> { + if let Some(module) = Vc::try_resolve_downcast::>(asset).await? { + Ok(module) + } else if let Some(source) = Vc::try_resolve_downcast::>(asset).await? { + Ok(Vc::upcast(RawModule::new(source))) + } else { + bail!("Asset must be a Module or a Source") + } +} + +// TODO All Vc::try_resolve_downcast::> calls should be removed diff --git a/crates/turbopack-core/src/output.rs b/crates/turbopack-core/src/output.rs index 07de6042ec1c0..d326a69f1cbd9 100644 --- a/crates/turbopack-core/src/output.rs +++ b/crates/turbopack-core/src/output.rs @@ -1,12 +1,18 @@ use anyhow::{Context, Result}; +use indexmap::IndexSet; use turbo_tasks::Vc; -use crate::asset::Asset; +use crate::{asset::Asset, ident::AssetIdent}; /// An asset that should be outputted, e. g. written to disk or served from a /// server. #[turbo_tasks::value_trait] -pub trait OutputAsset: Asset {} +pub trait OutputAsset: Asset { + // TODO change this to path() -> Vc + /// 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; +} #[turbo_tasks::value(transparent)] pub struct OutputAssets(Vec>>); @@ -19,12 +25,19 @@ impl OutputAssets { } } +/// A set of [OutputAsset]s +#[turbo_tasks::value(transparent)] +pub struct OutputAssetsSet(IndexSet>>); + /// This is a temporary function that should be removed once the [OutputAsset] /// trait completely replaces the [Asset] trait. /// TODO make this function unnecessary #[turbo_tasks::function] pub async fn asset_to_output_asset(asset: Vc>) -> Result>> { - Vc::try_resolve_sidecast::>(asset) + Vc::try_resolve_downcast::>(asset) .await? .context("Asset must be a OutputAsset") } + +// TODO All Vc::try_resolve_downcast::> calls should be +// removed diff --git a/crates/turbopack-core/src/proxied_asset.rs b/crates/turbopack-core/src/proxied_asset.rs index 43885565b0322..691a9af2bf22d 100644 --- a/crates/turbopack-core/src/proxied_asset.rs +++ b/crates/turbopack-core/src/proxied_asset.rs @@ -29,15 +29,15 @@ impl ProxiedAsset { } #[turbo_tasks::value_impl] -impl OutputAsset for ProxiedAsset {} - -#[turbo_tasks::value_impl] -impl Asset for ProxiedAsset { +impl OutputAsset for ProxiedAsset { #[turbo_tasks::function] fn ident(&self) -> Vc { AssetIdent::from_path(self.path) } +} +#[turbo_tasks::value_impl] +impl Asset for ProxiedAsset { #[turbo_tasks::function] fn content(&self) -> Vc { self.asset.content() diff --git a/crates/turbopack-core/src/raw_module.rs b/crates/turbopack-core/src/raw_module.rs index 3a9a51460c612..272f5f7bbf30f 100644 --- a/crates/turbopack-core/src/raw_module.rs +++ b/crates/turbopack-core/src/raw_module.rs @@ -15,15 +15,15 @@ pub struct RawModule { } #[turbo_tasks::value_impl] -impl Module for RawModule {} - -#[turbo_tasks::value_impl] -impl Asset for RawModule { +impl Module for RawModule { #[turbo_tasks::function] fn ident(&self) -> Vc { self.source.ident() } +} +#[turbo_tasks::value_impl] +impl Asset for RawModule { #[turbo_tasks::function] fn content(&self) -> Vc { self.source.content() diff --git a/crates/turbopack-core/src/raw_output.rs b/crates/turbopack-core/src/raw_output.rs index e432ffc904639..870da2bcaa0bf 100644 --- a/crates/turbopack-core/src/raw_output.rs +++ b/crates/turbopack-core/src/raw_output.rs @@ -15,15 +15,15 @@ pub struct RawOutput { } #[turbo_tasks::value_impl] -impl OutputAsset for RawOutput {} - -#[turbo_tasks::value_impl] -impl Asset for RawOutput { +impl OutputAsset for RawOutput { #[turbo_tasks::function] fn ident(&self) -> Vc { self.source.ident() } +} +#[turbo_tasks::value_impl] +impl Asset for RawOutput { #[turbo_tasks::function] fn content(&self) -> Vc { self.source.content() diff --git a/crates/turbopack-core/src/reference/mod.rs b/crates/turbopack-core/src/reference/mod.rs index 3ca57c44a3078..fa612a40f26cc 100644 --- a/crates/turbopack-core/src/reference/mod.rs +++ b/crates/turbopack-core/src/reference/mod.rs @@ -6,6 +6,8 @@ use turbo_tasks::{TryJoinIterExt, ValueToString, Vc}; use crate::{ asset::{Asset, Assets}, issue::IssueContextExt, + module::{convert_asset_to_module, Module, Modules}, + output::{OutputAsset, OutputAssets}, resolve::{PrimaryResolveResult, ResolveResult}, }; pub mod source_map; @@ -117,6 +119,165 @@ pub async fn all_referenced_assets(asset: Vc>) -> Result>, +) -> 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()); + } + } + let output_assets = + assets + .into_iter() + .map(|asset| async move { + Ok(Vc::try_resolve_downcast::>(asset).await?) + }) + .try_join() + .await? + .into_iter() + .flatten() + .collect(); + Ok(Vc::cell(output_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. +/// +/// [Asset]: crate::asset::Asset +#[turbo_tasks::function] +pub async fn all_referenced_modules(module: Vc>) -> Result> { + let references_set = module.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()); + } + } + let modules = assets.into_iter().map(convert_asset_to_module).collect(); + Ok(Vc::cell(modules)) +} + +/// Aggregates all primary [Module]s referenced by an [Module]. [AssetReference] +/// This does not include transitively references [Module]s, only includes +/// primary [Module]s referenced. +/// +/// [Module]: crate::module::Module +#[turbo_tasks::function] +pub async fn primary_referenced_modules(module: Vc>) -> Result> { + let modules = module + .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() + .map(|asset| async move { Ok(Vc::try_resolve_downcast::>(asset).await?) }) + .try_join() + .await? + .into_iter() + .flatten() + .collect(); + Ok(Vc::cell(modules)) +} + +/// Aggregates all primary [OutputAsset]s referenced by an [OutputAsset]. +/// [AssetReference] This does not include transitively references +/// [OutputAsset]s, only includes primary [OutputAsset]s referenced. +/// +/// [OutputAsset]: crate::output::OutputAsset +#[turbo_tasks::function] +pub async fn primary_referenced_output_assets( + output_asset: Vc>, +) -> Result> { + let output_assets = + output_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() + .map(|asset| async move { + Ok(Vc::try_resolve_downcast::>(asset).await?) + }) + .try_join() + .await? + .into_iter() + .flatten() + .collect(); + 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. @@ -149,14 +310,14 @@ pub async fn primary_referenced_assets(asset: Vc>) -> Result>) -> Result> { +pub async fn all_modules(asset: Vc>) -> Result> { // TODO need to track import path here let mut queue = VecDeque::with_capacity(32); - queue.push_back((asset, all_referenced_assets(asset))); + queue.push_back((asset, all_referenced_modules(asset))); let mut assets = HashSet::new(); assets.insert(asset); while let Some((parent, references)) = queue.pop_front() { @@ -165,7 +326,7 @@ pub async fn all_assets(asset: Vc>) -> Result> { .await?; for asset in references.await?.iter() { if assets.insert(*asset) { - queue.push_back((*asset, all_referenced_assets(*asset))); + queue.push_back((*asset, all_referenced_modules(*asset))); } } } diff --git a/crates/turbopack-core/src/reference_type.rs b/crates/turbopack-core/src/reference_type.rs index db5875adff6c4..390921ba63191 100644 --- a/crates/turbopack-core/src/reference_type.rs +++ b/crates/turbopack-core/src/reference_type.rs @@ -3,13 +3,13 @@ use std::fmt::Display; use indexmap::IndexMap; use turbo_tasks::Vc; -use crate::{asset::Asset, resolve::ModulePart}; +use crate::{module::Module, resolve::ModulePart}; /// Named references to inner assets. Modules can used them to allow to /// per-module aliases of some requests to already created module assets. /// Name is usually in UPPER_CASE to make it clear that this is an inner asset. #[turbo_tasks::value(transparent)] -pub struct InnerAssets(IndexMap>>); +pub struct InnerAssets(IndexMap>>); #[turbo_tasks::value_impl] impl InnerAssets { diff --git a/crates/turbopack-core/src/resolve/mod.rs b/crates/turbopack-core/src/resolve/mod.rs index 4da8dc4932378..3472ba6db4ca2 100644 --- a/crates/turbopack-core/src/resolve/mod.rs +++ b/crates/turbopack-core/src/resolve/mod.rs @@ -33,6 +33,7 @@ use crate::{ pattern::{read_matches, PatternMatch}, plugin::ResolvePlugin, }, + source::{asset_to_source, Source}, }; mod alias_map; @@ -685,7 +686,8 @@ async fn handle_resolve_plugins( let mut new_references = Vec::new(); for primary in result_value.primary.iter() { - if let PrimaryResolveResult::Asset(asset) = primary { + if let &PrimaryResolveResult::Asset(asset) = primary { + let asset = asset_to_source(asset); if let Some(new_result) = apply_plugins_to_path( asset.ident().path().resolve().await?, context, diff --git a/crates/turbopack-core/src/resolve/origin.rs b/crates/turbopack-core/src/resolve/origin.rs index 201c0be70972b..fbed76ff35811 100644 --- a/crates/turbopack-core/src/resolve/origin.rs +++ b/crates/turbopack-core/src/resolve/origin.rs @@ -3,7 +3,7 @@ use turbo_tasks::{Upcast, Value, Vc}; use turbo_tasks_fs::FileSystemPath; use super::{options::ResolveOptions, parse::Request, ResolveResult}; -use crate::{asset::AssetOption, context::AssetContext, reference_type::ReferenceType}; +use crate::{context::AssetContext, module::OptionModule, reference_type::ReferenceType}; /// A location where resolving can occur from. It carries some meta information /// that are needed for resolving from here. @@ -21,7 +21,8 @@ pub trait ResolveOrigin { /// Get an inner asset form this origin that doesn't require resolving but /// is directly attached - fn get_inner_asset(self: Vc, _request: Vc) -> Vc { + fn get_inner_asset(self: Vc, request: Vc) -> Vc { + let _ = request; Vc::cell(None) } } @@ -83,7 +84,7 @@ async fn resolve_asset( reference_type: Value, ) -> Result> { if let Some(asset) = *resolve_origin.get_inner_asset(request).await? { - return Ok(ResolveResult::asset(asset).cell()); + return Ok(ResolveResult::asset(Vc::upcast(asset)).cell()); } Ok(resolve_origin.context().resolve_asset( resolve_origin.origin_path(), @@ -147,7 +148,7 @@ impl ResolveOrigin for ResolveOriginWithTransition { } #[turbo_tasks::function] - fn get_inner_asset(&self, request: Vc) -> Vc { + fn get_inner_asset(&self, request: Vc) -> Vc { self.previous.get_inner_asset(request) } } diff --git a/crates/turbopack-core/src/source.rs b/crates/turbopack-core/src/source.rs index 4b8ef63fb7df1..6b6a6f677605e 100644 --- a/crates/turbopack-core/src/source.rs +++ b/crates/turbopack-core/src/source.rs @@ -1,12 +1,19 @@ use anyhow::{Context, Result}; use turbo_tasks::Vc; -use crate::asset::{Asset, AssetOption}; +use crate::{ + asset::{Asset, AssetOption}, + ident::AssetIdent, +}; /// (Unparsed) Source Code. Source Code is processed into [Module]s by the /// [AssetContext]. All [Source]s have content and an identifier. #[turbo_tasks::value_trait] -pub trait Source: Asset {} +pub trait Source: Asset { + /// The identifier of the [Source]. It's expected to be unique and capture + /// all properties of the [Source]. + fn ident(&self) -> Vc; +} #[turbo_tasks::value(transparent)] pub struct OptionSource(Option>>); @@ -39,3 +46,5 @@ pub async fn option_asset_to_source(asset: Vc) -> Result> calls should be removed diff --git a/crates/turbopack-core/src/source_map/source_map_asset.rs b/crates/turbopack-core/src/source_map/source_map_asset.rs index 09a7d71d10a69..ffbafd905545e 100644 --- a/crates/turbopack-core/src/source_map/source_map_asset.rs +++ b/crates/turbopack-core/src/source_map/source_map_asset.rs @@ -16,22 +16,19 @@ use crate::{ /// Represents the source map of an ecmascript asset. #[turbo_tasks::value] pub struct SourceMapAsset { - asset: Vc>, + asset: Vc>, } #[turbo_tasks::value_impl] impl SourceMapAsset { #[turbo_tasks::function] - pub fn new(asset: Vc>) -> Vc { + pub fn new(asset: Vc>) -> Vc { SourceMapAsset { asset }.cell() } } #[turbo_tasks::value_impl] -impl OutputAsset for SourceMapAsset {} - -#[turbo_tasks::value_impl] -impl Asset for SourceMapAsset { +impl OutputAsset for SourceMapAsset { #[turbo_tasks::function] async fn ident(&self) -> Result> { // NOTE(alexkirsz) We used to include the asset's version id in the path, @@ -40,7 +37,10 @@ impl Asset for SourceMapAsset { self.asset.ident().path().append(".map".to_string()), )) } +} +#[turbo_tasks::value_impl] +impl Asset for SourceMapAsset { #[turbo_tasks::function] async fn content(&self) -> Result> { let Some(generate_source_map) = @@ -100,13 +100,13 @@ impl Introspectable for SourceMapAsset { /// server/build system of the presence of the source map #[turbo_tasks::value] pub struct SourceMapAssetReference { - asset: Vc>, + asset: Vc>, } #[turbo_tasks::value_impl] impl SourceMapAssetReference { #[turbo_tasks::function] - pub fn new(asset: Vc>) -> Vc { + pub fn new(asset: Vc>) -> Vc { SourceMapAssetReference { asset }.cell() } } diff --git a/crates/turbopack-core/src/virtual_output.rs b/crates/turbopack-core/src/virtual_output.rs new file mode 100644 index 0000000000000..f3418eb016cc4 --- /dev/null +++ b/crates/turbopack-core/src/virtual_output.rs @@ -0,0 +1,39 @@ +use turbo_tasks::Vc; +use turbo_tasks_fs::FileSystemPath; + +use crate::{ + asset::{Asset, AssetContent}, + ident::AssetIdent, + output::OutputAsset, +}; + +/// An [Asset] that is created from some passed source code. +#[turbo_tasks::value] +pub struct VirtualOutputAsset { + pub path: Vc, + pub content: Vc, +} + +#[turbo_tasks::value_impl] +impl VirtualOutputAsset { + #[turbo_tasks::function] + pub fn new(path: Vc, content: Vc) -> Vc { + VirtualOutputAsset { path, content }.cell() + } +} + +#[turbo_tasks::value_impl] +impl OutputAsset for VirtualOutputAsset { + #[turbo_tasks::function] + fn ident(&self) -> Vc { + AssetIdent::from_path(self.path) + } +} + +#[turbo_tasks::value_impl] +impl Asset for VirtualOutputAsset { + #[turbo_tasks::function] + fn content(&self) -> Vc { + self.content + } +} diff --git a/crates/turbopack-core/src/virtual_source.rs b/crates/turbopack-core/src/virtual_source.rs index cebf5486642ea..8b0378dc85220 100644 --- a/crates/turbopack-core/src/virtual_source.rs +++ b/crates/turbopack-core/src/virtual_source.rs @@ -31,15 +31,15 @@ impl VirtualSource { } #[turbo_tasks::value_impl] -impl Source for VirtualSource {} - -#[turbo_tasks::value_impl] -impl Asset for VirtualSource { +impl Source for VirtualSource { #[turbo_tasks::function] fn ident(&self) -> Vc { self.ident } +} +#[turbo_tasks::value_impl] +impl Asset for VirtualSource { #[turbo_tasks::function] fn content(&self) -> Vc { self.content diff --git a/crates/turbopack-css/src/asset.rs b/crates/turbopack-css/src/asset.rs index 7c8899471c90e..c0ed045eeb641 100644 --- a/crates/turbopack-css/src/asset.rs +++ b/crates/turbopack-css/src/asset.rs @@ -82,12 +82,15 @@ impl ParseCss for CssModuleAsset { } #[turbo_tasks::value_impl] -impl Asset for CssModuleAsset { +impl Module for CssModuleAsset { #[turbo_tasks::function] 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() @@ -106,9 +109,6 @@ impl Asset for CssModuleAsset { } } -#[turbo_tasks::value_impl] -impl Module for CssModuleAsset {} - #[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 b7caf1008c197..1506fedec56c5 100644 --- a/crates/turbopack-css/src/chunk/mod.rs +++ b/crates/turbopack-css/src/chunk/mod.rs @@ -270,6 +270,12 @@ async fn css_chunk_content_single_entry( #[turbo_tasks::value_impl] impl Chunk for CssChunk { + #[turbo_tasks::function] + fn ident(self: Vc) -> Vc { + let self_as_output_asset: Vc> = Vc::upcast(self); + self_as_output_asset.ident() + } + #[turbo_tasks::function] fn chunking_context(&self) -> Vc> { self.context @@ -349,10 +355,7 @@ impl OutputChunk for CssChunk { } #[turbo_tasks::value_impl] -impl OutputAsset for CssChunk {} - -#[turbo_tasks::value_impl] -impl Asset for CssChunk { +impl OutputAsset for CssChunk { #[turbo_tasks::function] async fn ident(self: Vc) -> Result> { let this = self.await?; @@ -382,7 +385,10 @@ impl Asset 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() @@ -501,10 +507,10 @@ pub trait CssChunkItem: ChunkItem { impl FromChunkableModule for Box { async fn from_asset( context: Vc>, - asset: Vc>, + asset: Vc>, ) -> Result>> { if let Some(placeable) = - Vc::try_resolve_sidecast::>(asset).await? + Vc::try_resolve_downcast::>(asset).await? { return Ok(Some(placeable.as_chunk_item(context))); } 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 e5f199fb9c1cd..0fa2c7a00ea1d 100644 --- a/crates/turbopack-css/src/chunk/single_item_chunk/chunk.rs +++ b/crates/turbopack-css/src/chunk/single_item_chunk/chunk.rs @@ -71,6 +71,12 @@ impl SingleItemCssChunk { #[turbo_tasks::value_impl] impl Chunk for SingleItemCssChunk { + #[turbo_tasks::function] + fn ident(self: Vc) -> Vc { + let self_as_output_asset: Vc> = Vc::upcast(self); + self_as_output_asset.ident() + } + #[turbo_tasks::function] fn chunking_context(&self) -> Vc> { self.context @@ -83,10 +89,7 @@ fn single_item_modifier() -> Vc { } #[turbo_tasks::value_impl] -impl OutputAsset for SingleItemCssChunk {} - -#[turbo_tasks::value_impl] -impl Asset for SingleItemCssChunk { +impl OutputAsset for SingleItemCssChunk { #[turbo_tasks::function] async fn ident(&self) -> Result> { Ok(AssetIdent::from_path( @@ -98,7 +101,10 @@ 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?; diff --git a/crates/turbopack-css/src/chunk/single_item_chunk/source_map.rs b/crates/turbopack-css/src/chunk/single_item_chunk/source_map.rs index 0f366935cb875..ab9875e352afb 100644 --- a/crates/turbopack-css/src/chunk/single_item_chunk/source_map.rs +++ b/crates/turbopack-css/src/chunk/single_item_chunk/source_map.rs @@ -28,17 +28,17 @@ impl SingleItemCssChunkSourceMapAsset { } #[turbo_tasks::value_impl] -impl OutputAsset for SingleItemCssChunkSourceMapAsset {} - -#[turbo_tasks::value_impl] -impl Asset for SingleItemCssChunkSourceMapAsset { +impl OutputAsset for SingleItemCssChunkSourceMapAsset { #[turbo_tasks::function] async fn ident(&self) -> Result> { Ok(AssetIdent::from_path( self.chunk.path().append(".map".to_string()), )) } +} +#[turbo_tasks::value_impl] +impl Asset for SingleItemCssChunkSourceMapAsset { #[turbo_tasks::function] async fn content(&self) -> Result> { let sm = if let Some(sm) = *self.chunk.generate_source_map().await? { diff --git a/crates/turbopack-css/src/chunk/source_map.rs b/crates/turbopack-css/src/chunk/source_map.rs index 9d7a3504313ab..14fabe9d3ccd1 100644 --- a/crates/turbopack-css/src/chunk/source_map.rs +++ b/crates/turbopack-css/src/chunk/source_map.rs @@ -28,17 +28,17 @@ impl CssChunkSourceMapAsset { } #[turbo_tasks::value_impl] -impl OutputAsset for CssChunkSourceMapAsset {} - -#[turbo_tasks::value_impl] -impl Asset for CssChunkSourceMapAsset { +impl OutputAsset for CssChunkSourceMapAsset { #[turbo_tasks::function] async fn ident(&self) -> Result> { Ok(AssetIdent::from_path( self.chunk.path().append(".map".to_string()), )) } +} +#[turbo_tasks::value_impl] +impl Asset for CssChunkSourceMapAsset { #[turbo_tasks::function] async fn content(&self) -> Result> { let sm = if let Some(sm) = *self.chunk.generate_source_map().await? { diff --git a/crates/turbopack-css/src/embed.rs b/crates/turbopack-css/src/embed.rs index 8527c015847b8..c797e4d4c6098 100644 --- a/crates/turbopack-css/src/embed.rs +++ b/crates/turbopack-css/src/embed.rs @@ -3,6 +3,7 @@ use turbopack_core::{ asset::Asset, chunk::{ChunkableModule, ChunkingContext}, module::Module, + output::OutputAsset, reference::AssetReferences, }; @@ -19,5 +20,5 @@ pub trait CssEmbed { /// TODO(alexkirsz) This should have a default impl that returns empty /// references. fn references(self: Vc) -> Vc; - fn embeddable_asset(self: Vc) -> Vc>; + fn embeddable_asset(self: Vc) -> Vc>; } diff --git a/crates/turbopack-css/src/global_asset.rs b/crates/turbopack-css/src/global_asset.rs index ce2384448c054..770c232be0a1e 100644 --- a/crates/turbopack-css/src/global_asset.rs +++ b/crates/turbopack-css/src/global_asset.rs @@ -46,12 +46,15 @@ impl GlobalCssAsset { } #[turbo_tasks::value_impl] -impl Asset for GlobalCssAsset { +impl Module for GlobalCssAsset { #[turbo_tasks::function] fn ident(&self) -> Vc { self.source.ident().with_modifier(modifier()) } +} +#[turbo_tasks::value_impl] +impl Asset for GlobalCssAsset { #[turbo_tasks::function] fn content(&self) -> Result> { bail!("CSS global asset has no contents") @@ -65,9 +68,6 @@ impl Asset for GlobalCssAsset { } } -#[turbo_tasks::value_impl] -impl Module for GlobalCssAsset {} - #[turbo_tasks::function] fn modifier() -> Vc { Vc::cell("global css".to_string()) diff --git a/crates/turbopack-css/src/module_asset.rs b/crates/turbopack-css/src/module_asset.rs index 9630e6b9138b7..21f7063d68955 100644 --- a/crates/turbopack-css/src/module_asset.rs +++ b/crates/turbopack-css/src/module_asset.rs @@ -61,12 +61,15 @@ impl ModuleCssAsset { } #[turbo_tasks::value_impl] -impl Asset for ModuleCssAsset { +impl Module for ModuleCssAsset { #[turbo_tasks::function] 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") @@ -202,9 +205,6 @@ impl ModuleCssAsset { } } -#[turbo_tasks::value_impl] -impl Module for ModuleCssAsset {} - #[turbo_tasks::value_impl] impl ChunkableModule for ModuleCssAsset { #[turbo_tasks::function] diff --git a/crates/turbopack-css/src/references/internal.rs b/crates/turbopack-css/src/references/internal.rs index 26aae69e33074..43fcc039099eb 100644 --- a/crates/turbopack-css/src/references/internal.rs +++ b/crates/turbopack-css/src/references/internal.rs @@ -1,7 +1,7 @@ use anyhow::Result; use turbo_tasks::{ValueToString, Vc}; use turbopack_core::{ - asset::Asset, chunk::ChunkableModuleReference, module::Module, reference::AssetReference, + chunk::ChunkableModuleReference, module::Module, reference::AssetReference, resolve::ResolveResult, }; diff --git a/crates/turbopack-css/src/references/url.rs b/crates/turbopack-css/src/references/url.rs index 56d293e3d206e..52110be05cd12 100644 --- a/crates/turbopack-css/src/references/url.rs +++ b/crates/turbopack-css/src/references/url.rs @@ -5,10 +5,10 @@ use swc_core::{ }; use turbo_tasks::{Value, ValueToString, Vc}; use turbopack_core::{ - asset::Asset, chunk::ChunkingContext, ident::AssetIdent, issue::{IssueSeverity, IssueSource}, + output::OutputAsset, reference::AssetReference, reference_type::UrlReferenceSubType, resolve::{origin::ResolveOrigin, parse::Request, PrimaryResolveResult, ResolveResult}, @@ -24,7 +24,7 @@ use crate::{ #[turbo_tasks::value(into = "new")] pub enum ReferencedAsset { - Some(Vc>), + Some(Vc>), None, } diff --git a/crates/turbopack-dev-server/src/html.rs b/crates/turbopack-dev-server/src/html.rs index c00bfb9811539..4461afb6c689e 100644 --- a/crates/turbopack-dev-server/src/html.rs +++ b/crates/turbopack-dev-server/src/html.rs @@ -37,15 +37,15 @@ fn dev_html_chunk_reference_description() -> Vc { } #[turbo_tasks::value_impl] -impl OutputAsset for DevHtmlAsset {} - -#[turbo_tasks::value_impl] -impl Asset for DevHtmlAsset { +impl OutputAsset for DevHtmlAsset { #[turbo_tasks::function] 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() diff --git a/crates/turbopack-dev-server/src/source/asset_graph.rs b/crates/turbopack-dev-server/src/source/asset_graph.rs index 855501e2512ca..a5dad568fce48 100644 --- a/crates/turbopack-dev-server/src/source/asset_graph.rs +++ b/crates/turbopack-dev-server/src/source/asset_graph.rs @@ -8,9 +8,10 @@ use indexmap::{indexset, IndexSet}; use turbo_tasks::{Completion, State, Value, ValueToString, Vc}; use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ - asset::{Asset, AssetsSet}, + asset::Asset, introspect::{asset::IntrospectableAsset, Introspectable, IntrospectableChildren}, - reference::all_referenced_assets, + output::{OutputAsset, OutputAssetsSet}, + reference::all_referenced_output_assets, }; use super::{ @@ -20,14 +21,14 @@ use super::{ }; #[turbo_tasks::value(transparent)] -struct AssetsMap(HashMap>>); +struct OutputAssetsMap(HashMap>>); -type ExpandedState = State>>>; +type ExpandedState = State>>>; #[turbo_tasks::value(serialization = "none", eq = "manual", cell = "new")] pub struct AssetGraphContentSource { root_path: Vc, - root_assets: Vc, + root_assets: Vc, expanded: Option, } @@ -35,7 +36,10 @@ pub struct AssetGraphContentSource { impl AssetGraphContentSource { /// Serves all assets references by root_asset. #[turbo_tasks::function] - pub fn new_eager(root_path: Vc, root_asset: Vc>) -> Vc { + pub fn new_eager( + root_path: Vc, + root_asset: Vc>, + ) -> Vc { Self::cell(AssetGraphContentSource { root_path, root_assets: Vc::cell(indexset! { root_asset }), @@ -46,7 +50,10 @@ impl AssetGraphContentSource { /// Serves all assets references by root_asset. Only serve references of an /// asset when it has served its content before. #[turbo_tasks::function] - pub fn new_lazy(root_path: Vc, root_asset: Vc>) -> Vc { + pub fn new_lazy( + root_path: Vc, + root_asset: Vc>, + ) -> Vc { Self::cell(AssetGraphContentSource { root_path, root_assets: Vc::cell(indexset! { root_asset }), @@ -58,7 +65,7 @@ impl AssetGraphContentSource { #[turbo_tasks::function] pub fn new_eager_multiple( root_path: Vc, - root_assets: Vc, + root_assets: Vc, ) -> Vc { Self::cell(AssetGraphContentSource { root_path, @@ -72,7 +79,7 @@ impl AssetGraphContentSource { #[turbo_tasks::function] pub fn new_lazy_multiple( root_path: Vc, - root_assets: Vc, + root_assets: Vc, ) -> Vc { Self::cell(AssetGraphContentSource { root_path, @@ -82,7 +89,7 @@ impl AssetGraphContentSource { } #[turbo_tasks::function] - async fn all_assets_map(self: Vc) -> Result> { + async fn all_assets_map(self: Vc) -> Result> { let this = self.await?; Ok(Vc::cell( expand( @@ -96,10 +103,10 @@ impl AssetGraphContentSource { } async fn expand( - root_assets: &IndexSet>>, + root_assets: &IndexSet>>, root_path: &FileSystemPath, expanded: Option<&ExpandedState>, -) -> Result>>> { +) -> Result>>> { let mut map = HashMap::new(); let mut assets = Vec::new(); let mut queue = VecDeque::with_capacity(32); @@ -111,14 +118,14 @@ async fn expand( assets.push((root_asset.ident().path(), *root_asset)); assets_set.insert(*root_asset); if expanded { - queue.push_back(all_referenced_assets(*root_asset)); + queue.push_back(all_referenced_output_assets(*root_asset)); } } } else { for root_asset in root_assets.iter() { assets.push((root_asset.ident().path(), *root_asset)); assets_set.insert(*root_asset); - queue.push_back(all_referenced_assets(*root_asset)); + queue.push_back(all_referenced_output_assets(*root_asset)); } } @@ -135,7 +142,7 @@ async fn expand( true }; if expanded { - queue.push_back(all_referenced_assets(*asset)); + queue.push_back(all_referenced_output_assets(*asset)); } assets.push((asset.ident().path(), *asset)); } @@ -165,7 +172,7 @@ async fn expand( /// This must not be a TaskInput since this would resolve the embedded asset. #[turbo_tasks::value(serialization = "auto_for_input")] #[derive(Hash, PartialOrd, Ord, Debug, Clone)] -struct UnresolvedAsset(Vc>); +struct UnresolvedAsset(Vc>); #[turbo_tasks::value_impl] impl ContentSource for AssetGraphContentSource { @@ -198,7 +205,7 @@ impl ContentSource for AssetGraphContentSource { struct AssetGraphGetContentSourceContent { source: Vc, /// The unresolved asset. - asset: Vc>, + asset: Vc>, } #[turbo_tasks::value_impl] @@ -281,13 +288,13 @@ impl Introspectable for AssetGraphContentSource { let root_assets = this.root_assets.await?; let root_asset_children = root_assets .iter() - .map(|&asset| (key, IntrospectableAsset::new(asset))); + .map(|&asset| (key, IntrospectableAsset::new(Vc::upcast(asset)))); let expanded_assets = self.all_assets_map().await?; let expanded_asset_children = expanded_assets .values() .filter(|a| !root_assets.contains(*a)) - .map(|asset| (inner_key, IntrospectableAsset::new(*asset))); + .map(|asset| (inner_key, IntrospectableAsset::new(Vc::upcast(*asset)))); Ok(Vc::cell( root_asset_children @@ -327,7 +334,7 @@ impl Introspectable for FullyExpaned { expand(&*source.root_assets.await?, &*source.root_path.await?, None).await?; let children = expanded_assets .iter() - .map(|(_k, &v)| (key, IntrospectableAsset::new(v))) + .map(|(_k, &v)| (key, IntrospectableAsset::new(Vc::upcast(v)))) .collect(); Ok(Vc::cell(children)) diff --git a/crates/turbopack-dev/src/chunking_context.rs b/crates/turbopack-dev/src/chunking_context.rs index c34cdfd79d324..47395d7acd3e1 100644 --- a/crates/turbopack-dev/src/chunking_context.rs +++ b/crates/turbopack-dev/src/chunking_context.rs @@ -6,10 +6,10 @@ use turbo_tasks::{ }; use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ - asset::Asset, chunk::{Chunk, ChunkableModule, ChunkingContext, Chunks, EvaluatableAssets}, environment::Environment, ident::AssetIdent, + module::Module, output::{OutputAsset, OutputAssets}, }; use turbopack_css::chunk::CssChunk; @@ -233,7 +233,10 @@ impl ChunkingContext for DevChunkingContext { } #[turbo_tasks::function] - async fn reference_chunk_source_maps(&self, chunk: Vc>) -> Result> { + async fn reference_chunk_source_maps( + &self, + chunk: Vc>, + ) -> Result> { let mut source_maps = self.reference_chunk_source_maps; let path = chunk.ident().path().await?; let extension = path.extension_ref().unwrap_or_default(); @@ -250,8 +253,8 @@ impl ChunkingContext for DevChunkingContext { #[turbo_tasks::function] async fn can_be_in_same_chunk( &self, - asset_a: Vc>, - asset_b: Vc>, + asset_a: Vc>, + asset_b: Vc>, ) -> Result> { let parent_dir = asset_a.ident().path().parent().await?; diff --git a/crates/turbopack-dev/src/ecmascript/chunk.rs b/crates/turbopack-dev/src/ecmascript/chunk.rs index c7cd715cd9790..fc67f02c33ba8 100644 --- a/crates/turbopack-dev/src/ecmascript/chunk.rs +++ b/crates/turbopack-dev/src/ecmascript/chunk.rs @@ -3,7 +3,7 @@ use indexmap::IndexSet; use turbo_tasks::{ValueToString, Vc}; use turbopack_core::{ asset::{Asset, AssetContent}, - chunk::{ChunkingContext, OutputChunk, OutputChunkRuntimeInfo}, + chunk::{Chunk, ChunkingContext, OutputChunk, OutputChunkRuntimeInfo}, ident::AssetIdent, introspect::{Introspectable, IntrospectableChildren}, output::OutputAsset, @@ -74,16 +74,16 @@ impl EcmascriptDevChunk { } #[turbo_tasks::value_impl] -impl OutputAsset for EcmascriptDevChunk {} - -#[turbo_tasks::value_impl] -impl Asset for EcmascriptDevChunk { +impl OutputAsset for EcmascriptDevChunk { #[turbo_tasks::function] fn ident(&self) -> Vc { 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?; diff --git a/crates/turbopack-dev/src/ecmascript/content.rs b/crates/turbopack-dev/src/ecmascript/content.rs index 90bc3dd07a4e6..fe2a48155b3f6 100644 --- a/crates/turbopack-dev/src/ecmascript/content.rs +++ b/crates/turbopack-dev/src/ecmascript/content.rs @@ -5,9 +5,10 @@ use indoc::writedoc; use turbo_tasks::Vc; use turbo_tasks_fs::File; use turbopack_core::{ - asset::{Asset, AssetContent}, + asset::AssetContent, chunk::{ChunkingContext, ModuleId}, code_builder::{Code, CodeBuilder}, + output::OutputAsset, source_map::{GenerateSourceMap, OptionSourceMap}, version::{ MergeableVersionedContent, Update, Version, VersionedContent, VersionedContentMerger, diff --git a/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs b/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs index f84f89cada2c3..4566c21d3c1c4 100644 --- a/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs +++ b/crates/turbopack-dev/src/ecmascript/evaluate/chunk.rs @@ -10,6 +10,7 @@ use turbopack_core::{ chunk::{Chunk, ChunkData, ChunkingContext, ChunksData, EvaluatableAssets, ModuleId}, code_builder::{Code, CodeBuilder}, ident::AssetIdent, + module::Module, output::{OutputAsset, OutputAssets}, reference::AssetReferences, source_map::{GenerateSourceMap, OptionSourceMap, SourceMapAssetReference}, @@ -175,10 +176,7 @@ fn modifier() -> Vc { } #[turbo_tasks::value_impl] -impl OutputAsset for EcmascriptDevEvaluateChunk {} - -#[turbo_tasks::value_impl] -impl Asset for EcmascriptDevEvaluateChunk { +impl OutputAsset for EcmascriptDevEvaluateChunk { #[turbo_tasks::function] async fn ident(&self) -> Result> { let mut ident = self.entry_chunk.ident().await?.clone_value(); @@ -201,7 +199,10 @@ impl Asset 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?; diff --git a/crates/turbopack-dev/src/ecmascript/list/asset.rs b/crates/turbopack-dev/src/ecmascript/list/asset.rs index a03f0a02f2bbe..2518c84d9f3aa 100644 --- a/crates/turbopack-dev/src/ecmascript/list/asset.rs +++ b/crates/turbopack-dev/src/ecmascript/list/asset.rs @@ -76,10 +76,7 @@ fn chunk_list_chunk_reference_description() -> Vc { } #[turbo_tasks::value_impl] -impl OutputAsset for EcmascriptDevChunkList {} - -#[turbo_tasks::value_impl] -impl Asset for EcmascriptDevChunkList { +impl OutputAsset for EcmascriptDevChunkList { #[turbo_tasks::function] async fn ident(&self) -> Result> { let mut ident = self.entry_chunk.ident().await?.clone_value(); @@ -95,7 +92,10 @@ impl Asset 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( diff --git a/crates/turbopack-dev/src/ecmascript/list/content.rs b/crates/turbopack-dev/src/ecmascript/list/content.rs index 320b181f0a3dc..08b14ee8107ba 100644 --- a/crates/turbopack-dev/src/ecmascript/list/content.rs +++ b/crates/turbopack-dev/src/ecmascript/list/content.rs @@ -10,6 +10,7 @@ use turbopack_core::{ asset::{Asset, AssetContent}, chunk::ChunkingContext, code_builder::{Code, CodeBuilder}, + output::OutputAsset, version::{ MergeableVersionedContent, Update, Version, VersionedContent, VersionedContentMerger, }, diff --git a/crates/turbopack-dev/src/ecmascript/merged/update.rs b/crates/turbopack-dev/src/ecmascript/merged/update.rs index fbba8fd9e4e9f..73989c6032021 100644 --- a/crates/turbopack-dev/src/ecmascript/merged/update.rs +++ b/crates/turbopack-dev/src/ecmascript/merged/update.rs @@ -6,9 +6,9 @@ use serde::Serialize; use turbo_tasks::{IntoTraitRef, ReadRef, TryJoinIterExt, Vc}; use turbo_tasks_fs::rope::Rope; use turbopack_core::{ - asset::Asset, chunk::{ChunkingContext, ModuleId}, code_builder::Code, + output::OutputAsset, version::{PartialUpdate, TotalUpdate, Update, Version}, }; diff --git a/crates/turbopack-ecmascript/src/chunk/content.rs b/crates/turbopack-ecmascript/src/chunk/content.rs index 057653ae305fe..c713d6d50c864 100644 --- a/crates/turbopack-ecmascript/src/chunk/content.rs +++ b/crates/turbopack-ecmascript/src/chunk/content.rs @@ -110,12 +110,12 @@ async fn ecmascript_chunk_content_single_entry( entry: Vc>, availability_info: Value, ) -> Result> { - let asset = Vc::upcast(entry); + let module = Vc::upcast(entry); Ok(EcmascriptChunkContent::cell( if let Some(res) = chunk_content::>( Vc::upcast(context), - asset, + module, None, availability_info, ) @@ -125,7 +125,7 @@ async fn ecmascript_chunk_content_single_entry( } else { chunk_content_split::>( Vc::upcast(context), - asset, + module, None, availability_info, ) diff --git a/crates/turbopack-ecmascript/src/chunk/item.rs b/crates/turbopack-ecmascript/src/chunk/item.rs index 680cd1c9e55b0..5e457a5845df1 100644 --- a/crates/turbopack-ecmascript/src/chunk/item.rs +++ b/crates/turbopack-ecmascript/src/chunk/item.rs @@ -5,7 +5,6 @@ use serde::{Deserialize, Serialize}; use turbo_tasks::{trace::TraceRawVcs, Upcast, Value, ValueToString, Vc}; use turbo_tasks_fs::rope::Rope; use turbopack_core::{ - asset::Asset, chunk::{ availability_info::AvailabilityInfo, available_assets::AvailableAssets, ChunkItem, ChunkableModule, ChunkingContext, FromChunkableModule, ModuleId, @@ -13,6 +12,7 @@ use turbopack_core::{ code_builder::{Code, CodeBuilder}, error::PrettyPrintError, issue::{code_gen::CodeGenerationIssue, IssueExt, IssueSeverity}, + module::Module, }; use super::{EcmascriptChunkPlaceable, EcmascriptChunkingContext}; @@ -213,10 +213,10 @@ async fn module_factory_with_code_generation_issue( impl FromChunkableModule for Box { async fn from_asset( context: Vc>, - asset: Vc>, + module: Vc>, ) -> Result>> { let Some(placeable) = - Vc::try_resolve_sidecast::>(asset).await? + Vc::try_resolve_sidecast::>(module).await? else { return Ok(None); }; @@ -232,7 +232,7 @@ impl FromChunkableModule for Box { async fn from_async_asset( context: Vc>, - asset: Vc>, + module: Vc>, availability_info: Value, ) -> Result>> { let Some(context) = @@ -247,19 +247,19 @@ impl FromChunkableModule for Box { current_availability_root, } => AvailabilityInfo::Inner { available_assets: AvailableAssets::new(vec![current_availability_root]), - current_availability_root: Vc::upcast(asset), + current_availability_root: Vc::upcast(module), }, AvailabilityInfo::Inner { available_assets, current_availability_root, } => AvailabilityInfo::Inner { available_assets: available_assets.with_roots(vec![current_availability_root]), - current_availability_root: Vc::upcast(asset), + current_availability_root: Vc::upcast(module), }, }; let manifest_asset = - ManifestChunkAsset::new(asset, context, Value::new(next_availability_info)); + ManifestChunkAsset::new(module, context, Value::new(next_availability_info)); Ok(Some(Vc::upcast(ManifestLoaderItem::new(manifest_asset)))) } } diff --git a/crates/turbopack-ecmascript/src/chunk/mod.rs b/crates/turbopack-ecmascript/src/chunk/mod.rs index a9720d5b8b835..fb7e2797d0e48 100644 --- a/crates/turbopack-ecmascript/src/chunk/mod.rs +++ b/crates/turbopack-ecmascript/src/chunk/mod.rs @@ -20,6 +20,7 @@ use turbopack_core::{ asset::{children_from_asset_references, content_to_details, IntrospectableAsset}, Introspectable, IntrospectableChildren, }, + module::Module, reference::AssetReferences, }; @@ -217,6 +218,79 @@ pub struct EcmascriptChunkComparison { #[turbo_tasks::value_impl] impl Chunk for EcmascriptChunk { + #[turbo_tasks::function] + async fn ident(self: Vc) -> Result> { + let this = self.await?; + + // All information that makes the chunk unique need to be encoded in the params. + + // All main entries are included + let main_entries = this.main_entries.await?; + let main_entry_key = Vc::cell(String::new()); + let mut assets = main_entries + .iter() + .map(|entry| (main_entry_key, entry.ident())) + .collect::>(); + + // The primary name of the chunk is the only entry or the common parent of all + // entries. + let path = if let [(_, ident)] = &assets[..] { + ident.path() + } else if let &Some(common_parent) = &*self.common_parent().await? { + common_parent + } else { + let (_, ident) = assets[0]; + ident.path() + }; + + // All omit entries are included + if let Some(omit_entries) = this.omit_entries { + let omit_entries = omit_entries.await?; + let omit_entry_key = Vc::cell("omit".to_string()); + for entry in omit_entries.iter() { + assets.push((omit_entry_key, entry.ident())) + } + } + + // Current availability root is included + if let Some(current_availability_root) = this.availability_info.current_availability_root() + { + let ident = current_availability_root.ident(); + let need_root = if let [(_, main_entry)] = &assets[..] { + main_entry.resolve().await? != ident.resolve().await? + } else { + true + }; + if need_root { + let availability_root_key = Vc::cell("current_availability_root".to_string()); + assets.push((availability_root_key, ident)); + } + } + + let mut modifiers = vec![]; + + // Available assets are included + if let Some(available_assets) = this.availability_info.available_assets() { + modifiers.push(Vc::cell(available_assets.hash().await?.to_string())); + } + + // Simplify when it's only a single main entry without extra info + let ident = if assets.len() == 1 && modifiers.is_empty() { + assets[0].1 + } else { + AssetIdent::new(Value::new(AssetIdent { + path, + query: None, + fragment: None, + assets, + modifiers, + part: None, + })) + }; + + Ok(ident) + } + #[turbo_tasks::function] fn chunking_context(&self) -> Vc> { Vc::upcast(self.context) @@ -297,79 +371,6 @@ impl EcmascriptChunk { #[turbo_tasks::value_impl] impl Asset for EcmascriptChunk { - #[turbo_tasks::function] - async fn ident(self: Vc) -> Result> { - let this = self.await?; - - // All information that makes the chunk unique need to be encoded in the params. - - // All main entries are included - let main_entries = this.main_entries.await?; - let main_entry_key = Vc::cell(String::new()); - let mut assets = main_entries - .iter() - .map(|entry| (main_entry_key, entry.ident())) - .collect::>(); - - // The primary name of the chunk is the only entry or the common parent of all - // entries. - let path = if let [(_, ident)] = &assets[..] { - ident.path() - } else if let &Some(common_parent) = &*self.common_parent().await? { - common_parent - } else { - let (_, ident) = assets[0]; - ident.path() - }; - - // All omit entries are included - if let Some(omit_entries) = this.omit_entries { - let omit_entries = omit_entries.await?; - let omit_entry_key = Vc::cell("omit".to_string()); - for entry in omit_entries.iter() { - assets.push((omit_entry_key, entry.ident())) - } - } - - // Current availability root is included - if let Some(current_availability_root) = this.availability_info.current_availability_root() - { - let ident = current_availability_root.ident(); - let need_root = if let [(_, main_entry)] = &assets[..] { - main_entry.resolve().await? != ident.resolve().await? - } else { - true - }; - if need_root { - let availability_root_key = Vc::cell("current_availability_root".to_string()); - assets.push((availability_root_key, ident)); - } - } - - let mut modifiers = vec![]; - - // Available assets are included - if let Some(available_assets) = this.availability_info.available_assets() { - modifiers.push(Vc::cell(available_assets.hash().await?.to_string())); - } - - // Simplify when it's only a single main entry without extra info - let ident = if assets.len() == 1 && modifiers.is_empty() { - assets[0].1 - } else { - AssetIdent::new(Value::new(AssetIdent { - path, - query: None, - fragment: None, - assets, - modifiers, - part: None, - })) - }; - - Ok(ident) - } - #[turbo_tasks::function] fn content(self: Vc) -> Result> { bail!("EcmascriptChunk::content() is not implemented") diff --git a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs index da9495150d1cc..5167044df5e74 100644 --- a/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs +++ b/crates/turbopack-ecmascript/src/chunk_group_files_asset.rs @@ -14,7 +14,7 @@ use turbopack_core::{ Introspectable, IntrospectableChildren, }, module::Module, - output::OutputAssets, + output::{OutputAsset, OutputAssets}, reference::{AssetReference, AssetReferences, SingleAssetReference}, }; @@ -53,12 +53,15 @@ fn runtime_entry_description() -> Vc { } #[turbo_tasks::value_impl] -impl Asset for ChunkGroupFilesAsset { +impl Module for ChunkGroupFilesAsset { #[turbo_tasks::function] 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()) @@ -84,9 +87,6 @@ impl Asset for ChunkGroupFilesAsset { } } -#[turbo_tasks::value_impl] -impl Module for ChunkGroupFilesAsset {} - #[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 f7b952973d138..ddbef65e1936f 100644 --- a/crates/turbopack-ecmascript/src/lib.rs +++ b/crates/turbopack-ecmascript/src/lib.rs @@ -52,7 +52,7 @@ pub use transform::{ use turbo_tasks::{trace::TraceRawVcs, RawVc, ReadRef, TryJoinIterExt, Value, ValueToString, Vc}; use turbo_tasks_fs::{rope::Rope, FileSystemPath}; use turbopack_core::{ - asset::{Asset, AssetContent, AssetOption}, + asset::{Asset, AssetContent}, chunk::{ availability_info::AvailabilityInfo, Chunk, ChunkItem, ChunkableModule, ChunkingContext, EvaluatableAsset, @@ -60,7 +60,7 @@ use turbopack_core::{ compile_time_info::CompileTimeInfo, context::AssetContext, ident::AssetIdent, - module::Module, + module::{Module, OptionModule}, reference::AssetReferences, reference_type::InnerAssets, resolve::{origin::ResolveOrigin, parse::Request, ModulePart}, @@ -376,7 +376,7 @@ impl EcmascriptModuleAsset { } #[turbo_tasks::value_impl] -impl Asset for EcmascriptModuleAsset { +impl Module for EcmascriptModuleAsset { #[turbo_tasks::function] async fn ident(&self) -> Result> { if let Some(inner_assets) = self.inner_assets { @@ -390,7 +390,10 @@ impl Asset for EcmascriptModuleAsset { Ok(self.source.ident().with_modifier(modifier())) } } +} +#[turbo_tasks::value_impl] +impl Asset for EcmascriptModuleAsset { #[turbo_tasks::function] fn content(&self) -> Vc { self.source.content() @@ -402,9 +405,6 @@ impl Asset for EcmascriptModuleAsset { } } -#[turbo_tasks::value_impl] -impl Module for EcmascriptModuleAsset {} - #[turbo_tasks::value_impl] impl ChunkableModule for EcmascriptModuleAsset { #[turbo_tasks::function] @@ -456,7 +456,7 @@ impl ResolveOrigin for EcmascriptModuleAsset { } #[turbo_tasks::function] - async fn get_inner_asset(&self, request: Vc) -> Result> { + async fn get_inner_asset(&self, request: Vc) -> Result> { Ok(Vc::cell(if let Some(inner_assets) = &self.inner_assets { if let Some(request) = request.await?.request() { inner_assets.await?.get(&request).copied() diff --git a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs index e96033457079a..dc969b6446d2e 100644 --- a/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs +++ b/crates/turbopack-ecmascript/src/manifest/chunk_asset.rs @@ -83,12 +83,15 @@ fn manifest_chunk_reference_description() -> Vc { } #[turbo_tasks::value_impl] -impl Asset for ManifestChunkAsset { +impl Module for ManifestChunkAsset { #[turbo_tasks::function] 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!() @@ -114,9 +117,6 @@ impl Asset for ManifestChunkAsset { } } -#[turbo_tasks::value_impl] -impl Module for ManifestChunkAsset {} - #[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 54d4c74d4a1b3..992fd3403527b 100644 --- a/crates/turbopack-ecmascript/src/manifest/chunk_item.rs +++ b/crates/turbopack-ecmascript/src/manifest/chunk_item.rs @@ -5,6 +5,7 @@ use turbopack_core::{ asset::Asset, chunk::{ChunkData, ChunkItem, ChunkingContext, ChunksData}, ident::AssetIdent, + module::Module, reference::AssetReferences, }; diff --git a/crates/turbopack-ecmascript/src/manifest/loader_item.rs b/crates/turbopack-ecmascript/src/manifest/loader_item.rs index fd97790318a59..0906cb164c0ae 100644 --- a/crates/turbopack-ecmascript/src/manifest/loader_item.rs +++ b/crates/turbopack-ecmascript/src/manifest/loader_item.rs @@ -4,9 +4,9 @@ use anyhow::{anyhow, Result}; use indoc::writedoc; use turbo_tasks::{TryJoinIterExt, Vc}; use turbopack_core::{ - asset::Asset, chunk::{ChunkData, ChunkItem, ChunkingContext, ChunksData}, ident::AssetIdent, + module::Module, reference::{AssetReferences, SingleAssetReference}, }; diff --git a/crates/turbopack-ecmascript/src/references/esm/base.rs b/crates/turbopack-ecmascript/src/references/esm/base.rs index 7cae330bf00aa..1de8b0faddcff 100644 --- a/crates/turbopack-ecmascript/src/references/esm/base.rs +++ b/crates/turbopack-ecmascript/src/references/esm/base.rs @@ -2,16 +2,16 @@ use anyhow::{anyhow, bail, Result}; use lazy_static::lazy_static; use swc_core::{ common::DUMMY_SP, - ecma::ast::{Expr, ExprStmt, Ident, Lit, Module, ModuleItem, Program, Script, Stmt}, + ecma::ast::{self, Expr, ExprStmt, Ident, Lit, ModuleItem, Program, Script, Stmt}, quote, }; use turbo_tasks::{Value, ValueToString, Vc}; use turbopack_core::{ - asset::Asset, chunk::{ ChunkableModuleReference, ChunkingContext, ChunkingType, ChunkingTypeOption, ModuleId, }, issue::{IssueSeverity, OptionIssueSource}, + module::Module, reference::AssetReference, reference_type::EcmaScriptModulesReferenceSubType, resolve::{ @@ -278,7 +278,7 @@ lazy_static! { pub(crate) fn insert_hoisted_stmt(program: &mut Program, stmt: Stmt) { match program { - Program::Module(Module { body, .. }) => { + Program::Module(ast::Module { body, .. }) => { let pos = body.iter().position(|item| { if let ModuleItem::Stmt(Stmt::Expr(ExprStmt { expr: box Expr::Lit(Lit::Str(s)), diff --git a/crates/turbopack-ecmascript/src/references/esm/export.rs b/crates/turbopack-ecmascript/src/references/esm/export.rs index 19407c576e103..e6e8b5ef840bc 100644 --- a/crates/turbopack-ecmascript/src/references/esm/export.rs +++ b/crates/turbopack-ecmascript/src/references/esm/export.rs @@ -8,15 +8,15 @@ use serde::{Deserialize, Serialize}; use swc_core::{ common::DUMMY_SP, ecma::ast::{ - ComputedPropName, Expr, ExprStmt, Ident, KeyValueProp, Lit, MemberExpr, MemberProp, Module, + self, ComputedPropName, Expr, ExprStmt, Ident, KeyValueProp, Lit, MemberExpr, MemberProp, ModuleItem, ObjectLit, Program, Prop, PropName, PropOrSpread, Script, Stmt, Str, }, quote, quote_expr, }; use turbo_tasks::{trace::TraceRawVcs, ValueToString, Vc}; use turbopack_core::{ - asset::Asset, issue::{analyze::AnalyzeIssue, IssueExt, IssueSeverity}, + module::Module, }; use super::{base::ReferencedAsset, EsmAssetReference}; @@ -243,7 +243,7 @@ impl CodeGenerateable for EsmExports { getters: Expr = getters.clone() ); match program { - Program::Module(Module { body, .. }) => { + Program::Module(ast::Module { body, .. }) => { body.insert(0, ModuleItem::Stmt(stmt)); } Program::Script(Script { body, .. }) => { diff --git a/crates/turbopack-ecmascript/src/references/mod.rs b/crates/turbopack-ecmascript/src/references/mod.rs index 266e658ceb996..990a29b444813 100644 --- a/crates/turbopack-ecmascript/src/references/mod.rs +++ b/crates/turbopack-ecmascript/src/references/mod.rs @@ -47,10 +47,10 @@ use swc_core::{ use turbo_tasks::{TryJoinIterExt, Upcast, Value, Vc}; use turbo_tasks_fs::{FileJsonContent, FileSystemPath}; use turbopack_core::{ - asset::Asset, compile_time_info::{CompileTimeInfo, FreeVarReference}, error::PrettyPrintError, issue::{IssueExt, IssueSource, OptionIssueSource}, + module::Module, reference::{AssetReference, AssetReferences, SourceMapReference}, reference_type::{CommonJsReferenceSubType, ReferenceType}, resolve::{ @@ -935,11 +935,7 @@ pub(crate) async fn analyze_ecmascript_module( Request::parse(Value::new(pat)), compile_time_info.environment().rendering(), Vc::cell(ast_path), - IssueSource::from_byte_offset( - Vc::upcast(source), - span.lo.to_usize(), - span.hi.to_usize(), - ), + IssueSource::from_byte_offset(source, span.lo.to_usize(), span.hi.to_usize()), in_try, )); } @@ -1734,7 +1730,7 @@ async fn handle_free_var_reference( } fn issue_source(source: Vc>, span: Span) -> Vc { - IssueSource::from_byte_offset(Vc::upcast(source), span.lo.to_usize(), span.hi.to_usize()) + IssueSource::from_byte_offset(source, span.lo.to_usize(), span.hi.to_usize()) } fn analyze_amd_define( @@ -2066,8 +2062,13 @@ async fn require_resolve_visitor( .primary .iter() .map(|result| async move { - Ok(if let PrimaryResolveResult::Asset(asset) = result { - Some(require_resolve(asset.ident().path()).await?) + Ok(if let &PrimaryResolveResult::Asset(asset) = result { + if let Some(module) = Vc::try_resolve_downcast::>(asset).await? + { + Some(require_resolve(module.ident().path()).await?) + } else { + None + } } else { None }) diff --git a/crates/turbopack-ecmascript/src/references/pattern_mapping.rs b/crates/turbopack-ecmascript/src/references/pattern_mapping.rs index 732da93ac0881..225e0c8a067e7 100644 --- a/crates/turbopack-ecmascript/src/references/pattern_mapping.rs +++ b/crates/turbopack-ecmascript/src/references/pattern_mapping.rs @@ -4,9 +4,8 @@ use swc_core::{ ecma::ast::{Expr, Lit}, quote, }; -use turbo_tasks::{debug::ValueDebug, Value, ValueToString, Vc}; +use turbo_tasks::{debug::ValueDebug, Value, Vc}; use turbopack_core::{ - asset::Asset, chunk::{ availability_info::AvailabilityInfo, ChunkableModule, ChunkingContext, FromChunkableModule, ModuleId, @@ -189,7 +188,7 @@ impl PatternMapping { } } if let Some(chunk_item) = - >::from_asset(context, asset).await? + >::from_asset(context, Vc::upcast(chunkable)).await? { return Ok(PatternMapping::cell(PatternMapping::Single( chunk_item.id().await?.clone_value(), @@ -199,10 +198,9 @@ impl PatternMapping { CodeGenerationIssue { severity: IssueSeverity::Bug.into(), title: Vc::cell("non-ecmascript placeable asset".to_string()), - message: Vc::cell(format!( - "asset {} is not placeable in ESM chunks, so it doesn't have a module id", - asset.ident().to_string().await? - )), + message: Vc::cell( + "asset is not placeable in ESM chunks, so it doesn't have a module id".to_string(), + ), path: origin.origin_path(), } .cell() diff --git a/crates/turbopack-ecmascript/src/references/raw.rs b/crates/turbopack-ecmascript/src/references/raw.rs index 31ec7fa8d44dd..09b16a4c8f289 100644 --- a/crates/turbopack-ecmascript/src/references/raw.rs +++ b/crates/turbopack-ecmascript/src/references/raw.rs @@ -1,7 +1,6 @@ use anyhow::Result; use turbo_tasks::{ValueToString, Vc}; use turbopack_core::{ - asset::Asset, reference::AssetReference, resolve::{pattern::Pattern, resolve_raw, ResolveResult}, source::Source, diff --git a/crates/turbopack-ecmascript/src/references/require_context.rs b/crates/turbopack-ecmascript/src/references/require_context.rs index 753e7b5697d78..e2eba505a7b6b 100644 --- a/crates/turbopack-ecmascript/src/references/require_context.rs +++ b/crates/turbopack-ecmascript/src/references/require_context.rs @@ -344,14 +344,17 @@ fn modifier(dir: String, include_subdirs: bool) -> Vc { } #[turbo_tasks::value_impl] -impl Asset for RequireContextAsset { +impl Module for RequireContextAsset { #[turbo_tasks::function] fn ident(&self) -> Vc { self.source .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!() @@ -369,9 +372,6 @@ impl Asset for RequireContextAsset { } } -#[turbo_tasks::value_impl] -impl Module for RequireContextAsset {} - #[turbo_tasks::value_impl] impl ChunkableModule for RequireContextAsset { #[turbo_tasks::function] diff --git a/crates/turbopack-ecmascript/src/resolve/node_native_binding.rs b/crates/turbopack-ecmascript/src/resolve/node_native_binding.rs index 556ed084aa166..58fcc03b664c3 100644 --- a/crates/turbopack-ecmascript/src/resolve/node_native_binding.rs +++ b/crates/turbopack-ecmascript/src/resolve/node_native_binding.rs @@ -11,6 +11,7 @@ use turbo_tasks_fs::{ use turbopack_core::{ asset::{Asset, AssetContent}, file_source::FileSource, + module::{convert_asset_to_module, Module}, reference::AssetReference, resolve::{ pattern::Pattern, resolve_raw, AffectingResolvingAssetReference, PrimaryResolveResult, @@ -100,6 +101,7 @@ pub async fn resolve_node_pre_gyp_files( .await?; let compile_target = compile_target.await?; if let Some(config_asset) = *config { + let config_asset = convert_asset_to_module(config_asset); if let AssetContent::File(file) = &*config_asset.content().await? { if let FileContent::Content(ref config_file) = &*file.await? { let config_file_path = config_asset.ident().path(); @@ -140,9 +142,10 @@ pub async fn resolve_node_pre_gyp_files( .results .iter() { - if let DirectoryEntry::File(dylib) | DirectoryEntry::Symlink(dylib) = entry + if let &DirectoryEntry::File(dylib) | &DirectoryEntry::Symlink(dylib) = + entry { - assets.insert(Vc::upcast(FileSource::new(*dylib))); + assets.insert(Vc::upcast(FileSource::new(dylib))); } } assets.insert(Vc::upcast(FileSource::new(resolved_file_vc))); @@ -159,16 +162,16 @@ pub async fn resolve_node_pre_gyp_files( .results .values() { - match entry { + match *entry { DirectoryEntry::File(dylib) => { - assets.insert(Vc::upcast(FileSource::new(*dylib))); + assets.insert(Vc::upcast(FileSource::new(dylib))); } DirectoryEntry::Symlink(dylib) => { let realpath_with_links = dylib.realpath_with_links().await?; for symlink in realpath_with_links.symlinks.iter() { assets.insert(Vc::upcast(FileSource::new(*symlink))); } - assets.insert(Vc::upcast(FileSource::new(*dylib))); + assets.insert(Vc::upcast(FileSource::new(dylib))); } _ => {} } diff --git a/crates/turbopack-ecmascript/src/text/mod.rs b/crates/turbopack-ecmascript/src/text/mod.rs index 893acc7bb5120..1fb3e20e0766c 100644 --- a/crates/turbopack-ecmascript/src/text/mod.rs +++ b/crates/turbopack-ecmascript/src/text/mod.rs @@ -30,10 +30,7 @@ impl TextContentFileSource { } #[turbo_tasks::value_impl] -impl Source for TextContentFileSource {} - -#[turbo_tasks::value_impl] -impl Asset for TextContentFileSource { +impl Source for TextContentFileSource { #[turbo_tasks::function] fn ident(&self) -> Vc { self.source @@ -41,7 +38,10 @@ impl Asset for TextContentFileSource { .with_modifier(modifier()) .rename_as("*.mjs".to_string()) } +} +#[turbo_tasks::value_impl] +impl Asset for TextContentFileSource { #[turbo_tasks::function] async fn content(&self) -> Result> { let source = self.source.content().file_content(); diff --git a/crates/turbopack-ecmascript/src/tree_shake/asset.rs b/crates/turbopack-ecmascript/src/tree_shake/asset.rs index e25318c70d911..d281ba38d2d0d 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/asset.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/asset.rs @@ -43,6 +43,16 @@ impl EcmascriptModulePartAsset { } } +#[turbo_tasks::value_impl] +impl Module for EcmascriptModulePartAsset { + #[turbo_tasks::function] + async fn ident(&self) -> Result> { + let inner = self.full_module.ident(); + + Ok(inner.with_part(self.part)) + } +} + #[turbo_tasks::value_impl] impl Asset for EcmascriptModulePartAsset { #[turbo_tasks::function] @@ -94,13 +104,6 @@ impl Asset for EcmascriptModulePartAsset { Ok(Vc::cell(assets)) } - - #[turbo_tasks::function] - async fn ident(&self) -> Result> { - let inner = self.full_module.ident(); - - Ok(inner.with_part(self.part)) - } } #[turbo_tasks::value_impl] @@ -125,9 +128,6 @@ impl EcmascriptChunkPlaceable for EcmascriptModulePartAsset { } } -#[turbo_tasks::value_impl] -impl Module for EcmascriptModulePartAsset {} - #[turbo_tasks::value_impl] impl ChunkableModule 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 af8b5080999b1..a3f08b173e48e 100644 --- a/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs +++ b/crates/turbopack-ecmascript/src/tree_shake/chunk_item.rs @@ -4,6 +4,7 @@ use turbopack_core::{ asset::Asset, chunk::{availability_info::AvailabilityInfo, ChunkItem}, ident::AssetIdent, + module::Module, reference::AssetReferences, }; diff --git a/crates/turbopack-ecmascript/src/typescript/mod.rs b/crates/turbopack-ecmascript/src/typescript/mod.rs index d007940d09ffc..841df81fa2f1a 100644 --- a/crates/turbopack-ecmascript/src/typescript/mod.rs +++ b/crates/turbopack-ecmascript/src/typescript/mod.rs @@ -8,6 +8,7 @@ use turbopack_core::{ asset::{Asset, AssetContent}, ident::AssetIdent, issue::{IssueSeverity, OptionIssueSource}, + module::Module, reference::{AssetReference, AssetReferences}, reference_type::{CommonJsReferenceSubType, ReferenceType}, resolve::{ @@ -38,12 +39,15 @@ impl TsConfigModuleAsset { } #[turbo_tasks::value_impl] -impl Asset for TsConfigModuleAsset { +impl Module for TsConfigModuleAsset { #[turbo_tasks::function] 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() diff --git a/crates/turbopack-ecmascript/src/webpack/mod.rs b/crates/turbopack-ecmascript/src/webpack/mod.rs index fc485ae942555..4c65daff8d6f8 100644 --- a/crates/turbopack-ecmascript/src/webpack/mod.rs +++ b/crates/turbopack-ecmascript/src/webpack/mod.rs @@ -5,6 +5,7 @@ use turbopack_core::{ asset::{Asset, AssetContent}, file_source::FileSource, ident::AssetIdent, + module::Module, reference::{AssetReference, AssetReferences}, reference_type::{CommonJsReferenceSubType, ReferenceType}, resolve::{ @@ -51,12 +52,15 @@ impl WebpackModuleAsset { } #[turbo_tasks::value_impl] -impl Asset for WebpackModuleAsset { +impl Module for WebpackModuleAsset { #[turbo_tasks::function] fn ident(&self) -> Vc { self.source.ident().with_modifier(modifier()) } +} +#[turbo_tasks::value_impl] +impl Asset for WebpackModuleAsset { #[turbo_tasks::function] fn content(&self) -> Vc { self.source.content() diff --git a/crates/turbopack-ecmascript/src/webpack/parse.rs b/crates/turbopack-ecmascript/src/webpack/parse.rs index b9fdf53598a25..24379f1462e7d 100644 --- a/crates/turbopack-ecmascript/src/webpack/parse.rs +++ b/crates/turbopack-ecmascript/src/webpack/parse.rs @@ -13,7 +13,7 @@ use swc_core::{ }; use turbo_tasks::{Value, Vc}; use turbo_tasks_fs::FileSystemPath; -use turbopack_core::{asset::Asset, source::Source}; +use turbopack_core::source::Source; use crate::{ analyzer::{graph::EvalContext, JsValue}, diff --git a/crates/turbopack-env/src/asset.rs b/crates/turbopack-env/src/asset.rs index ac6629e1d8c31..0533718cbb832 100644 --- a/crates/turbopack-env/src/asset.rs +++ b/crates/turbopack-env/src/asset.rs @@ -31,15 +31,15 @@ impl ProcessEnvAsset { } #[turbo_tasks::value_impl] -impl Source for ProcessEnvAsset {} - -#[turbo_tasks::value_impl] -impl Asset for ProcessEnvAsset { +impl Source for ProcessEnvAsset { #[turbo_tasks::function] fn ident(&self) -> Vc { AssetIdent::from_path(self.root.join(".env.js".to_string())) } +} +#[turbo_tasks::value_impl] +impl Asset for ProcessEnvAsset { #[turbo_tasks::function] async fn content(&self) -> Result> { let env = self.env.read_all().await?; diff --git a/crates/turbopack-json/src/lib.rs b/crates/turbopack-json/src/lib.rs index 58c42908c7faa..ced0b6abd363b 100644 --- a/crates/turbopack-json/src/lib.rs +++ b/crates/turbopack-json/src/lib.rs @@ -48,21 +48,21 @@ impl JsonModuleAsset { } #[turbo_tasks::value_impl] -impl Asset for JsonModuleAsset { +impl Module for JsonModuleAsset { #[turbo_tasks::function] fn ident(&self) -> Vc { self.source.ident().with_modifier(modifier()) } +} +#[turbo_tasks::value_impl] +impl Asset for JsonModuleAsset { #[turbo_tasks::function] fn content(&self) -> Vc { self.source.content() } } -#[turbo_tasks::value_impl] -impl Module for JsonModuleAsset {} - #[turbo_tasks::value_impl] impl ChunkableModule for JsonModuleAsset { #[turbo_tasks::function] diff --git a/crates/turbopack-mdx/src/lib.rs b/crates/turbopack-mdx/src/lib.rs index 0af70af0402c3..e5a4c064fad81 100644 --- a/crates/turbopack-mdx/src/lib.rs +++ b/crates/turbopack-mdx/src/lib.rs @@ -166,12 +166,15 @@ impl MdxModuleAsset { } #[turbo_tasks::value_impl] -impl Asset for MdxModuleAsset { +impl Module for MdxModuleAsset { #[turbo_tasks::function] fn ident(&self) -> Vc { self.source.ident().with_modifier(modifier()) } +} +#[turbo_tasks::value_impl] +impl Asset for MdxModuleAsset { #[turbo_tasks::function] fn content(&self) -> Vc { self.source.content() @@ -183,9 +186,6 @@ impl Asset for MdxModuleAsset { } } -#[turbo_tasks::value_impl] -impl Module for MdxModuleAsset {} - #[turbo_tasks::value_impl] impl ChunkableModule for MdxModuleAsset { #[turbo_tasks::function] diff --git a/crates/turbopack-node/src/bootstrap.rs b/crates/turbopack-node/src/bootstrap.rs index 510c6c35edcdc..a31ac20947591 100644 --- a/crates/turbopack-node/src/bootstrap.rs +++ b/crates/turbopack-node/src/bootstrap.rs @@ -7,7 +7,7 @@ use turbopack_core::{ asset::{Asset, AssetContent}, chunk::{Chunk, ChunkingContext, EvaluatableAssets}, ident::AssetIdent, - output::OutputAssets, + output::{OutputAsset, OutputAssets}, reference::{AssetReferences, SingleAssetReference}, }; use turbopack_ecmascript::utils::StringifyJs; @@ -33,12 +33,15 @@ impl NodeJsBootstrapAsset { } #[turbo_tasks::value_impl] -impl Asset for NodeJsBootstrapAsset { +impl OutputAsset for NodeJsBootstrapAsset { #[turbo_tasks::function] fn ident(&self) -> Vc { AssetIdent::from_path(self.path) } +} +#[turbo_tasks::value_impl] +impl Asset for NodeJsBootstrapAsset { #[turbo_tasks::function] async fn content(&self) -> Result> { let context_path = self.path.parent().await?; diff --git a/crates/turbopack-node/src/evaluate.rs b/crates/turbopack-node/src/evaluate.rs index 13e8ee9dcaa4c..038140464db86 100644 --- a/crates/turbopack-node/src/evaluate.rs +++ b/crates/turbopack-node/src/evaluate.rs @@ -21,12 +21,13 @@ use turbo_tasks_fs::{ glob::Glob, to_sys_path, DirectoryEntry, File, FileSystemPath, ReadGlobResult, }; use turbopack_core::{ - asset::{Asset, AssetContent}, + asset::AssetContent, chunk::{ChunkableModule, ChunkingContext, EvaluatableAsset, EvaluatableAssets}, context::AssetContext, file_source::FileSource, ident::AssetIdent, issue::{Issue, IssueExt, IssueSeverity}, + module::Module, reference_type::{InnerAssets, ReferenceType}, virtual_source::VirtualSource, }; @@ -94,7 +95,7 @@ pub struct JavaScriptEvaluation(#[turbo_tasks(trace_ignore)] JavaScriptStream); /// Pass the file you cared as `runtime_entries` to invalidate and reload the /// evaluated result automatically. pub async fn get_evaluate_pool( - module_asset: Vc>, + module_asset: Vc>, cwd: Vc, env: Vc>, context: Vc>, @@ -133,7 +134,7 @@ pub async fn get_evaluate_pool( )), Value::new(ReferenceType::Internal(Vc::cell(indexmap! { "INNER".to_string() => module_asset, - "RUNTIME".to_string() => Vc::upcast(runtime_asset) + "RUNTIME".to_string() => runtime_asset }))), ); @@ -179,7 +180,7 @@ pub async fn get_evaluate_pool( .cell(), ); - let output_root = chunking_context.output_root(); + let output_root: Vc = chunking_context.output_root(); let emit_package = emit_package_json(output_root); let emit = emit(bootstrap, output_root); let assets_for_source_mapping = internal_assets_for_source_mapping(bootstrap, output_root); @@ -228,7 +229,7 @@ impl futures_retry::ErrorHandler for PoolErrorHandler { /// evaluated result automatically. #[turbo_tasks::function] pub fn evaluate( - module_asset: Vc>, + module_asset: Vc>, cwd: Vc, env: Vc>, context_ident_for_issue: Vc, @@ -292,7 +293,7 @@ pub fn evaluate( #[turbo_tasks::function] async fn compute_evaluate_stream( - module_asset: Vc>, + module_asset: Vc>, cwd: Vc, env: Vc>, context_ident_for_issue: Vc, diff --git a/crates/turbopack-node/src/lib.rs b/crates/turbopack-node/src/lib.rs index c4f1a26600acf..97be4a133e970 100644 --- a/crates/turbopack-node/src/lib.rs +++ b/crates/turbopack-node/src/lib.rs @@ -17,11 +17,13 @@ use turbo_tasks::{ use turbo_tasks_env::ProcessEnv; use turbo_tasks_fs::{to_sys_path, File, FileSystemPath}; use turbopack_core::{ - asset::{Asset, AssetContent, AssetsSet}, + asset::{Asset, AssetContent}, chunk::{ChunkableModule, ChunkingContext, EvaluatableAsset, EvaluatableAssets}, - reference::primary_referenced_assets, + module::Module, + output::{OutputAsset, OutputAssetsSet}, + reference::primary_referenced_output_assets, source_map::GenerateSourceMap, - virtual_source::VirtualSource, + virtual_output::VirtualOutputAsset, }; use self::{bootstrap::NodeJsBootstrapAsset, pool::NodeJsPool, source_map::StructuredError}; @@ -40,7 +42,7 @@ pub mod transforms; #[turbo_tasks::function] async fn emit( - intermediate_asset: Vc>, + intermediate_asset: Vc>, intermediate_output_path: Vc, ) -> Result> { Ok(Vc::::cell( @@ -59,8 +61,8 @@ async fn emit( #[derive(Debug)] #[turbo_tasks::value] struct SeparatedAssets { - internal_assets: Vc, - external_asset_entrypoints: Vc, + internal_assets: Vc, + external_asset_entrypoints: Vc, } /// Extracts the subgraph of "internal" assets (assets within the passes @@ -68,9 +70,9 @@ struct SeparatedAssets { /// "internal" subgraph. #[turbo_tasks::function] async fn internal_assets( - intermediate_asset: Vc>, + intermediate_asset: Vc>, intermediate_output_path: Vc, -) -> Result> { +) -> Result> { Ok( separate_assets(intermediate_asset, intermediate_output_path) .strongly_consistent() @@ -86,7 +88,7 @@ pub struct AssetsForSourceMapping(HashMap> /// the [GenerateSourceMap] trait. #[turbo_tasks::function] async fn internal_assets_for_source_mapping( - intermediate_asset: Vc>, + intermediate_asset: Vc>, intermediate_output_path: Vc, ) -> Result> { let internal_assets = internal_assets(intermediate_asset, intermediate_output_path).await?; @@ -113,7 +115,7 @@ pub async fn external_asset_entrypoints( runtime_entries: Vc, chunking_context: Vc>, intermediate_output_path: Vc, -) -> Result> { +) -> Result> { Ok(separate_assets( get_intermediate_asset(chunking_context, module, runtime_entries) .resolve() @@ -129,20 +131,20 @@ pub async fn external_asset_entrypoints( /// assets. #[turbo_tasks::function] async fn separate_assets( - intermediate_asset: Vc>, + intermediate_asset: Vc>, intermediate_output_path: Vc, ) -> Result> { let intermediate_output_path = &*intermediate_output_path.await?; #[derive(PartialEq, Eq, Hash, Clone, Copy)] enum Type { - Internal(Vc>), - External(Vc>), + Internal(Vc>), + External(Vc>), } let get_asset_children = |asset| async move { let Type::Internal(asset) = asset else { return Ok(Vec::new()); }; - primary_referenced_assets(asset) + primary_referenced_output_assets(asset) .await? .iter() .map(|asset| async { @@ -198,7 +200,7 @@ async fn separate_assets( /// ESM, for example. fn emit_package_json(dir: Vc) -> Vc { emit( - Vc::upcast(VirtualSource::new( + Vc::upcast(VirtualOutputAsset::new( dir.join("package.json".to_string()), AssetContent::file(File::from("{\"type\": \"commonjs\"}").into()), )), @@ -211,7 +213,7 @@ fn emit_package_json(dir: Vc) -> Vc { pub async fn get_renderer_pool( cwd: Vc, env: Vc>, - intermediate_asset: Vc>, + intermediate_asset: Vc>, intermediate_output_path: Vc, output_root: Vc, project_dir: Vc, @@ -262,7 +264,7 @@ pub async fn get_intermediate_asset( chunking_context: Vc>, main_entry: Vc>, other_entries: Vc, -) -> Result>> { +) -> Result>> { Ok(Vc::upcast( NodeJsBootstrapAsset { path: chunking_context.chunk_path(main_entry.ident(), ".js".to_string()), diff --git a/crates/turbopack-node/src/render/node_api_source.rs b/crates/turbopack-node/src/render/node_api_source.rs index e6a9bf6aca2e4..f0ce7093ceb81 100644 --- a/crates/turbopack-node/src/render/node_api_source.rs +++ b/crates/turbopack-node/src/render/node_api_source.rs @@ -191,11 +191,11 @@ impl Introspectable for NodeApiContentSource { )); set.insert(( Vc::cell("intermediate asset".to_string()), - IntrospectableAsset::new(get_intermediate_asset( + IntrospectableAsset::new(Vc::upcast(get_intermediate_asset( entry.chunking_context, entry.module, entry.runtime_entries, - )), + ))), )); } Ok(Vc::cell(set)) diff --git a/crates/turbopack-node/src/render/render_proxy.rs b/crates/turbopack-node/src/render/render_proxy.rs index 07d0e90577020..fc438e933b207 100644 --- a/crates/turbopack-node/src/render/render_proxy.rs +++ b/crates/turbopack-node/src/render/render_proxy.rs @@ -12,10 +12,10 @@ use turbo_tasks_bytes::{Bytes, Stream}; use turbo_tasks_env::ProcessEnv; use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ - asset::Asset, chunk::{ChunkingContext, EvaluatableAsset, EvaluatableAssets}, error::PrettyPrintError, issue::IssueExt, + module::Module, }; use turbopack_dev_server::source::{Body, ProxyResult}; diff --git a/crates/turbopack-node/src/render/render_static.rs b/crates/turbopack-node/src/render/render_static.rs index 3587619ccacd1..e6d3f27b99fb0 100644 --- a/crates/turbopack-node/src/render/render_static.rs +++ b/crates/turbopack-node/src/render/render_static.rs @@ -16,6 +16,7 @@ use turbopack_core::{ chunk::{ChunkingContext, EvaluatableAsset, EvaluatableAssets}, error::PrettyPrintError, issue::IssueExt, + module::Module, }; use turbopack_dev_server::{ html::DevHtmlAsset, diff --git a/crates/turbopack-node/src/render/rendered_source.rs b/crates/turbopack-node/src/render/rendered_source.rs index eee4bc01a5929..a0be20b2612ca 100644 --- a/crates/turbopack-node/src/render/rendered_source.rs +++ b/crates/turbopack-node/src/render/rendered_source.rs @@ -1,13 +1,15 @@ use anyhow::{anyhow, Result}; use indexmap::IndexSet; use serde_json::Value as JsonValue; -use turbo_tasks::{Value, Vc}; +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, + output::OutputAsset, reference::AssetReference, resolve::PrimaryResolveResult, version::VersionedContentExt, @@ -123,7 +125,14 @@ impl GetContentSource for NodeRenderContentSource { } else { None } - }), + }) + .map(|&asset| async move { + Ok(Vc::try_resolve_downcast::>(asset).await?) + }) + .try_join() + .await? + .into_iter() + .flatten(), ) } for &entry in entries.await?.iter() { @@ -291,11 +300,11 @@ impl Introspectable for NodeRenderContentSource { )); set.insert(( Vc::cell("intermediate asset".to_string()), - IntrospectableAsset::new(get_intermediate_asset( + IntrospectableAsset::new(Vc::upcast(get_intermediate_asset( entry.chunking_context, entry.module, entry.runtime_entries, - )), + ))), )); } Ok(Vc::cell(set)) diff --git a/crates/turbopack-node/src/source_map/mod.rs b/crates/turbopack-node/src/source_map/mod.rs index 7241fe3259c3d..388186eedb433 100644 --- a/crates/turbopack-node/src/source_map/mod.rs +++ b/crates/turbopack-node/src/source_map/mod.rs @@ -17,7 +17,8 @@ use turbo_tasks_fs::{ }; use turbopack_cli_utils::source_context::format_source_context_lines; use turbopack_core::{ - asset::Asset, source_map::GenerateSourceMap, PROJECT_FILESYSTEM_NAME, SOURCE_MAP_ROOT_NAME, + output::OutputAsset, source_map::GenerateSourceMap, PROJECT_FILESYSTEM_NAME, + SOURCE_MAP_ROOT_NAME, }; use turbopack_ecmascript::magic_identifier::unmangle_identifiers; @@ -312,15 +313,30 @@ impl StructuredError { } } -#[instrument(level = Level::TRACE, skip_all)] pub async fn trace_stack( error: StructuredError, - root_asset: Vc>, + root_asset: Vc>, output_path: Vc, project_dir: Vc, ) -> Result { let assets_for_source_mapping = internal_assets_for_source_mapping(root_asset, output_path); + trace_stack_with_source_mapping_assets( + error, + assets_for_source_mapping, + output_path, + project_dir, + ) + .await +} + +#[instrument(level = Level::TRACE, skip_all)] +pub async fn trace_stack_with_source_mapping_assets( + error: StructuredError, + assets_for_source_mapping: Vc, + output_path: Vc, + project_dir: Vc, +) -> Result { error .print( assets_for_source_mapping, diff --git a/crates/turbopack-node/src/transforms/postcss.rs b/crates/turbopack-node/src/transforms/postcss.rs index 04e9a150afdfa..82aedac95381a 100644 --- a/crates/turbopack-node/src/transforms/postcss.rs +++ b/crates/turbopack-node/src/transforms/postcss.rs @@ -109,15 +109,15 @@ struct PostCssTransformedAsset { } #[turbo_tasks::value_impl] -impl Source for PostCssTransformedAsset {} - -#[turbo_tasks::value_impl] -impl Asset for PostCssTransformedAsset { +impl Source for PostCssTransformedAsset { #[turbo_tasks::function] fn ident(&self) -> Vc { self.source.ident() } +} +#[turbo_tasks::value_impl] +impl Asset for PostCssTransformedAsset { #[turbo_tasks::function] async fn content(self: Vc) -> Result> { let this = self.await?; @@ -170,10 +170,10 @@ fn postcss_executor( context: Vc>, postcss_config_path: Vc, ) -> Vc> { - let config_asset = Vc::upcast(context.process( + let config_asset = context.process( Vc::upcast(FileSource::new(postcss_config_path)), Value::new(ReferenceType::Entry(EntryReferenceSubType::Undefined)), - )); + ); context.process( Vc::upcast(VirtualSource::new( @@ -228,7 +228,7 @@ impl PostCssTransformedAsset { let css_path = css_fs_path.path.as_str(); let config_value = evaluate( - Vc::upcast(postcss_executor), + postcss_executor, project_path, env, this.source.ident(), diff --git a/crates/turbopack-node/src/transforms/webpack.rs b/crates/turbopack-node/src/transforms/webpack.rs index b939143c37763..b32a186765962 100644 --- a/crates/turbopack-node/src/transforms/webpack.rs +++ b/crates/turbopack-node/src/transforms/webpack.rs @@ -91,10 +91,7 @@ struct WebpackLoadersProcessedAsset { } #[turbo_tasks::value_impl] -impl Source for WebpackLoadersProcessedAsset {} - -#[turbo_tasks::value_impl] -impl Asset for WebpackLoadersProcessedAsset { +impl Source for WebpackLoadersProcessedAsset { #[turbo_tasks::function] async fn ident(&self) -> Result> { Ok( @@ -105,7 +102,10 @@ impl Asset for WebpackLoadersProcessedAsset { }, ) } +} +#[turbo_tasks::value_impl] +impl Asset for WebpackLoadersProcessedAsset { #[turbo_tasks::function] async fn content(self: Vc) -> Result> { Ok(self.process().await?.content) @@ -159,7 +159,7 @@ impl WebpackLoadersProcessedAsset { let resource_path = resource_fs_path.path.as_str(); let loaders = transform.loaders.await?; let config_value = evaluate( - Vc::upcast(webpack_loaders_executor), + webpack_loaders_executor, project_path, env, this.source.ident(), diff --git a/crates/turbopack-static/src/fixed.rs b/crates/turbopack-static/src/fixed.rs index 093b555033872..b5c2831e1ba9f 100644 --- a/crates/turbopack-static/src/fixed.rs +++ b/crates/turbopack-static/src/fixed.rs @@ -29,15 +29,15 @@ impl FixedStaticAsset { } #[turbo_tasks::value_impl] -impl OutputAsset for FixedStaticAsset {} - -#[turbo_tasks::value_impl] -impl Asset for FixedStaticAsset { +impl OutputAsset for FixedStaticAsset { #[turbo_tasks::function] async fn ident(&self) -> Result> { Ok(AssetIdent::from_path(self.output_path)) } +} +#[turbo_tasks::value_impl] +impl Asset for FixedStaticAsset { #[turbo_tasks::function] fn content(&self) -> Vc { self.source.content() diff --git a/crates/turbopack-static/src/lib.rs b/crates/turbopack-static/src/lib.rs index 7d474354c9d86..87123660c9a58 100644 --- a/crates/turbopack-static/src/lib.rs +++ b/crates/turbopack-static/src/lib.rs @@ -70,21 +70,21 @@ impl StaticModuleAsset { } #[turbo_tasks::value_impl] -impl Asset for StaticModuleAsset { +impl Module for StaticModuleAsset { #[turbo_tasks::function] fn ident(&self) -> Vc { self.source.ident().with_modifier(modifier()) } +} +#[turbo_tasks::value_impl] +impl Asset for StaticModuleAsset { #[turbo_tasks::function] fn content(&self) -> Vc { self.source.content() } } -#[turbo_tasks::value_impl] -impl Module for StaticModuleAsset {} - #[turbo_tasks::value_impl] impl ChunkableModule for StaticModuleAsset { #[turbo_tasks::function] @@ -141,10 +141,7 @@ struct StaticAsset { } #[turbo_tasks::value_impl] -impl OutputAsset for StaticAsset {} - -#[turbo_tasks::value_impl] -impl Asset for StaticAsset { +impl OutputAsset for StaticAsset { #[turbo_tasks::function] async fn ident(&self) -> Result> { let content = self.source.content(); @@ -163,7 +160,10 @@ impl Asset for StaticAsset { .asset_path(content_hash_b16, self.source.ident()); Ok(AssetIdent::from_path(asset_path)) } +} +#[turbo_tasks::value_impl] +impl Asset for StaticAsset { #[turbo_tasks::function] fn content(&self) -> Vc { self.source.content() @@ -239,7 +239,7 @@ impl CssEmbed for StaticCssEmbed { } #[turbo_tasks::function] - fn embeddable_asset(&self) -> Vc> { + fn embeddable_asset(&self) -> Vc> { Vc::upcast(self.static_asset) } } diff --git a/crates/turbopack-swc-utils/src/emitter.rs b/crates/turbopack-swc-utils/src/emitter.rs index f2a2bde04c9eb..938acef44ce7d 100644 --- a/crates/turbopack-swc-utils/src/emitter.rs +++ b/crates/turbopack-swc-utils/src/emitter.rs @@ -7,7 +7,6 @@ use swc_core::common::{ }; use turbo_tasks::Vc; use turbopack_core::{ - asset::Asset, issue::{analyze::AnalyzeIssue, IssueExt, IssueSeverity, IssueSource}, source::Source, }; @@ -43,7 +42,7 @@ impl Emitter for IssueEmitter { let source = db.span.primary_span().map(|span| { IssueSource::from_byte_offset( - Vc::upcast(self.source), + self.source, self.source_map.lookup_byte_offset(span.lo()).pos.to_usize(), self.source_map.lookup_byte_offset(span.lo()).pos.to_usize(), ) diff --git a/crates/turbopack-test-utils/src/snapshot.rs b/crates/turbopack-test-utils/src/snapshot.rs index 8e22967066caa..3875db9015560 100644 --- a/crates/turbopack-test-utils/src/snapshot.rs +++ b/crates/turbopack-test-utils/src/snapshot.rs @@ -49,7 +49,7 @@ pub async fn snapshot_issues< } // Annoyingly, the PlainIssue.source -> PlainIssueSource.asset -> - // PlainAsset.path -> FileSystemPath.fs -> DiskFileSystem.root changes + // PlainSource.path -> FileSystemPath.fs -> DiskFileSystem.root changes // for everyone. let content = debug_string .as_str() diff --git a/crates/turbopack-tests/tests/execution.rs b/crates/turbopack-tests/tests/execution.rs index 1e6e8737fedc8..9fb37ccfc7ab6 100644 --- a/crates/turbopack-tests/tests/execution.rs +++ b/crates/turbopack-tests/tests/execution.rs @@ -23,7 +23,6 @@ use turbopack::{ resolve_options_context::ResolveOptionsContext, ModuleAssetContext, }; use turbopack_core::{ - asset::Asset, chunk::{EvaluatableAssetExt, EvaluatableAssets}, compile_time_defines, compile_time_info::CompileTimeInfo, @@ -33,6 +32,7 @@ use turbopack_core::{ issue::{Issue, IssueContextExt}, module::Module, reference_type::{EntryReferenceSubType, ReferenceType}, + source::Source, }; use turbopack_dev::DevChunkingContext; use turbopack_node::evaluate::evaluate; @@ -251,7 +251,7 @@ async fn run_test(resource: String) -> Result> { let test_asset = FileSource::new(test_path); let res = evaluate( - Vc::upcast(jest_entry_asset), + jest_entry_asset, chunk_root_path, Vc::upcast(CommandLineProcessEnv::new()), test_asset.ident(), diff --git a/crates/turbopack-tests/tests/execution/turbopack/basic/comptime/issues/Error resolving commonjs request-ee63e3.txt b/crates/turbopack-tests/tests/execution/turbopack/basic/comptime/issues/Error resolving commonjs request-ee63e3.txt index c90f929c5bc53..aa7d4526b8dc7 100644 --- a/crates/turbopack-tests/tests/execution/turbopack/basic/comptime/issues/Error resolving commonjs request-ee63e3.txt +++ b/crates/turbopack-tests/tests/execution/turbopack/basic/comptime/issues/Error resolving commonjs request-ee63e3.txt @@ -8,7 +8,7 @@ PlainIssue { documentation_link: "", source: Some( PlainIssueSource { - asset: PlainAsset { + asset: PlainSource { ident: "[project]/crates/turbopack-tests/tests/execution/turbopack/basic/comptime/input/index.js", }, start: SourcePos { diff --git a/crates/turbopack-tests/tests/snapshot.rs b/crates/turbopack-tests/tests/snapshot.rs index 3bed7be4b035e..dce09b346c44e 100644 --- a/crates/turbopack-tests/tests/snapshot.rs +++ b/crates/turbopack-tests/tests/snapshot.rs @@ -37,6 +37,7 @@ use turbopack_core::{ environment::{BrowserEnvironment, Environment, ExecutionEnvironment, NodeJsEnvironment}, file_source::FileSource, issue::{Issue, IssueContextExt}, + module::Module, output::OutputAsset, reference::all_referenced_assets, reference_type::{EntryReferenceSubType, ReferenceType}, @@ -413,7 +414,7 @@ async fn walk_asset( .iter() .copied() .map(|asset| async move { - Ok(Vc::try_resolve_sidecast::>(asset).await?) + Ok(Vc::try_resolve_downcast::>(asset).await?) }) .try_join() .await? diff --git a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/issues/Error resolving commonjs request-b97684.txt b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/issues/Error resolving commonjs request-b97684.txt index ce47151891645..968f52b5d3a18 100644 --- a/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/issues/Error resolving commonjs request-b97684.txt +++ b/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/issues/Error resolving commonjs request-b97684.txt @@ -8,7 +8,7 @@ PlainIssue { documentation_link: "", source: Some( PlainIssueSource { - asset: PlainAsset { + asset: PlainSource { ident: "[project]/crates/turbopack-tests/tests/snapshot/imports/resolve_error_cjs/input/index.js", }, start: SourcePos { diff --git a/crates/turbopack/examples/turbopack.rs b/crates/turbopack/examples/turbopack.rs index bfe663ac240b4..152ab13995d1b 100644 --- a/crates/turbopack/examples/turbopack.rs +++ b/crates/turbopack/examples/turbopack.rs @@ -69,7 +69,7 @@ async fn main() -> Result<()> { Vc::upcast(source), Value::new(turbopack_core::reference_type::ReferenceType::Undefined), ); - let rebased = RebasedAsset::new(Vc::upcast(module), input, output); + let rebased = RebasedAsset::new(module, input, output); emit_with_completion(Vc::upcast(rebased), output).await?; Ok(unit().node) diff --git a/crates/turbopack/src/graph/mod.rs b/crates/turbopack/src/graph/mod.rs index 378186151bc14..e8e24dcda948f 100644 --- a/crates/turbopack/src/graph/mod.rs +++ b/crates/turbopack/src/graph/mod.rs @@ -2,11 +2,11 @@ use std::collections::HashSet; use anyhow::Result; use turbo_tasks::Vc; -use turbopack_core::{asset::Asset, reference::all_referenced_assets}; +use turbopack_core::{output::OutputAsset, reference::all_referenced_output_assets}; #[turbo_tasks::value(shared)] pub enum AggregatedGraph { - Leaf(Vc>), + Leaf(Vc>), Node { depth: usize, content: HashSet>, @@ -17,7 +17,7 @@ pub enum AggregatedGraph { #[turbo_tasks::value_impl] impl AggregatedGraph { #[turbo_tasks::function] - fn leaf(asset: Vc>) -> Vc { + fn leaf(asset: Vc>) -> Vc { Self::cell(AggregatedGraph::Leaf(asset)) } } @@ -48,7 +48,7 @@ impl AggregatedGraph { Ok(match *self.await? { AggregatedGraph::Leaf(asset) => { let mut refs = HashSet::new(); - for reference in all_referenced_assets(asset).await?.iter() { + for reference in all_referenced_output_assets(asset).await?.iter() { let reference = reference.resolve().await?; if asset != reference { refs.insert(AggregatedGraph::leaf(reference)); @@ -75,7 +75,7 @@ impl AggregatedGraph { async fn cost(self: Vc) -> Result> { Ok(match *self.await? { AggregatedGraph::Leaf(asset) => { - AggregationCost(all_referenced_assets(asset).await?.len()).into() + AggregationCost(all_referenced_output_assets(asset).await?.len()).into() } AggregatedGraph::Node { ref references, .. } => { AggregationCost(references.len()).into() @@ -116,7 +116,7 @@ impl AggregatedGraph { } #[turbo_tasks::function] -pub async fn aggregate(asset: Vc>) -> Result> { +pub async fn aggregate(asset: Vc>) -> Result> { let mut current = AggregatedGraph::leaf(asset); loop { if current.references().await?.set.is_empty() { @@ -189,7 +189,7 @@ struct AggregatedGraphsSet { #[turbo_tasks::value(shared)] pub enum AggregatedGraphNodeContent { - Asset(Vc>), + Asset(Vc>), Children(HashSet>), } diff --git a/crates/turbopack/src/lib.rs b/crates/turbopack/src/lib.rs index 3400526bb417d..2a8122104863f 100644 --- a/crates/turbopack/src/lib.rs +++ b/crates/turbopack/src/lib.rs @@ -41,8 +41,9 @@ use turbopack_core::{ ident::AssetIdent, issue::{Issue, IssueExt}, module::Module, + output::OutputAsset, raw_module::RawModule, - reference::all_referenced_assets, + reference::all_referenced_output_assets, reference_type::{EcmaScriptModulesReferenceSubType, InnerAssets, ReferenceType}, resolve::{ options::ResolveOptions, origin::PlainResolveOrigin, parse::Request, resolve, ModulePart, @@ -503,7 +504,7 @@ impl AssetContext for ModuleAssetContext { #[turbo_tasks::function] pub async fn emit_with_completion( - asset: Vc>, + asset: Vc>, output_dir: Vc, ) -> Vc { emit_assets_aggregated(asset, output_dir) @@ -511,7 +512,7 @@ pub async fn emit_with_completion( #[turbo_tasks::function] async fn emit_assets_aggregated( - asset: Vc>, + asset: Vc>, output_dir: Vc, ) -> Vc { let aggregated = aggregate(asset); @@ -535,13 +536,13 @@ async fn emit_aggregated_assets( } #[turbo_tasks::function] -pub async fn emit_asset(asset: Vc>) -> Vc { +pub async fn emit_asset(asset: Vc>) -> Vc { asset.content().write(asset.ident().path()) } #[turbo_tasks::function] pub async fn emit_asset_into_dir( - asset: Vc>, + asset: Vc>, output_dir: Vc, ) -> Result> { let dir = &*output_dir.await?; @@ -552,25 +553,25 @@ pub async fn emit_asset_into_dir( }) } -type AssetSet = HashSet>>; +type OutputAssetSet = HashSet>>; #[turbo_tasks::value(shared)] struct ReferencesList { - referenced_by: HashMap>, AssetSet>, + referenced_by: HashMap>, OutputAssetSet>, } #[turbo_tasks::function] async fn compute_back_references(aggregated: Vc) -> Result> { Ok(match &*aggregated.content().await? { - AggregatedGraphNodeContent::Asset(asset) => { + &AggregatedGraphNodeContent::Asset(asset) => { let mut referenced_by = HashMap::new(); - for reference in all_referenced_assets(*asset).await?.iter() { - referenced_by.insert(*reference, [*asset].into_iter().collect()); + for reference in all_referenced_output_assets(asset).await?.iter() { + referenced_by.insert(*reference, [asset].into_iter().collect()); } ReferencesList { referenced_by }.into() } AggregatedGraphNodeContent::Children(children) => { - let mut referenced_by = HashMap::>, AssetSet>::new(); + let mut referenced_by = HashMap::>, OutputAssetSet>::new(); let lists = children .iter() .map(|child| compute_back_references(*child)) @@ -595,7 +596,10 @@ async fn compute_back_references(aggregated: Vc) -> Result) -> Result> { let list = list.await?; const N: usize = 5; - let mut top = Vec::<(&Vc>, &HashSet>>)>::new(); + let mut top = Vec::<( + &Vc>, + &HashSet>>, + )>::new(); for tuple in list.referenced_by.iter() { let mut current = tuple; for item in &mut top { diff --git a/crates/turbopack/src/rebase/mod.rs b/crates/turbopack/src/rebase/mod.rs index 7a097baeb613c..1cf960eaf4b21 100644 --- a/crates/turbopack/src/rebase/mod.rs +++ b/crates/turbopack/src/rebase/mod.rs @@ -6,15 +6,18 @@ use turbo_tasks_fs::FileSystemPath; use turbopack_core::{ asset::{Asset, AssetContent}, ident::AssetIdent, + module::{convert_asset_to_module, Module}, output::OutputAsset, reference::{AssetReference, AssetReferences}, resolve::ResolveResult, }; +/// Converts a [Module] graph into an [OutputAsset] graph by placing it into a +/// different directory. #[turbo_tasks::value] #[derive(Hash)] pub struct RebasedAsset { - source: Vc>, + source: Vc>, input_dir: Vc, output_dir: Vc, } @@ -23,7 +26,7 @@ pub struct RebasedAsset { impl RebasedAsset { #[turbo_tasks::function] pub fn new( - source: Vc>, + source: Vc>, input_dir: Vc, output_dir: Vc, ) -> Vc { @@ -36,10 +39,7 @@ impl RebasedAsset { } #[turbo_tasks::value_impl] -impl OutputAsset for RebasedAsset {} - -#[turbo_tasks::value_impl] -impl Asset for RebasedAsset { +impl OutputAsset for RebasedAsset { #[turbo_tasks::function] fn ident(&self) -> Vc { AssetIdent::from_path(FileSystemPath::rebase( @@ -48,7 +48,10 @@ impl Asset for RebasedAsset { self.output_dir, )) } +} +#[turbo_tasks::value_impl] +impl Asset for RebasedAsset { #[turbo_tasks::function] fn content(&self) -> Vc { self.source.content() @@ -87,8 +90,9 @@ impl AssetReference for RebasedAssetReference { Ok(result .map( |asset| { + let module = convert_asset_to_module(asset); let asset = - Vc::upcast(RebasedAsset::new(asset, self.input_dir, self.output_dir)); + Vc::upcast(RebasedAsset::new(module, self.input_dir, self.output_dir)); async move { Ok(asset) } }, |reference| { diff --git a/crates/turbopack/tests/node-file-trace.rs b/crates/turbopack/tests/node-file-trace.rs index 24b941081e2cb..78eb09415aa7f 100644 --- a/crates/turbopack/tests/node-file-trace.rs +++ b/crates/turbopack/tests/node-file-trace.rs @@ -36,13 +36,12 @@ use turbopack::{ emit_with_completion, module_options::ModuleOptionsContext, rebase::RebasedAsset, register, resolve_options_context::ResolveOptionsContext, ModuleAssetContext, }; -#[cfg(not(feature = "bench_against_node_nft"))] -use turbopack_core::asset::Asset; use turbopack_core::{ compile_time_info::CompileTimeInfo, context::AssetContext, environment::{Environment, ExecutionEnvironment, NodeJsEnvironment}, file_source::FileSource, + output::OutputAsset, reference_type::ReferenceType, };