|
| 1 | +use anyhow::{bail, Result}; |
| 2 | +use serde_json::json; |
| 3 | +use turbo_tasks::{RcStr, ResolvedVc, ValueToString, Vc}; |
| 4 | +use turbo_tasks_fs::{DiskFileSystem, File, FileSystem, FileSystemPath, VirtualFileSystem}; |
| 5 | +use turbopack_core::{ |
| 6 | + asset::{Asset, AssetContent}, |
| 7 | + ident::AssetIdent, |
| 8 | + output::OutputAsset, |
| 9 | + reference::all_assets_from_entries, |
| 10 | +}; |
| 11 | + |
| 12 | +/// A json file that produces references to all files that are needed by the given module |
| 13 | +/// at runtime. This will include, for example, node native modules, unanalyzable packages, |
| 14 | +/// client side chunks, etc. |
| 15 | +/// |
| 16 | +/// With this file, users can determine the minimum set of files that are needed alongside |
| 17 | +/// their bundle. |
| 18 | +#[turbo_tasks::value(shared)] |
| 19 | +pub struct NftJsonAsset { |
| 20 | + /// The chunk for which the asset is being generated |
| 21 | + chunk: Vc<Box<dyn OutputAsset>>, |
| 22 | + output_fs: Vc<DiskFileSystem>, |
| 23 | + project_fs: Vc<DiskFileSystem>, |
| 24 | + client_fs: Vc<Box<dyn FileSystem>>, |
| 25 | + /// Additional assets to include in the nft json. This can be used to manually collect assets |
| 26 | + /// that are known to be required but are not in the graph yet, for whatever reason. |
| 27 | + /// |
| 28 | + /// An example of this is the two-phase approach used by the `ClientReferenceManifest` in |
| 29 | + /// next.js. |
| 30 | + additional_assets: Vec<ResolvedVc<Box<dyn OutputAsset>>>, |
| 31 | +} |
| 32 | + |
| 33 | +#[turbo_tasks::value_impl] |
| 34 | +impl NftJsonAsset { |
| 35 | + #[turbo_tasks::function] |
| 36 | + pub fn new( |
| 37 | + chunk: Vc<Box<dyn OutputAsset>>, |
| 38 | + output_fs: Vc<DiskFileSystem>, |
| 39 | + project_fs: Vc<DiskFileSystem>, |
| 40 | + client_fs: Vc<Box<dyn FileSystem>>, |
| 41 | + additional_assets: Vec<ResolvedVc<Box<dyn OutputAsset>>>, |
| 42 | + ) -> Vc<Self> { |
| 43 | + NftJsonAsset { |
| 44 | + chunk, |
| 45 | + output_fs, |
| 46 | + project_fs, |
| 47 | + client_fs, |
| 48 | + additional_assets, |
| 49 | + } |
| 50 | + .cell() |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[turbo_tasks::value(transparent)] |
| 55 | +pub struct OutputSpecifier(Option<RcStr>); |
| 56 | + |
| 57 | +#[turbo_tasks::value_impl] |
| 58 | +impl NftJsonAsset { |
| 59 | + #[turbo_tasks::function] |
| 60 | + async fn ident_in_project_fs(self: Vc<Self>) -> Result<Vc<FileSystemPath>> { |
| 61 | + let this = self.await?; |
| 62 | + let project_fs = this.project_fs.await?; |
| 63 | + let output_fs = this.output_fs.await?; |
| 64 | + let nft_folder = self.ident().path().parent().await?; |
| 65 | + |
| 66 | + if let Some(subdir) = output_fs.root.strip_prefix(&*project_fs.root) { |
| 67 | + Ok(this |
| 68 | + .project_fs |
| 69 | + .root() |
| 70 | + .join(subdir.into()) |
| 71 | + .join(nft_folder.path.clone())) |
| 72 | + } else { |
| 73 | + // TODO: what are the implications of this? |
| 74 | + bail!("output fs not inside project fs"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + #[turbo_tasks::function] |
| 79 | + async fn ident_in_client_fs(self: Vc<Self>) -> Result<Vc<FileSystemPath>> { |
| 80 | + Ok(self |
| 81 | + .await? |
| 82 | + .client_fs |
| 83 | + .root() |
| 84 | + .join(self.ident().path().parent().await?.path.clone())) |
| 85 | + } |
| 86 | + |
| 87 | + #[turbo_tasks::function] |
| 88 | + async fn get_output_specifier( |
| 89 | + self: Vc<Self>, |
| 90 | + path: Vc<FileSystemPath>, |
| 91 | + ) -> Result<Vc<OutputSpecifier>> { |
| 92 | + let this = self.await?; |
| 93 | + let path_fs = path.fs().resolve().await?; |
| 94 | + let path_ref = path.await?; |
| 95 | + let nft_folder = self.ident().path().parent().await?; |
| 96 | + |
| 97 | + if path_fs == Vc::upcast(this.output_fs.resolve().await?) { |
| 98 | + // e.g. a referenced chunk |
| 99 | + return Ok(Vc::cell(Some( |
| 100 | + nft_folder.get_relative_path_to(&path_ref).unwrap(), |
| 101 | + ))); |
| 102 | + } else if path_fs == Vc::upcast(this.project_fs.resolve().await?) { |
| 103 | + return Ok(Vc::cell(Some( |
| 104 | + self.ident_in_project_fs() |
| 105 | + .await? |
| 106 | + .get_relative_path_to(&path_ref) |
| 107 | + .unwrap(), |
| 108 | + ))); |
| 109 | + } else if path_fs == Vc::upcast(this.client_fs.resolve().await?) { |
| 110 | + return Ok(Vc::cell(Some( |
| 111 | + self.ident_in_client_fs() |
| 112 | + .await? |
| 113 | + .get_relative_path_to(&path_ref) |
| 114 | + .unwrap() |
| 115 | + .replace("/_next/", "/.next/") |
| 116 | + .into(), |
| 117 | + ))); |
| 118 | + } |
| 119 | + |
| 120 | + if let Some(path_fs) = Vc::try_resolve_downcast_type::<VirtualFileSystem>(path_fs).await? { |
| 121 | + if path_fs.await?.name == "externals" || path_fs.await?.name == "traced" { |
| 122 | + return Ok(Vc::cell(Some( |
| 123 | + self.ident_in_project_fs() |
| 124 | + .await? |
| 125 | + .get_relative_path_to( |
| 126 | + &*this.project_fs.root().join(path_ref.path.clone()).await?, |
| 127 | + ) |
| 128 | + .unwrap(), |
| 129 | + ))); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + println!("Unknown filesystem for {}", path.to_string().await?); |
| 134 | + Ok(Vc::cell(None)) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +#[turbo_tasks::value_impl] |
| 139 | +impl OutputAsset for NftJsonAsset { |
| 140 | + #[turbo_tasks::function] |
| 141 | + async fn ident(&self) -> Result<Vc<AssetIdent>> { |
| 142 | + let path = self.chunk.ident().path().await?; |
| 143 | + Ok(AssetIdent::from_path( |
| 144 | + path.fs |
| 145 | + .root() |
| 146 | + .join(format!("{}.nft.json", path.path).into()), |
| 147 | + )) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +#[turbo_tasks::value_impl] |
| 152 | +impl Asset for NftJsonAsset { |
| 153 | + #[turbo_tasks::function] |
| 154 | + async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> { |
| 155 | + let this = &*self.await?; |
| 156 | + let mut result = Vec::new(); |
| 157 | + |
| 158 | + let chunk = this.chunk.to_resolved().await?; |
| 159 | + let entries = this |
| 160 | + .additional_assets |
| 161 | + .iter() |
| 162 | + .copied() |
| 163 | + .chain(std::iter::once(chunk)) |
| 164 | + .collect(); |
| 165 | + for referenced_chunk in all_assets_from_entries(Vc::cell(entries)).await? { |
| 166 | + if referenced_chunk.ident().path().await?.extension_ref() == Some("map") { |
| 167 | + continue; |
| 168 | + } |
| 169 | + |
| 170 | + if chunk == referenced_chunk.to_resolved().await? { |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + let specifier = self |
| 175 | + .get_output_specifier(referenced_chunk.ident().path()) |
| 176 | + .await?; |
| 177 | + if let Some(specifier) = &*specifier { |
| 178 | + result.push(specifier.clone()); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + result.sort(); |
| 183 | + result.dedup(); |
| 184 | + let json = json!({ |
| 185 | + "version": 1, |
| 186 | + "files": result |
| 187 | + }); |
| 188 | + |
| 189 | + Ok(AssetContent::file(File::from(json.to_string()).into())) |
| 190 | + } |
| 191 | +} |
0 commit comments