turbopack_css/chunk/
source_map.rs

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