turbopack_static/
output_asset.rs

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