-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathentrypoint.rs
More file actions
200 lines (182 loc) · 6.57 KB
/
Copy pathentrypoint.rs
File metadata and controls
200 lines (182 loc) · 6.57 KB
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
use anyhow::Result;
use std::sync::Arc;
use turbo_tasks::{Effects, FxIndexSet, ReadRef, ResolvedVc, TryJoinIterExt, Vc, take_effects};
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{
asset::AssetContent,
issue::PlainIssue,
output::{OutputAsset, OutputAssets},
virtual_output::VirtualOutputAsset,
};
use crate::{
endpoint::{Endpoint, Endpoints},
operation::EntrypointsOperation,
project::ProjectContainer,
utils::get_issues,
webpack_stats::{OutputAssetGroups, generate_webpack_stats},
};
#[turbo_tasks::value(shared)]
pub struct Entrypoints {
pub apps: Option<ResolvedVc<Endpoints>>,
pub libraries: Option<ResolvedVc<Endpoints>>,
}
#[turbo_tasks::function(operation, root)]
pub async fn get_all_written_entrypoints_with_issues_operation(
container: ResolvedVc<ProjectContainer>,
) -> Result<Vc<EntrypointsWithIssues>> {
let entrypoints_operation =
EntrypointsOperation::new(all_entrypoints_write_to_disk_operation(container));
let entrypoints = entrypoints_operation.read_strongly_consistent().await?;
let issues = get_issues(entrypoints_operation).await?;
let effects = Arc::new(take_effects(entrypoints_operation).await?);
Ok(EntrypointsWithIssues {
entrypoints,
issues,
effects,
}
.cell())
}
#[turbo_tasks::function(operation, root)]
pub async fn all_entrypoints_write_to_disk_operation(
project: ResolvedVc<ProjectContainer>,
) -> Result<Vc<Entrypoints>> {
let _ = project
.project()
.emit_all_output_assets(all_output_assets_operation(project))
.resolve()
.await?;
Ok(project.entrypoints())
}
#[turbo_tasks::function(operation, root)]
pub async fn all_output_assets_operation(
container: ResolvedVc<ProjectContainer>,
) -> Result<Vc<OutputAssets>> {
let project = container.project();
let endpoint_asset_groups = project
.get_all_endpoints()
.await?
.iter()
.map(|endpoint| async move { Ok(endpoint.output().await?.output_assets) })
.try_join()
.await?;
let output_assets: FxIndexSet<ResolvedVc<Box<dyn OutputAsset>>> = endpoint_asset_groups
.iter()
.map(|assets| async move { assets.await })
.try_join()
.await?
.iter()
.flat_map(|assets| assets.iter().copied())
.collect();
let output_assets: Vc<OutputAssets> = Vc::cell(output_assets.into_iter().collect());
if !*container.project().should_create_webpack_stats().await? {
return Ok(output_assets);
}
let dist_root = container.project().dist_root();
let server_config = container.project().config().server().await?;
let has_server = server_config
.entry
.as_ref()
.is_some_and(|entry| entry.has_entries())
|| server_config.function.is_some();
let mut stats_outputs: Vec<ResolvedVc<Box<dyn OutputAsset>>> = Vec::new();
if !has_server {
stats_outputs.push(
make_stats_output(
output_assets,
Vc::<OutputAssetGroups>::cell(endpoint_asset_groups),
dist_root,
)
.await?,
);
} else {
let server_dist_root_vc = container.project().server_dist_root();
let server_dist_root_read = server_dist_root_vc.await?;
let mut client: Vec<ResolvedVc<Box<dyn OutputAsset>>> = Vec::new();
let mut server: Vec<ResolvedVc<Box<dyn OutputAsset>>> = Vec::new();
let mut client_groups = Vec::with_capacity(endpoint_asset_groups.len());
for assets in endpoint_asset_groups {
let mut group = Vec::new();
for asset in assets.await?.iter().copied() {
if !asset.path().await?.is_inside_ref(&server_dist_root_read) {
group.push(asset);
}
}
if !group.is_empty() {
client_groups.push(ResolvedVc::cell(group));
}
}
for asset in output_assets.await?.iter().copied() {
if asset.path().await?.is_inside_ref(&server_dist_root_read) {
server.push(asset);
} else {
client.push(asset);
}
}
stats_outputs.push(
make_stats_output(
Vc::cell(client),
Vc::<OutputAssetGroups>::cell(client_groups),
dist_root,
)
.await?,
);
if !server.is_empty() {
let server_assets = ResolvedVc::cell(server);
stats_outputs.push(
make_stats_output(
*server_assets,
Vc::<OutputAssetGroups>::cell(vec![server_assets]),
server_dist_root_vc,
)
.await?,
);
}
}
Ok(output_assets.concatenate(*ResolvedVc::cell(stats_outputs)))
}
async fn make_stats_output(
assets: Vc<OutputAssets>,
asset_groups: Vc<OutputAssetGroups>,
dist_root: Vc<FileSystemPath>,
) -> Result<ResolvedVc<Box<dyn OutputAsset>>> {
let webpack_stats = generate_webpack_stats(assets, asset_groups, dist_root).await?;
let stats_json = serde_json::to_string_pretty(&*webpack_stats)?;
let dist_root_owned = dist_root.owned().await?;
let stats_output = VirtualOutputAsset::new(
dist_root_owned.join("stats.json")?,
AssetContent::file(FileContent::from(File::from(stats_json)).cell()),
)
.to_resolved()
.await?;
Ok(ResolvedVc::upcast(stats_output))
}
#[turbo_tasks::value(shared, serialization = "skip")]
pub struct EntrypointsWithIssues {
pub entrypoints: ReadRef<EntrypointsOperation>,
pub issues: Arc<Vec<ReadRef<PlainIssue>>>,
pub effects: Arc<Effects>,
}
#[turbo_tasks::function(operation, root)]
pub async fn get_entrypoints_with_issues_operation(
container: ResolvedVc<ProjectContainer>,
) -> Result<Vc<EntrypointsWithIssues>> {
let entrypoints_operation =
EntrypointsOperation::new(project_container_entrypoints_operation(container));
let entrypoints = entrypoints_operation.read_strongly_consistent().await?;
let issues = get_issues(entrypoints_operation).await?;
let effects = Arc::new(take_effects(entrypoints_operation).await?);
Ok(EntrypointsWithIssues {
entrypoints,
issues,
effects,
}
.cell())
}
#[turbo_tasks::function(operation, root)]
fn project_container_entrypoints_operation(
// the container is a long-lived object with internally mutable state, there's no risk of it
// becoming stale
container: ResolvedVc<ProjectContainer>,
) -> Vc<Entrypoints> {
container.entrypoints()
}