11use anyhow:: { anyhow, Result } ;
22use mime_guess:: mime:: TEXT_HTML_UTF_8 ;
3- use turbo_tasks:: primitives:: StringVc ;
3+ use turbo_tasks:: { primitives:: StringVc , TryJoinIterExt } ;
44use turbo_tasks_fs:: { File , FileSystemPathVc } ;
55use turbo_tasks_hash:: { encode_hex, Xxh3Hash64Hasher } ;
66use turbopack_core:: {
77 asset:: { Asset , AssetContentVc , AssetVc , AssetsVc } ,
8+ chunk:: {
9+ ChunkableAsset , ChunkableAssetVc , ChunkingContext , ChunkingContextVc , EvaluatableAssetsVc ,
10+ } ,
811 ident:: AssetIdentVc ,
912 reference:: { AssetReferencesVc , SingleAssetReferenceVc } ,
1013 version:: { Version , VersionVc , VersionedContent , VersionedContentVc } ,
@@ -17,7 +20,13 @@ use turbopack_core::{
1720#[ derive( Clone ) ]
1821pub struct DevHtmlAsset {
1922 path : FileSystemPathVc ,
20- chunk_groups : Vec < AssetsVc > ,
23+ // TODO(WEB-945) This should become a `Vec<DevHtmlEntry>` once we have a
24+ // `turbo_tasks::input` attribute macro/`Input` derive macro.
25+ entries : Vec < (
26+ ChunkableAssetVc ,
27+ ChunkingContextVc ,
28+ Option < EvaluatableAssetsVc > ,
29+ ) > ,
2130 body : Option < String > ,
2231}
2332
@@ -39,16 +48,12 @@ impl Asset for DevHtmlAsset {
3948 }
4049
4150 #[ turbo_tasks:: function]
42- async fn references ( & self ) -> Result < AssetReferencesVc > {
51+ async fn references ( self_vc : DevHtmlAssetVc ) -> Result < AssetReferencesVc > {
4352 let mut references = Vec :: new ( ) ;
44- for chunk_group in & self . chunk_groups {
45- let chunks = chunk_group. await ?;
46- for chunk in chunks. iter ( ) {
47- references. push (
48- SingleAssetReferenceVc :: new ( * chunk, dev_html_chunk_reference_description ( ) )
49- . into ( ) ,
50- ) ;
51- }
53+ for chunk in & * self_vc. chunks ( ) . await ? {
54+ references. push (
55+ SingleAssetReferenceVc :: new ( * chunk, dev_html_chunk_reference_description ( ) ) . into ( ) ,
56+ ) ;
5257 }
5358 Ok ( AssetReferencesVc :: cell ( references) )
5459 }
@@ -61,10 +66,17 @@ impl Asset for DevHtmlAsset {
6166
6267impl DevHtmlAssetVc {
6368 /// Create a new dev HTML asset.
64- pub fn new ( path : FileSystemPathVc , chunk_groups : Vec < AssetsVc > ) -> Self {
69+ pub fn new (
70+ path : FileSystemPathVc ,
71+ entries : Vec < (
72+ ChunkableAssetVc ,
73+ ChunkingContextVc ,
74+ Option < EvaluatableAssetsVc > ,
75+ ) > ,
76+ ) -> Self {
6577 DevHtmlAsset {
6678 path,
67- chunk_groups ,
79+ entries ,
6880 body : None ,
6981 }
7082 . cell ( )
@@ -73,12 +85,16 @@ impl DevHtmlAssetVc {
7385 /// Create a new dev HTML asset.
7486 pub fn new_with_body (
7587 path : FileSystemPathVc ,
76- chunk_groups : Vec < AssetsVc > ,
88+ entries : Vec < (
89+ ChunkableAssetVc ,
90+ ChunkingContextVc ,
91+ Option < EvaluatableAssetsVc > ,
92+ ) > ,
7793 body : String ,
7894 ) -> Self {
7995 DevHtmlAsset {
8096 path,
81- chunk_groups ,
97+ entries ,
8298 body : Some ( body) ,
8399 }
84100 . cell ( )
@@ -110,17 +126,44 @@ impl DevHtmlAssetVc {
110126 let context_path = this. path . parent ( ) . await ?;
111127
112128 let mut chunk_paths = vec ! [ ] ;
113- for chunk_group in & this. chunk_groups {
114- for chunk in chunk_group. await ?. iter ( ) {
115- let chunk_path = & * chunk. ident ( ) . path ( ) . await ?;
116- if let Some ( relative_path) = context_path. get_path_to ( chunk_path) {
117- chunk_paths. push ( format ! ( "/{relative_path}" ) ) ;
118- }
129+ for chunk in & * self . chunks ( ) . await ? {
130+ let chunk_path = & * chunk. ident ( ) . path ( ) . await ?;
131+ if let Some ( relative_path) = context_path. get_path_to ( chunk_path) {
132+ chunk_paths. push ( format ! ( "/{relative_path}" ) ) ;
119133 }
120134 }
121135
122136 Ok ( DevHtmlAssetContentVc :: new ( chunk_paths, this. body . clone ( ) ) )
123137 }
138+
139+ #[ turbo_tasks:: function]
140+ async fn chunks ( self ) -> Result < AssetsVc > {
141+ let this = self . await ?;
142+
143+ let all_assets = this
144+ . entries
145+ . iter ( )
146+ . map ( |entry| async move {
147+ let ( chunkable_asset, chunking_context, runtime_entries) = entry;
148+
149+ let chunk = chunkable_asset. as_root_chunk ( * chunking_context) ;
150+ let assets = if let Some ( runtime_entries) = runtime_entries {
151+ chunking_context. evaluated_chunk_group ( chunk, * runtime_entries)
152+ } else {
153+ chunking_context. chunk_group ( chunk)
154+ } ;
155+
156+ Ok ( assets. await ?)
157+ } )
158+ . try_join ( )
159+ . await ?
160+ . iter ( )
161+ . flatten ( )
162+ . copied ( )
163+ . collect ( ) ;
164+
165+ Ok ( AssetsVc :: cell ( all_assets) )
166+ }
124167}
125168
126169#[ turbo_tasks:: value]
0 commit comments