turbopack_static/
output_asset.rs

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