1
1
use anyhow:: { anyhow, Result } ;
2
2
use mime_guess:: mime:: TEXT_HTML_UTF_8 ;
3
- use turbo_tasks:: primitives:: StringVc ;
3
+ use turbo_tasks:: { primitives:: StringVc , TryJoinIterExt } ;
4
4
use turbo_tasks_fs:: { File , FileSystemPathVc } ;
5
5
use turbo_tasks_hash:: { encode_hex, Xxh3Hash64Hasher } ;
6
6
use turbopack_core:: {
7
7
asset:: { Asset , AssetContentVc , AssetVc , AssetsVc } ,
8
+ chunk:: {
9
+ ChunkableAsset , ChunkableAssetVc , ChunkingContext , ChunkingContextVc , EvaluatableAssetsVc ,
10
+ } ,
8
11
ident:: AssetIdentVc ,
9
12
reference:: { AssetReferencesVc , SingleAssetReferenceVc } ,
10
13
version:: { Version , VersionVc , VersionedContent , VersionedContentVc } ,
@@ -17,7 +20,13 @@ use turbopack_core::{
17
20
#[ derive( Clone ) ]
18
21
pub struct DevHtmlAsset {
19
22
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
+ ) > ,
21
30
body : Option < String > ,
22
31
}
23
32
@@ -39,16 +48,12 @@ impl Asset for DevHtmlAsset {
39
48
}
40
49
41
50
#[ turbo_tasks:: function]
42
- async fn references ( & self ) -> Result < AssetReferencesVc > {
51
+ async fn references ( self_vc : DevHtmlAssetVc ) -> Result < AssetReferencesVc > {
43
52
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
+ ) ;
52
57
}
53
58
Ok ( AssetReferencesVc :: cell ( references) )
54
59
}
@@ -61,10 +66,17 @@ impl Asset for DevHtmlAsset {
61
66
62
67
impl DevHtmlAssetVc {
63
68
/// 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 {
65
77
DevHtmlAsset {
66
78
path,
67
- chunk_groups ,
79
+ entries ,
68
80
body : None ,
69
81
}
70
82
. cell ( )
@@ -73,12 +85,16 @@ impl DevHtmlAssetVc {
73
85
/// Create a new dev HTML asset.
74
86
pub fn new_with_body (
75
87
path : FileSystemPathVc ,
76
- chunk_groups : Vec < AssetsVc > ,
88
+ entries : Vec < (
89
+ ChunkableAssetVc ,
90
+ ChunkingContextVc ,
91
+ Option < EvaluatableAssetsVc > ,
92
+ ) > ,
77
93
body : String ,
78
94
) -> Self {
79
95
DevHtmlAsset {
80
96
path,
81
- chunk_groups ,
97
+ entries ,
82
98
body : Some ( body) ,
83
99
}
84
100
. cell ( )
@@ -110,17 +126,44 @@ impl DevHtmlAssetVc {
110
126
let context_path = this. path . parent ( ) . await ?;
111
127
112
128
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}" ) ) ;
119
133
}
120
134
}
121
135
122
136
Ok ( DevHtmlAssetContentVc :: new ( chunk_paths, this. body . clone ( ) ) )
123
137
}
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
+ }
124
167
}
125
168
126
169
#[ turbo_tasks:: value]
0 commit comments