turbopack_core/source_map/
source_map_asset.rs1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use turbo_rcstr::{RcStr, rcstr};
4use turbo_tasks::{
5 FxIndexSet, NonLocalValue, ResolvedVc, ValueToString, Vc, debug::ValueDebugFormat,
6 trace::TraceRawVcs,
7};
8use turbo_tasks_fs::{File, FileSystemPath};
9
10use crate::{
11 asset::{Asset, AssetContent},
12 chunk::ChunkingContext,
13 ident::AssetIdent,
14 introspect::{Introspectable, IntrospectableChildren},
15 output::OutputAsset,
16 source_map::{GenerateSourceMap, SourceMap},
17};
18
19#[derive(PartialEq, Eq, Serialize, Deserialize, NonLocalValue, TraceRawVcs, ValueDebugFormat)]
20enum PathType {
21 Fixed {
22 path: FileSystemPath,
23 },
24 FromIdent {
25 chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
26 ident_for_path: ResolvedVc<AssetIdent>,
27 },
28}
29
30#[turbo_tasks::value]
32pub struct SourceMapAsset {
33 path_ty: PathType,
34 generate_source_map: ResolvedVc<Box<dyn GenerateSourceMap>>,
35}
36
37#[turbo_tasks::value_impl]
38impl SourceMapAsset {
39 #[turbo_tasks::function]
40 pub fn new(
41 chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
42 ident_for_path: ResolvedVc<AssetIdent>,
43 generate_source_map: ResolvedVc<Box<dyn GenerateSourceMap>>,
44 ) -> Vc<Self> {
45 SourceMapAsset {
46 path_ty: PathType::FromIdent {
47 chunking_context,
48 ident_for_path,
49 },
50 generate_source_map,
51 }
52 .cell()
53 }
54
55 #[turbo_tasks::function]
56 pub fn new_fixed(
57 path: FileSystemPath,
58 generate_source_map: ResolvedVc<Box<dyn GenerateSourceMap>>,
59 ) -> Vc<Self> {
60 SourceMapAsset {
61 path_ty: PathType::Fixed { path },
62 generate_source_map,
63 }
64 .cell()
65 }
66}
67
68#[turbo_tasks::value_impl]
69impl OutputAsset for SourceMapAsset {
70 #[turbo_tasks::function]
71 async fn path(self: Vc<Self>) -> Result<Vc<FileSystemPath>> {
72 let this = self.await?;
75 Ok(match &this.path_ty {
76 PathType::FromIdent {
77 chunking_context,
78 ident_for_path,
79 } => chunking_context
80 .chunk_path(
81 Some(Vc::upcast(self)),
82 **ident_for_path,
83 None,
84 rcstr!(".js"),
85 )
86 .await?
87 .append(".map")?
88 .cell(),
89 PathType::Fixed { path } => path.append(".map")?.cell(),
90 })
91 }
92}
93
94#[turbo_tasks::value_impl]
95impl Asset for SourceMapAsset {
96 #[turbo_tasks::function]
97 async fn content(&self) -> Result<Vc<AssetContent>> {
98 if let Some(sm) = &*self.generate_source_map.generate_source_map().await? {
99 Ok(AssetContent::file(File::from(sm.clone()).into()))
100 } else {
101 Ok(AssetContent::file(
102 File::from(SourceMap::empty_rope()).into(),
103 ))
104 }
105 }
106}
107
108#[turbo_tasks::value_impl]
109impl Introspectable for SourceMapAsset {
110 #[turbo_tasks::function]
111 fn ty(&self) -> Vc<RcStr> {
112 Vc::cell(rcstr!("source map"))
113 }
114
115 #[turbo_tasks::function]
116 fn title(self: Vc<Self>) -> Vc<RcStr> {
117 self.path().to_string()
118 }
119
120 #[turbo_tasks::function]
121 fn details(&self) -> Vc<RcStr> {
122 Vc::cell(rcstr!("source map of an asset"))
123 }
124
125 #[turbo_tasks::function]
126 fn children(&self) -> Result<Vc<IntrospectableChildren>> {
127 let mut children = FxIndexSet::default();
128 if let Some(asset) =
129 ResolvedVc::try_sidecast::<Box<dyn Introspectable>>(self.generate_source_map)
130 {
131 children.insert((rcstr!("asset"), asset));
132 }
133 Ok(Vc::cell(children))
134 }
135}