turbopack_core/
output.rs

1use anyhow::Result;
2use turbo_tasks::{FxIndexSet, ResolvedVc, Vc};
3use turbo_tasks_fs::FileSystemPath;
4
5use crate::asset::Asset;
6
7#[turbo_tasks::value(transparent)]
8pub struct OptionOutputAsset(Option<ResolvedVc<Box<dyn OutputAsset>>>);
9
10/// An asset that should be outputted, e. g. written to disk or served from a
11/// server.
12#[turbo_tasks::value_trait]
13pub trait OutputAsset: Asset {
14    /// The identifier of the [OutputAsset]. It's expected to be unique and
15    /// capture all properties of the [OutputAsset].
16    #[turbo_tasks::function]
17    fn path(&self) -> Vc<FileSystemPath>;
18
19    /// Other references [OutputAsset]s from this [OutputAsset].
20    #[turbo_tasks::function]
21    fn references(self: Vc<Self>) -> Vc<OutputAssets> {
22        OutputAssets::empty()
23    }
24
25    #[turbo_tasks::function]
26    fn size_bytes(self: Vc<Self>) -> Vc<Option<u64>> {
27        Vc::cell(None)
28    }
29}
30
31#[turbo_tasks::value(transparent)]
32pub struct OutputAssets(Vec<ResolvedVc<Box<dyn OutputAsset>>>);
33
34#[turbo_tasks::value_impl]
35impl OutputAssets {
36    #[turbo_tasks::function]
37    pub fn new(assets: Vec<ResolvedVc<Box<dyn OutputAsset>>>) -> Vc<Self> {
38        Vc::cell(assets)
39    }
40
41    #[turbo_tasks::function]
42    pub async fn concatenate(&self, other: Vc<Self>) -> Result<Vc<Self>> {
43        let mut assets: FxIndexSet<_> = self.0.iter().copied().collect();
44        assets.extend(other.await?.iter().copied());
45        Ok(Vc::cell(assets.into_iter().collect()))
46    }
47}
48
49impl OutputAssets {
50    pub fn empty() -> Vc<Self> {
51        Self::new(vec![])
52    }
53
54    pub fn empty_resolved() -> ResolvedVc<Self> {
55        ResolvedVc::cell(vec![])
56    }
57}
58
59/// A set of [OutputAsset]s
60#[turbo_tasks::value(transparent)]
61pub struct OutputAssetsSet(FxIndexSet<ResolvedVc<Box<dyn OutputAsset>>>);
62
63// TODO All Vc::try_resolve_downcast::<Box<dyn OutputAsset>> calls should be
64// removed