turbopack_css/chunk/
source_map.rs

1use anyhow::Result;
2use turbo_rcstr::rcstr;
3use turbo_tasks::{ResolvedVc, Vc};
4use turbo_tasks_fs::{File, FileSystemPath};
5use turbopack_core::{
6    asset::{Asset, AssetContent},
7    chunk::ChunkingContext,
8    output::OutputAsset,
9    source_map::{GenerateSourceMap, SourceMap},
10};
11
12use super::CssChunk;
13
14/// Represents the source map of an css chunk.
15#[turbo_tasks::value]
16pub struct CssChunkSourceMapAsset {
17    chunk: ResolvedVc<CssChunk>,
18}
19
20#[turbo_tasks::value_impl]
21impl CssChunkSourceMapAsset {
22    #[turbo_tasks::function]
23    pub fn new(chunk: ResolvedVc<CssChunk>) -> Vc<Self> {
24        CssChunkSourceMapAsset { chunk }.cell()
25    }
26}
27
28#[turbo_tasks::value_impl]
29impl OutputAsset for CssChunkSourceMapAsset {
30    #[turbo_tasks::function]
31    async fn path(self: Vc<Self>) -> Result<Vc<FileSystemPath>> {
32        let this = self.await?;
33        let ident = this.chunk.ident_for_path();
34        Ok(this
35            .chunk
36            .await?
37            .chunking_context
38            .chunk_path(Some(Vc::upcast(self)), ident, None, rcstr!(".css"))
39            .await?
40            .append(".map")?
41            .cell())
42    }
43}
44
45#[turbo_tasks::value_impl]
46impl Asset for CssChunkSourceMapAsset {
47    #[turbo_tasks::function]
48    async fn content(&self) -> Result<Vc<AssetContent>> {
49        if let Some(sm) = &*self.chunk.generate_source_map().await? {
50            Ok(AssetContent::file(File::from(sm.clone()).into()))
51        } else {
52            Ok(AssetContent::file(
53                File::from(SourceMap::empty_rope()).into(),
54            ))
55        }
56    }
57}