-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathchunk_group_files_asset.rs
256 lines (231 loc) · 7.46 KB
/
chunk_group_files_asset.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use anyhow::Result;
use indexmap::IndexSet;
use turbo_tasks::{TryJoinIterExt, Value, ValueToString, Vc};
use turbo_tasks_fs::{File, FileSystemPath};
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::{
availability_info::AvailabilityInfo, Chunk, ChunkItem, ChunkableModule, ChunkingContext,
EvaluatableAssets,
},
ident::AssetIdent,
introspect::{
asset::{content_to_details, IntrospectableAsset},
Introspectable, IntrospectableChildren,
},
module::Module,
output::{OutputAsset, OutputAssets},
reference::{AssetReference, AssetReferences, SingleAssetReference},
};
use crate::{
chunk::{
EcmascriptChunk, EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
EcmascriptChunkingContext, EcmascriptExports,
},
utils::StringifyJs,
EcmascriptModuleAsset,
};
#[turbo_tasks::function]
fn modifier() -> Vc<String> {
Vc::cell("chunk group files".to_string())
}
/// An asset that exports a list of chunk URLs by putting the [asset] into a
/// ChunkGroup with the provided ChunkingContext.
#[turbo_tasks::value(shared)]
pub struct ChunkGroupFilesAsset {
pub module: Vc<Box<dyn ChunkableModule>>,
pub client_root: Vc<FileSystemPath>,
pub chunking_context: Vc<Box<dyn ChunkingContext>>,
pub runtime_entries: Option<Vc<EvaluatableAssets>>,
}
#[turbo_tasks::function]
fn module_description() -> Vc<String> {
Vc::cell("module".to_string())
}
#[turbo_tasks::function]
fn runtime_entry_description() -> Vc<String> {
Vc::cell("runtime entry".to_string())
}
#[turbo_tasks::value_impl]
impl Module for ChunkGroupFilesAsset {
#[turbo_tasks::function]
fn ident(&self) -> Vc<AssetIdent> {
self.module.ident().with_modifier(modifier())
}
#[turbo_tasks::function]
async fn references(&self) -> Result<Vc<AssetReferences>> {
let mut references: Vec<Vc<Box<dyn AssetReference>>> = vec![Vc::upcast(
SingleAssetReference::new(Vc::upcast(self.module), module_description()),
)];
if let Some(runtime_entries) = self.runtime_entries {
references.extend(runtime_entries.await?.iter().map(|&entry| {
let reference: Vc<Box<dyn AssetReference>> = Vc::upcast(SingleAssetReference::new(
Vc::upcast(entry),
runtime_entry_description(),
));
reference
}));
}
Ok(Vc::cell(references))
}
}
#[turbo_tasks::value_impl]
impl Asset for ChunkGroupFilesAsset {
#[turbo_tasks::function]
fn content(&self) -> Vc<AssetContent> {
AssetContent::file(File::from("// Chunking only content".to_string()).into())
}
}
#[turbo_tasks::value_impl]
impl ChunkableModule for ChunkGroupFilesAsset {
#[turbo_tasks::function]
fn as_chunk(
self: Vc<Self>,
context: Vc<Box<dyn ChunkingContext>>,
availability_info: Value<AvailabilityInfo>,
) -> Vc<Box<dyn Chunk>> {
Vc::upcast(EcmascriptChunk::new(
context,
Vc::upcast(self),
availability_info,
))
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkPlaceable for ChunkGroupFilesAsset {
#[turbo_tasks::function]
async fn as_chunk_item(
self: Vc<Self>,
chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
) -> Result<Vc<Box<dyn EcmascriptChunkItem>>> {
let this = self.await?;
Ok(Vc::upcast(
ChunkGroupFilesChunkItem {
chunking_context,
client_root: this.client_root,
inner: self,
}
.cell(),
))
}
#[turbo_tasks::function]
fn get_exports(&self) -> Vc<EcmascriptExports> {
EcmascriptExports::Value.cell()
}
}
#[turbo_tasks::value]
struct ChunkGroupFilesChunkItem {
chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
client_root: Vc<FileSystemPath>,
inner: Vc<ChunkGroupFilesAsset>,
}
#[turbo_tasks::value_impl]
impl ChunkGroupFilesChunkItem {
#[turbo_tasks::function]
async fn chunks(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
let this = self.await?;
let module = this.inner.await?;
let chunks = if let Some(ecma) =
Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(module.module).await?
{
module.chunking_context.evaluated_chunk_group(
ecma.as_root_chunk(module.chunking_context),
module
.runtime_entries
.unwrap_or_else(EvaluatableAssets::empty)
.with_entry(Vc::upcast(ecma)),
)
} else {
module
.chunking_context
.chunk_group(module.module.as_root_chunk(module.chunking_context))
};
Ok(chunks)
}
}
#[turbo_tasks::value_impl]
impl EcmascriptChunkItem for ChunkGroupFilesChunkItem {
#[turbo_tasks::function]
fn chunking_context(&self) -> Vc<Box<dyn EcmascriptChunkingContext>> {
self.chunking_context
}
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<EcmascriptChunkItemContent>> {
let chunks = self.chunks();
let this = self.await?;
let module = this.inner.await?;
let client_root = module.client_root.await?;
let chunks_paths = chunks
.await?
.iter()
.map(|chunk| chunk.ident().path())
.try_join()
.await?;
let chunks_paths: Vec<_> = chunks_paths
.iter()
.filter_map(|path| client_root.get_path_to(path))
.collect();
Ok(EcmascriptChunkItemContent {
inner_code: format!(
"__turbopack_export_value__({:#});\n",
StringifyJs(&chunks_paths)
)
.into(),
..Default::default()
}
.cell())
}
}
#[turbo_tasks::function]
fn chunk_group_chunk_reference_description() -> Vc<String> {
Vc::cell("chunk group chunk".to_string())
}
#[turbo_tasks::value_impl]
impl ChunkItem for ChunkGroupFilesChunkItem {
#[turbo_tasks::function]
fn asset_ident(&self) -> Vc<AssetIdent> {
self.inner.ident()
}
#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<AssetReferences>> {
let chunks = self.chunks();
Ok(Vc::cell(
chunks
.await?
.iter()
.copied()
.map(|chunk| {
SingleAssetReference::new(
Vc::upcast(chunk),
chunk_group_chunk_reference_description(),
)
})
.map(Vc::upcast)
.collect(),
))
}
}
#[turbo_tasks::value_impl]
impl Introspectable for ChunkGroupFilesAsset {
#[turbo_tasks::function]
fn ty(&self) -> Vc<String> {
Vc::cell("chunk group files asset".to_string())
}
#[turbo_tasks::function]
fn details(self: Vc<Self>) -> Vc<String> {
content_to_details(self.content())
}
#[turbo_tasks::function]
fn title(self: Vc<Self>) -> Vc<String> {
self.ident().to_string()
}
#[turbo_tasks::function]
async fn children(self: Vc<Self>) -> Result<Vc<IntrospectableChildren>> {
let mut children = IndexSet::new();
children.insert((
Vc::cell("inner asset".to_string()),
IntrospectableAsset::new(Vc::upcast(self.await?.module)),
));
Ok(Vc::cell(children))
}
}