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
use std::{
collections::HashMap,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};
use crossbeam::channel::{self};
use crossbeam::queue::SegQueue;
use dashmap::DashSet;
use futures::future::join_all;
use petgraph::{
dot::Dot,
graph::NodeIndex,
visit::{depth_first_search, DfsEvent},
};
use smol_str::SmolStr;
use crate::{
chunk::Chunk, js_ext_module::JsExtModule, js_module::JsModule, plugin::ResolvedId,
plugin_driver::PluginDriver, worker::Worker, bundle_options::BundleOptions,
};
#[derive(Debug, Clone, Copy)]
pub enum Relation {
AsyncImport,
StaticImport,
}
type ModuleGraph = petgraph::Graph<SmolStr, Relation>;
type ChunkGraph = petgraph::Graph<SmolStr, ()>;
pub enum Msg {
DependencyReference(SmolStr, SmolStr, Relation),
NewMod(JsModule),
NewExtMod(JsExtModule),
}
pub struct GraphContainer {
pub plugin_driver: Arc<PluginDriver>,
pub resolved_entries: Vec<ResolvedId>,
pub module_by_id: HashMap<SmolStr, JsModule>,
pub input: BundleOptions,
}
impl GraphContainer {
pub async fn build(&mut self) {
self.generate_module_graph().await;
}
pub fn sort_modules(&mut self) {
}
pub async fn split_chunks(&mut self) {
}
pub async fn generate_module_graph(&mut self) {
let nums_of_thread = num_cpus::get();
let idle_thread_count: Arc<AtomicUsize> = Arc::new(AtomicUsize::new(nums_of_thread));
let job_queue: Arc<SegQueue<ResolvedId>> = Default::default();
self.resolved_entries = join_all(
self.input
.entries
.iter()
.map(|entry| self.plugin_driver.resolve_id(None, entry)),
)
.await
.into_iter()
.collect();
println!("resolved_entries {:?}", self.resolved_entries);
let mut path_to_node_idx: HashMap<SmolStr, NodeIndex> = Default::default();
let mut module_graph = ModuleGraph::new();
self.resolved_entries.iter().for_each(|resolved_entry_id| {
let entry_idx = module_graph.add_node(resolved_entry_id.id.clone());
path_to_node_idx.insert(resolved_entry_id.id.clone(), entry_idx);
job_queue.push(resolved_entry_id.clone());
});
let processed_id: Arc<DashSet<SmolStr>> = Default::default();
let (tx, rx) = channel::unbounded::<Msg>();
println!("job_queue {:?}", job_queue.len());
for idx in 0..nums_of_thread {
println!("spawing {:?}", idx);
let idle_thread_count = idle_thread_count.clone();
let plugin_driver = self.plugin_driver.clone();
let worker = Worker {
tx: tx.clone(),
job_queue: job_queue.clone(),
processed_id: processed_id.clone(),
plugin_driver,
};
tokio::task::spawn(async move {
'root: loop {
println!("worker: {:?}", idx);
idle_thread_count.fetch_sub(1, Ordering::SeqCst);
worker.run().await;
idle_thread_count.fetch_add(1, Ordering::SeqCst);
loop {
if !worker.job_queue.is_empty() {
break;
} else if idle_thread_count.load(Ordering::SeqCst) == nums_of_thread {
break 'root;
}
}
}
});
}
while idle_thread_count.load(Ordering::SeqCst) != nums_of_thread
|| job_queue.len() > 0
|| !rx.is_empty()
{
if let Ok(job) = rx.try_recv() {
match job {
Msg::NewMod(module) => {
self.module_by_id.insert(module.id.clone(), module);
}
Msg::DependencyReference(from, to, rel) => {
let from_id = *path_to_node_idx
.entry(from)
.or_insert_with_key(|key| module_graph.add_node(key.clone()));
let to_id = *path_to_node_idx
.entry(to)
.or_insert_with_key(|key| module_graph.add_node(key.clone()));
module_graph.add_edge(from_id, to_id, rel);
}
_ => {}
}
}
}
println!("graph: {:?}", Dot::new(&module_graph));
}
}