-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathsource.rs
More file actions
135 lines (120 loc) · 4.79 KB
/
Copy pathsource.rs
File metadata and controls
135 lines (120 loc) · 4.79 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
use anyhow::{Result, anyhow};
use pack_api::project::Project;
use rustc_hash::FxHashSet;
use turbo_tasks::{
NonLocalValue, OperationVc, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc,
trace::TraceRawVcs,
};
use turbopack_core::chunk::{ChunkableModule, EvaluatableAsset};
use turbopack_dev_server::{
SourceProvider,
html::{DevHtmlAsset, DevHtmlEntry},
introspect::IntrospectionSource,
source::{
ContentSource, asset_graph::AssetGraphContentSource, combined::CombinedContentSource,
router::PrefixedRouterContentSource,
},
};
#[turbo_tasks::function]
pub async fn create_web_entry_source(
project: ResolvedVc<Project>,
) -> Result<Vc<Box<dyn ContentSource>>> {
let entries = match &*project.app_project().await? {
Some(app_project) => {
let asset_context = Vc::upcast(app_project.app_module_context());
let runtime_entries = app_project.app_runtime_entries();
let chunking_context = app_project
.project()
.client_chunking_context()
.to_resolved()
.await?;
app_project
.resolved_entrypoints()
.await?
.iter()
.map(async |app| {
let module_graph = app
.module_graph_for_entry(asset_context, runtime_entries)
.to_resolved()
.await?;
let entry_modules = app.app_entry_modules(asset_context).await?.to_vec();
entry_modules
.into_iter()
.map(async |m| {
if let (Some(chunkable_module), Some(entry)) = (
ResolvedVc::try_sidecast::<Box<dyn ChunkableModule>>(m),
ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(m),
) {
Ok(DevHtmlEntry {
chunkable_module,
module_graph,
chunking_context,
runtime_entries: Some(
runtime_entries.with_entry(*entry).to_resolved().await?,
),
})
} else if let Some(chunkable_module) =
ResolvedVc::try_sidecast::<Box<dyn ChunkableModule>>(m)
{
// TODO this is missing runtime code, so it's probably broken and we should also
// add an ecmascript chunk with the runtime code
Ok(DevHtmlEntry {
chunkable_module,
module_graph,
chunking_context,
runtime_entries: None,
})
} else {
Err(anyhow!(
"Entry module is not chunkable, so it can't be used to bootstrap the \
application"
))
}
})
.try_join()
.await
})
.try_flat_join()
.await?
}
None => vec![],
};
let client_root = project.client_root().owned().await?;
let entry_asset = Vc::upcast(DevHtmlAsset::new_with_body(
client_root.join("index.html")?,
entries,
// Just add this root node for test
r#"<div id="root"></div>"#.into(),
));
let graph = Vc::upcast(AssetGraphContentSource::new_lazy(client_root, entry_asset));
Ok(graph)
}
#[turbo_tasks::function(operation)]
async fn source(
web_source: ResolvedVc<Box<dyn ContentSource>>,
) -> Result<Vc<Box<dyn ContentSource>>> {
let main_source = CombinedContentSource::new(vec![web_source])
.to_resolved()
.await?;
let introspect = ResolvedVc::upcast(
IntrospectionSource {
roots: FxHashSet::from_iter([ResolvedVc::upcast(main_source)]),
}
.resolved_cell(),
);
let main_source = ResolvedVc::upcast(main_source);
Ok(Vc::upcast(PrefixedRouterContentSource::new(
Default::default(),
vec![("__turbopack__".into(), introspect)],
*main_source,
)))
}
#[derive(Clone, TraceRawVcs, NonLocalValue)]
pub struct ServerSourceProvider {
pub web_source: ResolvedVc<Box<dyn ContentSource>>,
}
impl SourceProvider for ServerSourceProvider {
fn get_source(&self) -> OperationVc<Box<dyn ContentSource>> {
source(self.web_source)
}
}