| use std::collections::{BTreeSet, VecDeque}; |
|
|
| use anyhow::{Result, bail}; |
| use serde_json::json; |
| use turbo_rcstr::RcStr; |
| use turbo_tasks::{ |
| ReadRef, ResolvedVc, TryFlatJoinIterExt, Vc, |
| graph::{AdjacencyMap, GraphTraversal}, |
| }; |
| use turbo_tasks_fs::{DirectoryEntry, File, FileSystem, FileSystemPath, glob::Glob}; |
| use turbopack_core::{ |
| asset::{Asset, AssetContent}, |
| output::{OutputAsset, OutputAssets}, |
| }; |
|
|
| use crate::project::Project; |
|
|
| |
| |
| |
| |
| |
| |
| #[turbo_tasks::value] |
| pub struct NftJsonAsset { |
| project: ResolvedVc<Project>, |
| |
| chunk: ResolvedVc<Box<dyn OutputAsset>>, |
| |
| |
| |
| |
| |
| additional_assets: Vec<ResolvedVc<Box<dyn OutputAsset>>>, |
| page_name: Option<RcStr>, |
| } |
|
|
| #[turbo_tasks::value_impl] |
| impl NftJsonAsset { |
| #[turbo_tasks::function] |
| pub fn new( |
| project: ResolvedVc<Project>, |
| page_name: Option<RcStr>, |
| chunk: ResolvedVc<Box<dyn OutputAsset>>, |
| additional_assets: Vec<ResolvedVc<Box<dyn OutputAsset>>>, |
| ) -> Vc<Self> { |
| NftJsonAsset { |
| chunk, |
| project, |
| additional_assets, |
| page_name, |
| } |
| .cell() |
| } |
| } |
|
|
| #[turbo_tasks::value_impl] |
| impl OutputAsset for NftJsonAsset { |
| #[turbo_tasks::function] |
| async fn path(&self) -> Result<Vc<FileSystemPath>> { |
| let path = self.chunk.path().await?; |
| Ok(path |
| .fs |
| .root() |
| .await? |
| .join(&format!("{}.nft.json", path.path))? |
| .cell()) |
| } |
| } |
|
|
| #[turbo_tasks::value(transparent)] |
| pub struct OutputSpecifier(Option<RcStr>); |
|
|
| fn get_output_specifier( |
| path_ref: &FileSystemPath, |
| ident_folder: &FileSystemPath, |
| ident_folder_in_project_fs: &FileSystemPath, |
| output_root: &FileSystemPath, |
| project_root: &FileSystemPath, |
| ) -> Result<Option<RcStr>> { |
| |
| if path_ref.is_inside_ref(output_root) { |
| return Ok(Some(ident_folder.get_relative_path_to(path_ref).unwrap())); |
| } |
|
|
| |
| if path_ref.is_inside_ref(project_root) { |
| return Ok(Some( |
| ident_folder_in_project_fs |
| .get_relative_path_to(path_ref) |
| .unwrap(), |
| )); |
| } |
|
|
| |
| bail!("NftJsonAsset: cannot handle filepath {path_ref}"); |
| } |
|
|
| |
| async fn apply_includes( |
| project_root_path: FileSystemPath, |
| glob: Vc<Glob>, |
| ident_folder: &FileSystemPath, |
| ) -> Result<BTreeSet<RcStr>> { |
| |
| let glob_result = project_root_path.read_glob(glob).await?; |
|
|
| |
| let mut result = BTreeSet::new(); |
| let mut stack = VecDeque::new(); |
| stack.push_back(glob_result); |
| while let Some(glob_result) = stack.pop_back() { |
| |
| for entry in glob_result.results.values() { |
| let DirectoryEntry::File(file_path) = entry else { |
| continue; |
| }; |
|
|
| let file_path_ref = file_path; |
| |
| if let Some(relative_path) = ident_folder.get_relative_path_to(file_path_ref) { |
| result.insert(relative_path); |
| } |
| } |
|
|
| for nested_result in glob_result.inner.values() { |
| let nested_result_ref = nested_result.await?; |
| stack.push_back(nested_result_ref); |
| } |
| } |
| Ok(result) |
| } |
|
|
| #[turbo_tasks::value_impl] |
| impl Asset for NftJsonAsset { |
| #[turbo_tasks::function] |
| async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> { |
| let this = &*self.await?; |
| let mut result: BTreeSet<RcStr> = BTreeSet::new(); |
|
|
| let output_root_ref = this.project.output_fs().root().await?; |
| let project_root_ref = this.project.project_fs().root().await?; |
| let next_config = this.project.next_config(); |
|
|
| let output_file_tracing_includes = &*next_config.output_file_tracing_includes().await?; |
| let output_file_tracing_excludes = &*next_config.output_file_tracing_excludes().await?; |
|
|
| let client_root = this.project.client_fs().root(); |
| let client_root = client_root.owned().await?; |
|
|
| |
| let project_root_path = this.project.project_root_path().owned().await?; |
| |
| let ident_folder = self.path().await?.parent(); |
| |
| let ident_folder_in_project_fs = project_root_path.join(&ident_folder.path)?; |
|
|
| let chunk = this.chunk; |
| let entries = this |
| .additional_assets |
| .iter() |
| .copied() |
| .chain(std::iter::once(chunk)) |
| .collect(); |
|
|
| let exclude_glob = if let Some(route) = &this.page_name { |
| let project_path = this.project.project_path().await?; |
|
|
| if let Some(excludes_config) = output_file_tracing_excludes { |
| let mut combined_excludes = BTreeSet::new(); |
|
|
| if let Some(excludes_obj) = excludes_config.as_object() { |
| for (glob_pattern, exclude_patterns) in excludes_obj { |
| |
| let glob = Glob::new(RcStr::from(glob_pattern.clone())).await?; |
| if glob.matches(route) |
| && let Some(patterns) = exclude_patterns.as_array() |
| { |
| for pattern in patterns { |
| if let Some(pattern_str) = pattern.as_str() { |
| combined_excludes.insert(pattern_str); |
| } |
| } |
| } |
| } |
| } |
|
|
| let glob = Glob::new( |
| format!( |
| "{project_path}/{{{}}}", |
| combined_excludes |
| .iter() |
| .copied() |
| .collect::<Vec<_>>() |
| .join(",") |
| ) |
| .into(), |
| ); |
|
|
| Some(glob) |
| } else { |
| None |
| } |
| } else { |
| None |
| }; |
|
|
| |
| for referenced_chunk in |
| all_assets_from_entries_filtered(Vc::cell(entries), client_root, exclude_glob).await? |
| { |
| if chunk.eq(referenced_chunk) { |
| continue; |
| } |
|
|
| let referenced_chunk_path = referenced_chunk.path().await?; |
| if referenced_chunk_path.has_extension(".map") { |
| continue; |
| } |
|
|
| let Some(specifier) = get_output_specifier( |
| &referenced_chunk_path, |
| &ident_folder, |
| &ident_folder_in_project_fs, |
| &output_root_ref, |
| &project_root_ref, |
| )? |
| else { |
| continue; |
| }; |
| result.insert(specifier); |
| } |
|
|
| |
| |
| if let Some(route) = &this.page_name { |
| let project_path = this.project.project_path().owned().await?; |
| let mut combined_includes = BTreeSet::new(); |
|
|
| |
| if let Some(includes_config) = output_file_tracing_includes |
| && let Some(includes_obj) = includes_config.as_object() |
| { |
| for (glob_pattern, include_patterns) in includes_obj { |
| |
| let glob = Glob::new(glob_pattern.as_str().into()).await?; |
| if glob.matches(route) |
| && let Some(patterns) = include_patterns.as_array() |
| { |
| for pattern in patterns { |
| if let Some(pattern_str) = pattern.as_str() { |
| combined_includes.insert(pattern_str); |
| } |
| } |
| } |
| } |
| } |
|
|
| |
| if !combined_includes.is_empty() { |
| let glob = Glob::new( |
| format!( |
| "{{{}}}", |
| combined_includes |
| .iter() |
| .copied() |
| .collect::<Vec<_>>() |
| .join(",") |
| ) |
| .into(), |
| ); |
| let additional_files = |
| apply_includes(project_path, glob, &ident_folder_in_project_fs).await?; |
| result.extend(additional_files); |
| } |
| } |
|
|
| let json = json!({ |
| "version": 1, |
| "files": result |
| }); |
|
|
| Ok(AssetContent::file(File::from(json.to_string()).into())) |
| } |
| } |
|
|
| |
| |
| #[turbo_tasks::function] |
| async fn all_assets_from_entries_filtered( |
| entries: Vc<OutputAssets>, |
| client_root: FileSystemPath, |
| exclude_glob: Option<Vc<Glob>>, |
| ) -> Result<Vc<OutputAssets>> { |
| let exclude_glob = if let Some(exclude_glob) = exclude_glob { |
| Some(exclude_glob.await?) |
| } else { |
| None |
| }; |
| Ok(Vc::cell( |
| AdjacencyMap::new() |
| .skip_duplicates() |
| .visit( |
| entries.await?.iter().copied().map(ResolvedVc::upcast), |
| |asset| get_referenced_server_assets(asset, &client_root, &exclude_glob), |
| ) |
| .await |
| .completed()? |
| .into_inner() |
| .into_postorder_topological() |
| .collect(), |
| )) |
| } |
|
|
| |
| |
| async fn get_referenced_server_assets( |
| asset: ResolvedVc<Box<dyn OutputAsset>>, |
| client_root: &FileSystemPath, |
| exclude_glob: &Option<ReadRef<Glob>>, |
| ) -> Result<Vec<ResolvedVc<Box<dyn OutputAsset>>>> { |
| asset |
| .references() |
| .await? |
| .iter() |
| .map(async |asset| { |
| let asset_path = asset.path().await?; |
| if asset_path.is_inside_ref(client_root) { |
| return Ok(None); |
| } |
|
|
| if exclude_glob |
| .as_ref() |
| .is_some_and(|g| g.matches(&asset_path.path)) |
| { |
| return Ok(None); |
| } |
|
|
| Ok(Some(*asset)) |
| }) |
| .try_flat_join() |
| .await |
| } |
|
|