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,
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 OutputAsset for StaticOutputAsset {
36    #[turbo_tasks::function]
37    async fn path(&self) -> Result<Vc<FileSystemPath>> {
38        let content = self.source.content();
39        let content_hash = if let AssetContent::File(file) = &*content.await? {
40            if let FileContent::Content(file) = &*file.await? {
41                turbo_tasks_hash::hash_xxh3_hash64(file.content())
42            } else {
43                anyhow::bail!("StaticAsset::path: not found")
44            }
45        } else {
46            anyhow::bail!("StaticAsset::path: unsupported file content")
47        };
48        let content_hash_b16 = turbo_tasks_hash::encode_hex(content_hash);
49        Ok(self.chunking_context.asset_path(
50            content_hash_b16.into(),
51            self.source.ident(),
52            self.tag.clone(),
53        ))
54    }
55}
56
57#[turbo_tasks::value_impl]
58impl Asset for StaticOutputAsset {
59    #[turbo_tasks::function]
60    fn content(&self) -> Vc<AssetContent> {
61        self.source.content()
62    }
63}