turbopack_core/
output.rs

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