File size: 4,298 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
use anyhow::Result;
use tracing::Instrument;
use turbo_tasks::{
FxIndexSet, ResolvedVc, TryFlatJoinIterExt, Vc,
graph::{AdjacencyMap, GraphTraversal},
};
use turbo_tasks_fs::{FileSystemPath, rebase};
use turbopack_core::{
asset::Asset,
output::{OutputAsset, OutputAssets},
};
/// Emits all assets transitively reachable from the given chunks, that are
/// inside the node root or the client root.
///
/// Assets inside the given client root are rebased to the given client output
/// path.
#[turbo_tasks::function]
pub async fn emit_all_assets(
assets: Vc<OutputAssets>,
node_root: FileSystemPath,
client_relative_path: FileSystemPath,
client_output_path: FileSystemPath,
) -> Result<()> {
emit_assets(
all_assets_from_entries(assets),
node_root,
client_relative_path,
client_output_path,
)
.as_side_effect()
.await?;
Ok(())
}
/// Emits all assets transitively reachable from the given chunks, that are
/// inside the node root or the client root.
///
/// Assets inside the given client root are rebased to the given client output
/// path.
#[turbo_tasks::function]
pub async fn emit_assets(
assets: Vc<OutputAssets>,
node_root: FileSystemPath,
client_relative_path: FileSystemPath,
client_output_path: FileSystemPath,
) -> Result<()> {
let _: Vec<()> = assets
.await?
.iter()
.copied()
.map(|asset| {
let node_root = node_root.clone();
let client_relative_path = client_relative_path.clone();
let client_output_path = client_output_path.clone();
async move {
let path = asset.path().owned().await?;
let span = tracing::info_span!("emit asset", name = %path.value_to_string().await?);
async move {
Ok(if path.is_inside_ref(&node_root) {
Some(emit(*asset).as_side_effect().await?)
} else if path.is_inside_ref(&client_relative_path) {
// Client assets are emitted to the client output path, which is prefixed
// with _next. We need to rebase them to remove that
// prefix.
Some(
emit_rebase(*asset, client_relative_path, client_output_path)
.as_side_effect()
.await?,
)
} else {
None
})
}
.instrument(span)
.await
}
})
.try_flat_join()
.await?;
Ok(())
}
#[turbo_tasks::function]
async fn emit(asset: Vc<Box<dyn OutputAsset>>) -> Result<()> {
asset
.content()
.resolve()
.await?
.write(asset.path().owned().await?)
.as_side_effect()
.await?;
Ok(())
}
#[turbo_tasks::function]
async fn emit_rebase(
asset: Vc<Box<dyn OutputAsset>>,
from: FileSystemPath,
to: FileSystemPath,
) -> Result<()> {
let path = rebase(asset.path().owned().await?, from, to)
.owned()
.await?;
let content = asset.content();
content
.resolve()
.await?
.write(path)
.as_side_effect()
.await?;
Ok(())
}
/// Walks the asset graph from multiple assets and collect all referenced
/// assets.
#[turbo_tasks::function]
pub async fn all_assets_from_entries(entries: Vc<OutputAssets>) -> Result<Vc<OutputAssets>> {
Ok(Vc::cell(
AdjacencyMap::new()
.skip_duplicates()
.visit(entries.await?.iter().copied(), get_referenced_assets)
.await
.completed()?
.into_inner()
.into_postorder_topological()
.collect::<FxIndexSet<_>>()
.into_iter()
.collect(),
))
}
/// Computes the list of all chunk children of a given chunk.
async fn get_referenced_assets(
asset: ResolvedVc<Box<dyn OutputAsset>>,
) -> Result<impl Iterator<Item = ResolvedVc<Box<dyn OutputAsset>>> + Send> {
Ok(asset
.references()
.await?
.iter()
.copied()
.collect::<Vec<_>>()
.into_iter())
}
|