1use anyhow::Result;
2use bincode::{Decode, Encode};
3use mime_guess::mime::TEXT_HTML_UTF_8;
4use turbo_rcstr::RcStr;
5use turbo_tasks::{ReadRef, ResolvedVc, TryJoinIterExt, Vc, trace::TraceRawVcs};
6use turbo_tasks_fs::{File, FileContent, FileSystemPath};
7use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_base64};
8use turbopack_core::{
9 asset::{Asset, AssetContent},
10 chunk::{
11 ChunkableModule, ChunkingContext, ChunkingContextExt, EvaluatableAssets,
12 availability_info::AvailabilityInfo,
13 },
14 module::Module,
15 module_graph::{ModuleGraph, chunk_group_info::ChunkGroup},
16 output::{OutputAsset, OutputAssets, OutputAssetsReference, OutputAssetsWithReferenced},
17 version::{Version, VersionedContent},
18};
19
20#[turbo_tasks::task_input]
21#[derive(Clone, Debug, Eq, Hash, PartialEq, TraceRawVcs, Encode, Decode)]
22pub struct DevHtmlEntry {
23 pub chunkable_module: ResolvedVc<Box<dyn ChunkableModule>>,
24 pub module_graph: ResolvedVc<ModuleGraph>,
25 pub chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
26 pub runtime_entries: Option<ResolvedVc<EvaluatableAssets>>,
27}
28
29#[turbo_tasks::value(shared)]
33#[derive(Clone)]
34pub struct DevHtmlAsset {
35 path: FileSystemPath,
36 entries: Vec<DevHtmlEntry>,
37 body: Option<RcStr>,
38}
39
40#[turbo_tasks::value_impl]
41impl OutputAssetsReference for DevHtmlAsset {
42 #[turbo_tasks::function]
43 fn references(self: Vc<Self>) -> Vc<OutputAssetsWithReferenced> {
44 self.chunk_group()
45 }
46}
47
48#[turbo_tasks::value_impl]
49impl OutputAsset for DevHtmlAsset {
50 #[turbo_tasks::function]
51 fn path(&self) -> Vc<FileSystemPath> {
52 self.path.clone().cell()
53 }
54}
55
56#[turbo_tasks::value_impl]
57impl Asset for DevHtmlAsset {
58 #[turbo_tasks::function]
59 fn content(self: Vc<Self>) -> Vc<AssetContent> {
60 self.html_content().content()
61 }
62
63 #[turbo_tasks::function]
64 fn versioned_content(self: Vc<Self>) -> Vc<Box<dyn VersionedContent>> {
65 Vc::upcast(self.html_content())
66 }
67}
68
69impl DevHtmlAsset {
70 pub fn new(path: FileSystemPath, entries: Vec<DevHtmlEntry>) -> Vc<Self> {
72 DevHtmlAsset {
73 path,
74 entries,
75 body: None,
76 }
77 .cell()
78 }
79
80 pub fn new_with_body(
82 path: FileSystemPath,
83 entries: Vec<DevHtmlEntry>,
84 body: RcStr,
85 ) -> Vc<Self> {
86 DevHtmlAsset {
87 path,
88 entries,
89 body: Some(body),
90 }
91 .cell()
92 }
93}
94
95#[turbo_tasks::value_impl]
96impl DevHtmlAsset {
97 #[turbo_tasks::function]
98 pub async fn with_path(self: Vc<Self>, path: FileSystemPath) -> Result<Vc<Self>> {
99 let mut html: DevHtmlAsset = self.owned().await?;
100 html.path = path;
101 Ok(html.cell())
102 }
103
104 #[turbo_tasks::function]
105 pub async fn with_body(self: Vc<Self>, body: RcStr) -> Result<Vc<Self>> {
106 let mut html: DevHtmlAsset = self.owned().await?;
107 html.body = Some(body);
108 Ok(html.cell())
109 }
110}
111
112#[turbo_tasks::value_impl]
113impl DevHtmlAsset {
114 #[turbo_tasks::function]
115 async fn html_content(self: Vc<Self>) -> Result<Vc<DevHtmlAssetContent>> {
116 let this = self.await?;
117 let context_path = this.path.parent();
118 let mut chunk_paths = vec![];
119 for chunk in &*self.chunk_group().await?.assets.await? {
120 let chunk_path = &*chunk.path().await?;
121 if let Some(relative_path) = context_path.get_path_to(chunk_path) {
122 chunk_paths.push(format!("/{relative_path}").into());
123 }
124 }
125
126 Ok(DevHtmlAssetContent::new(chunk_paths, this.body.clone()))
127 }
128
129 #[turbo_tasks::function]
130 async fn chunk_group(&self) -> Result<Vc<OutputAssetsWithReferenced>> {
131 let all_chunk_groups = self
132 .entries
133 .iter()
134 .map(|entry| async move {
135 let &DevHtmlEntry {
136 chunkable_module,
137 chunking_context,
138 module_graph,
139 runtime_entries,
140 } = entry;
141
142 let asset_with_referenced = if let Some(runtime_entries) = runtime_entries {
143 let runtime_entries =
144 if let Some(evaluatable) = ResolvedVc::try_downcast(chunkable_module) {
145 runtime_entries
146 .with_entry(*evaluatable)
147 .to_resolved()
148 .await?
149 } else {
150 runtime_entries
151 };
152 chunking_context
153 .evaluated_chunk_group_assets(
154 chunkable_module.ident(),
155 ChunkGroup::Entry(
156 runtime_entries
157 .await?
158 .iter()
159 .map(|v| ResolvedVc::upcast(*v))
160 .collect(),
161 ),
162 *module_graph,
163 OutputAssets::empty(),
164 AvailabilityInfo::root(),
165 )
166 .await?
167 } else {
168 chunking_context
169 .root_chunk_group_assets(
170 chunkable_module.ident(),
171 ChunkGroup::Entry(vec![ResolvedVc::upcast(chunkable_module)]),
172 *module_graph,
173 )
174 .await?
175 };
176
177 Ok((
178 asset_with_referenced.assets.await?,
179 asset_with_referenced.referenced_assets.await?,
180 asset_with_referenced.references.await?,
181 ))
182 })
183 .try_join()
184 .await?;
185
186 let mut all_assets = Vec::new();
187 let mut all_referenced_assets = Vec::new();
188 let mut all_references = Vec::new();
189 for (asset, referenced_asset, reference) in all_chunk_groups {
190 all_assets.extend(asset);
191 all_referenced_assets.extend(referenced_asset);
192 all_references.extend(reference);
193 }
194
195 Ok(OutputAssetsWithReferenced {
196 assets: ResolvedVc::cell(all_assets),
197 referenced_assets: ResolvedVc::cell(all_referenced_assets),
198 references: ResolvedVc::cell(all_references),
199 }
200 .cell())
201 }
202}
203
204#[turbo_tasks::value(operation)]
205struct DevHtmlAssetContent {
206 chunk_paths: Vec<RcStr>,
207 body: Option<RcStr>,
208}
209
210impl DevHtmlAssetContent {
211 fn new(chunk_paths: Vec<RcStr>, body: Option<RcStr>) -> Vc<Self> {
212 DevHtmlAssetContent { chunk_paths, body }.cell()
213 }
214}
215
216#[turbo_tasks::value_impl]
217impl DevHtmlAssetContent {
218 #[turbo_tasks::function]
219 fn content(&self) -> Result<Vc<AssetContent>> {
220 let mut scripts = Vec::new();
221 let mut stylesheets = Vec::new();
222
223 for relative_path in &*self.chunk_paths {
224 if relative_path.ends_with(".js") {
225 scripts.push(format!("<script src=\"{relative_path}\"></script>"));
226 } else if relative_path.ends_with(".css") {
227 stylesheets.push(format!(
228 "<link data-turbopack rel=\"stylesheet\" href=\"{relative_path}\">"
229 ));
230 } else {
231 anyhow::bail!("chunk with unknown asset type: {}", relative_path)
232 }
233 }
234
235 let body = match &self.body {
236 Some(body) => body.as_str(),
237 None => "",
238 };
239
240 let html: RcStr = format!(
241 "<!DOCTYPE html>\n<html>\n<head>\n{}\n</head>\n<body>\n{}\n{}\n</body>\n</html>",
242 stylesheets.join("\n"),
243 body,
244 scripts.join("\n"),
245 )
246 .into();
247
248 Ok(AssetContent::file(
249 FileContent::Content(File::from(html).with_content_type(TEXT_HTML_UTF_8)).cell(),
250 ))
251 }
252
253 #[turbo_tasks::function]
254 async fn version(self: Vc<Self>) -> Result<Vc<DevHtmlAssetVersion>> {
255 let this = self.await?;
256 Ok(DevHtmlAssetVersion { content: this }.cell())
257 }
258}
259
260#[turbo_tasks::value_impl]
261impl VersionedContent for DevHtmlAssetContent {
262 #[turbo_tasks::function]
263 fn content(self: Vc<Self>) -> Vc<AssetContent> {
264 self.content()
265 }
266
267 #[turbo_tasks::function]
268 fn version(self: Vc<Self>) -> Vc<Box<dyn Version>> {
269 Vc::upcast(self.version())
270 }
271}
272
273#[turbo_tasks::value(operation)]
274struct DevHtmlAssetVersion {
275 content: ReadRef<DevHtmlAssetContent>,
276}
277
278#[turbo_tasks::value_impl]
279impl Version for DevHtmlAssetVersion {
280 #[turbo_tasks::function]
281 fn id(&self) -> Vc<RcStr> {
282 let mut hasher = Xxh3Hash64Hasher::new();
283 for relative_path in &*self.content.chunk_paths {
284 hasher.write_ref(relative_path);
285 }
286 if let Some(body) = &self.content.body {
287 hasher.write_ref(body);
288 }
289 let hash = hasher.finish();
290 let hash = encode_base64(hash);
291 Vc::cell(hash.into())
292 }
293}