|
|
use anyhow::Result; |
|
|
use next_core::{all_assets_from_entries, next_manifests::AssetBinding}; |
|
|
use serde::{Deserialize, Serialize}; |
|
|
use tracing::Instrument; |
|
|
use turbo_rcstr::RcStr; |
|
|
use turbo_tasks::{ |
|
|
NonLocalValue, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc, trace::TraceRawVcs, |
|
|
}; |
|
|
use turbo_tasks_fs::FileSystemPath; |
|
|
use turbopack_core::{ |
|
|
asset::{Asset, AssetContent}, |
|
|
output::{OutputAsset, OutputAssets}, |
|
|
}; |
|
|
use turbopack_wasm::wasm_edge_var_name; |
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, TraceRawVcs, NonLocalValue)] |
|
|
pub struct ServerPath { |
|
|
|
|
|
pub path: String, |
|
|
pub content_hash: u64, |
|
|
} |
|
|
|
|
|
|
|
|
#[turbo_tasks::value(transparent)] |
|
|
pub struct ServerPaths(Vec<ServerPath>); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::function] |
|
|
pub async fn all_server_paths( |
|
|
assets: Vc<OutputAssets>, |
|
|
node_root: FileSystemPath, |
|
|
) -> Result<Vc<ServerPaths>> { |
|
|
let span = tracing::info_span!("all_server_paths"); |
|
|
async move { |
|
|
let all_assets = all_assets_from_entries(assets).await?; |
|
|
let node_root = node_root.clone(); |
|
|
Ok(Vc::cell( |
|
|
all_assets |
|
|
.iter() |
|
|
.map(|&asset| { |
|
|
let node_root = node_root.clone(); |
|
|
|
|
|
async move { |
|
|
Ok( |
|
|
if let Some(path) = node_root.get_path_to(&*asset.path().await?) { |
|
|
let content_hash = match *asset.content().await? { |
|
|
AssetContent::File(file) => *file.hash().await?, |
|
|
AssetContent::Redirect { .. } => 0, |
|
|
}; |
|
|
Some(ServerPath { |
|
|
path: path.to_string(), |
|
|
content_hash, |
|
|
}) |
|
|
} else { |
|
|
None |
|
|
}, |
|
|
) |
|
|
} |
|
|
}) |
|
|
.try_flat_join() |
|
|
.await?, |
|
|
)) |
|
|
} |
|
|
.instrument(span) |
|
|
.await |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[turbo_tasks::function] |
|
|
pub async fn all_paths_in_root( |
|
|
assets: Vc<OutputAssets>, |
|
|
root: FileSystemPath, |
|
|
) -> Result<Vc<Vec<RcStr>>> { |
|
|
let all_assets = &*all_assets_from_entries(assets).await?; |
|
|
|
|
|
Ok(Vc::cell( |
|
|
get_paths_from_root(&root, all_assets, |_| true).await?, |
|
|
)) |
|
|
} |
|
|
|
|
|
pub(crate) async fn get_paths_from_root( |
|
|
root: &FileSystemPath, |
|
|
output_assets: impl IntoIterator<Item = &ResolvedVc<Box<dyn OutputAsset>>>, |
|
|
filter: impl FnOnce(&str) -> bool + Copy, |
|
|
) -> Result<Vec<RcStr>> { |
|
|
output_assets |
|
|
.into_iter() |
|
|
.map(move |&file| async move { |
|
|
let path = &*file.path().await?; |
|
|
let Some(relative) = root.get_path_to(path) else { |
|
|
return Ok(None); |
|
|
}; |
|
|
|
|
|
Ok(if filter(relative) { |
|
|
Some(relative.into()) |
|
|
} else { |
|
|
None |
|
|
}) |
|
|
}) |
|
|
.try_flat_join() |
|
|
.await |
|
|
} |
|
|
|
|
|
pub(crate) async fn get_js_paths_from_root( |
|
|
root: &FileSystemPath, |
|
|
output_assets: impl IntoIterator<Item = &ResolvedVc<Box<dyn OutputAsset>>>, |
|
|
) -> Result<Vec<RcStr>> { |
|
|
get_paths_from_root(root, output_assets, |path| path.ends_with(".js")).await |
|
|
} |
|
|
|
|
|
pub(crate) async fn get_wasm_paths_from_root( |
|
|
root: &FileSystemPath, |
|
|
output_assets: impl IntoIterator<Item = &ResolvedVc<Box<dyn OutputAsset>>>, |
|
|
) -> Result<Vec<(RcStr, ResolvedVc<Box<dyn OutputAsset>>)>> { |
|
|
output_assets |
|
|
.into_iter() |
|
|
.map(move |&file| async move { |
|
|
let path = &*file.path().await?; |
|
|
let Some(relative) = root.get_path_to(path) else { |
|
|
return Ok(None); |
|
|
}; |
|
|
|
|
|
Ok(if relative.ends_with(".wasm") { |
|
|
Some((relative.into(), file)) |
|
|
} else { |
|
|
None |
|
|
}) |
|
|
}) |
|
|
.try_flat_join() |
|
|
.await |
|
|
} |
|
|
|
|
|
pub(crate) async fn get_asset_paths_from_root( |
|
|
root: &FileSystemPath, |
|
|
output_assets: impl IntoIterator<Item = &ResolvedVc<Box<dyn OutputAsset>>>, |
|
|
) -> Result<Vec<RcStr>> { |
|
|
get_paths_from_root(root, output_assets, |path| { |
|
|
!path.ends_with(".js") && !path.ends_with(".map") && !path.ends_with(".wasm") |
|
|
}) |
|
|
.await |
|
|
} |
|
|
|
|
|
pub(crate) async fn get_font_paths_from_root( |
|
|
root: &FileSystemPath, |
|
|
output_assets: impl IntoIterator<Item = &ResolvedVc<Box<dyn OutputAsset>>>, |
|
|
) -> Result<Vec<RcStr>> { |
|
|
get_paths_from_root(root, output_assets, |path| { |
|
|
path.ends_with(".woff") |
|
|
|| path.ends_with(".woff2") |
|
|
|| path.ends_with(".eot") |
|
|
|| path.ends_with(".ttf") |
|
|
|| path.ends_with(".otf") |
|
|
}) |
|
|
.await |
|
|
} |
|
|
|
|
|
pub(crate) async fn wasm_paths_to_bindings( |
|
|
paths: impl IntoIterator<Item = (RcStr, ResolvedVc<Box<dyn OutputAsset>>)>, |
|
|
) -> Result<Vec<AssetBinding>> { |
|
|
paths |
|
|
.into_iter() |
|
|
.map(async |(path, asset)| { |
|
|
Ok(AssetBinding { |
|
|
name: wasm_edge_var_name(Vc::upcast(*asset)).owned().await?, |
|
|
file_path: path, |
|
|
}) |
|
|
}) |
|
|
.try_join() |
|
|
.await |
|
|
} |
|
|
|
|
|
pub(crate) fn paths_to_bindings(paths: Vec<RcStr>) -> Vec<AssetBinding> { |
|
|
paths |
|
|
.into_iter() |
|
|
.map(|path| AssetBinding { |
|
|
name: path.clone(), |
|
|
file_path: path, |
|
|
}) |
|
|
.collect() |
|
|
} |
|
|
|