Skip to main content

turbopack_static/
output_asset.rs

1use anyhow::{Context, Result};
2use turbo_rcstr::RcStr;
3use turbo_tasks::{ResolvedVc, Vc};
4use turbo_tasks_fs::FileSystemPath;
5use turbo_tasks_hash::HashAlgorithm;
6use turbopack_core::{
7    asset::{Asset, AssetContent},
8    chunk::ChunkingContext,
9    output::{OutputAsset, OutputAssetsReference},
10    source::Source,
11};
12#[turbo_tasks::value]
13pub struct StaticOutputAsset {
14    chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
15    source: ResolvedVc<Box<dyn Source>>,
16    tag: Option<RcStr>,
17}
18
19#[turbo_tasks::value_impl]
20impl StaticOutputAsset {
21    #[turbo_tasks::function]
22    pub fn new(
23        chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
24        source: ResolvedVc<Box<dyn Source>>,
25        tag: Option<RcStr>,
26    ) -> Vc<Self> {
27        Self::cell(StaticOutputAsset {
28            chunking_context,
29            source,
30            tag,
31        })
32    }
33}
34
35#[turbo_tasks::value_impl]
36impl OutputAssetsReference for StaticOutputAsset {}
37
38#[turbo_tasks::value_impl]
39impl OutputAsset for StaticOutputAsset {
40    #[turbo_tasks::function]
41    async fn path(&self) -> Result<Vc<FileSystemPath>> {
42        let content = self.source.content();
43        let content_hash = content
44            .content_hash(HashAlgorithm::Xxh3Hash128Hex)
45            .owned()
46            .await?
47            .context(
48                "Missing content when trying to generate the content hash for StaticOutputAsset",
49            )?;
50        Ok(self.chunking_context.asset_path(
51            Vc::cell(content_hash),
52            self.source.ident(),
53            self.tag.clone(),
54        ))
55    }
56}
57
58#[turbo_tasks::value_impl]
59impl Asset for StaticOutputAsset {
60    #[turbo_tasks::function]
61    fn content(&self) -> Vc<AssetContent> {
62        self.source.content()
63    }
64}