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    fn path(&self) -> Vc<FileSystemPath>;
17
18    /// Other references [OutputAsset]s from this [OutputAsset].
19    fn references(self: Vc<Self>) -> Vc<OutputAssets> {
20        OutputAssets::empty()
21    }
22
23    fn size_bytes(self: Vc<Self>) -> Vc<Option<u64>> {
24        Vc::cell(None)
25    }
26}
27
28#[turbo_tasks::value(transparent)]
29pub struct OutputAssets(Vec<ResolvedVc<Box<dyn OutputAsset>>>);
30
31#[turbo_tasks::value_impl]
32impl OutputAssets {
33    #[turbo_tasks::function]
34    pub fn new(assets: Vec<ResolvedVc<Box<dyn OutputAsset>>>) -> Vc<Self> {
35        Vc::cell(assets)
36    }
37
38    #[turbo_tasks::function]
39    pub async fn concatenate(&self, other: Vc<Self>) -> Result<Vc<Self>> {
40        let mut assets: FxIndexSet<_> = self.0.iter().copied().collect();
41        assets.extend(other.await?.iter().copied());
42        Ok(Vc::cell(assets.into_iter().collect()))
43    }
44}
45
46impl OutputAssets {
47    pub fn empty() -> Vc<Self> {
48        Self::new(vec![])
49    }
50
51    pub fn empty_resolved() -> ResolvedVc<Self> {
52        ResolvedVc::cell(vec![])
53    }
54}
55
56/// A set of [OutputAsset]s
57#[turbo_tasks::value(transparent)]
58pub struct OutputAssetsSet(FxIndexSet<ResolvedVc<Box<dyn OutputAsset>>>);
59
60// TODO All Vc::try_resolve_downcast::<Box<dyn OutputAsset>> calls should be
61// removed