-
-
Notifications
You must be signed in to change notification settings - Fork 771
Expand file tree
/
Copy pathchunk_group.rs
More file actions
255 lines (228 loc) · 7.47 KB
/
chunk_group.rs
File metadata and controls
255 lines (228 loc) · 7.47 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
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
use std::{cell::RefCell, ptr::NonNull};
use napi::{Either, Env, JsString, bindgen_prelude::ToNapiValue};
use napi_derive::napi;
use rspack_collections::UkeyMap;
use rspack_core::{Compilation, CompilationId};
use rspack_napi::OneShotRef;
use crate::{
chunk::ChunkWrapper,
location::RealDependencyLocation,
module::{ModuleObject, ModuleObjectRef},
};
#[napi]
pub struct ChunkGroup {
pub(crate) chunk_group_ukey: rspack_core::ChunkGroupUkey,
compilation: NonNull<Compilation>,
}
impl ChunkGroup {
fn as_ref(&self) -> napi::Result<(&'static Compilation, &'static rspack_core::ChunkGroup)> {
let compilation = unsafe { self.compilation.as_ref() };
if let Some(chunk_group) = compilation.chunk_group_by_ukey.get(&self.chunk_group_ukey) {
Ok((compilation, chunk_group))
} else {
Err(napi::Error::from_reason(format!(
"Unable to access chunk_group with id = {:?} now. The module have been removed on the Rust side.",
self.chunk_group_ukey
)))
}
}
}
#[napi]
impl ChunkGroup {
#[napi(getter, ts_return_type = "Chunk[]")]
pub fn chunks(&self) -> napi::Result<Vec<ChunkWrapper>> {
let (compilation, chunk_graph) = self.as_ref()?;
Ok(
chunk_graph
.chunks
.iter()
.map(|ukey| ChunkWrapper::new(*ukey, compilation))
.collect::<Vec<_>>(),
)
}
#[napi(getter)]
pub fn index(&self) -> napi::Result<Either<u32, ()>> {
let (_, chunk_graph) = self.as_ref()?;
Ok(match chunk_graph.index {
Some(index) => Either::A(index),
None => Either::B(()),
})
}
#[napi(getter)]
pub fn name(&self) -> napi::Result<Either<&str, ()>> {
let (_, chunk_graph) = self.as_ref()?;
Ok(match chunk_graph.name() {
Some(name) => Either::A(name),
None => Either::B(()),
})
}
#[napi(getter)]
pub fn origins<'a>(&self, env: &'a Env) -> napi::Result<Vec<JsChunkGroupOrigin<'a>>> {
let (compilation, chunk_graph) = self.as_ref()?;
let origins = chunk_graph.origins();
let mut js_origins = Vec::with_capacity(origins.len());
for origin in origins {
let loc = if let Some(loc) = &origin.loc {
Some(match loc {
rspack_core::DependencyLocation::Real(real) => Either::B(real.into()),
rspack_core::DependencyLocation::Synthetic(synthetic) => {
Either::A(env.create_string(&synthetic.name)?)
}
})
} else {
None
};
js_origins.push(JsChunkGroupOrigin {
module: origin.module.and_then(|module_id| {
compilation
.module_by_identifier(&module_id)
.map(|module| ModuleObject::with_ref(module.as_ref(), compilation.compiler_id()))
}),
request: match &origin.request {
Some(request) => Some(env.create_string(request)?),
None => None,
},
loc,
})
}
Ok(js_origins)
}
#[napi(getter, ts_return_type = "ChunkGroup[]")]
pub fn children_iterable(&self) -> napi::Result<Vec<ChunkGroupWrapper>> {
let (compilation, chunk_graph) = self.as_ref()?;
Ok(
chunk_graph
.children_iterable()
.map(|ukey| ChunkGroupWrapper::new(*ukey, compilation))
.collect::<Vec<_>>(),
)
}
#[napi]
pub fn is_initial(&self) -> napi::Result<bool> {
let (_, chunk_group) = self.as_ref()?;
Ok(chunk_group.is_initial())
}
#[napi(ts_return_type = "ChunkGroup[]")]
pub fn get_parents(&self) -> napi::Result<Vec<ChunkGroupWrapper>> {
let (compilation, chunk_group) = self.as_ref()?;
Ok(
chunk_group
.parents
.iter()
.map(|ukey| ChunkGroupWrapper::new(*ukey, compilation))
.collect(),
)
}
#[napi(ts_return_type = "Chunk")]
pub fn get_runtime_chunk(&self) -> napi::Result<ChunkWrapper> {
let (compilation, chunk_group) = self.as_ref()?;
let chunk_ukey = chunk_group.get_runtime_chunk(&compilation.chunk_group_by_ukey);
Ok(ChunkWrapper::new(chunk_ukey, compilation))
}
#[napi(ts_return_type = "Chunk")]
pub fn get_entrypoint_chunk(&self) -> napi::Result<ChunkWrapper> {
let (compilation, chunk_group) = self.as_ref()?;
let chunk_ukey = chunk_group.get_entrypoint_chunk();
Ok(ChunkWrapper::new(chunk_ukey, compilation))
}
#[napi]
pub fn get_files(&self) -> napi::Result<Vec<&String>> {
let (compilation, chunk_group) = self.as_ref()?;
Ok(
chunk_group
.chunks
.iter()
.filter_map(|chunk_ukey| {
compilation
.chunk_by_ukey
.get(chunk_ukey)
.map(|chunk| chunk.files().iter())
})
.flatten()
.collect::<Vec<_>>(),
)
}
#[napi(ts_args_type = "module: Module")]
pub fn get_module_pre_order_index(&self, module: ModuleObjectRef) -> napi::Result<Option<u32>> {
let (_, chunk_group) = self.as_ref()?;
Ok(
chunk_group
.module_pre_order_index(&module.identifier)
.map(|v| v as u32),
)
}
#[napi(ts_args_type = "module: Module")]
pub fn get_module_post_order_index(&self, module: ModuleObjectRef) -> napi::Result<Option<u32>> {
let (_, chunk_group) = self.as_ref()?;
Ok(
chunk_group
.module_post_order_index(&module.identifier)
.map(|v| v as u32),
)
}
}
thread_local! {
static CHUNK_GROUP_INSTANCE_REFS: RefCell<UkeyMap<CompilationId, UkeyMap<rspack_core::ChunkGroupUkey, OneShotRef>>> = Default::default();
}
pub struct ChunkGroupWrapper {
chunk_group_ukey: rspack_core::ChunkGroupUkey,
compilation_id: CompilationId,
compilation: NonNull<Compilation>,
}
impl ChunkGroupWrapper {
pub fn new(chunk_group_ukey: rspack_core::ChunkGroupUkey, compilation: &Compilation) -> Self {
#[allow(clippy::unwrap_used)]
Self {
chunk_group_ukey,
compilation_id: compilation.id(),
compilation: NonNull::new(compilation as *const Compilation as *mut Compilation).unwrap(),
}
}
pub fn cleanup_last_compilation(compilation_id: CompilationId) {
CHUNK_GROUP_INSTANCE_REFS.with(|refs| {
let mut refs_by_compilation_id = refs.borrow_mut();
refs_by_compilation_id.remove(&compilation_id)
});
}
}
impl ToNapiValue for ChunkGroupWrapper {
unsafe fn to_napi_value(
env: napi::sys::napi_env,
val: Self,
) -> napi::Result<napi::sys::napi_value> {
unsafe {
CHUNK_GROUP_INSTANCE_REFS.with(|refs| {
let mut refs_by_compilation_id = refs.borrow_mut();
let entry = refs_by_compilation_id.entry(val.compilation_id);
let refs = match entry {
std::collections::hash_map::Entry::Occupied(entry) => entry.into_mut(),
std::collections::hash_map::Entry::Vacant(entry) => {
let refs = UkeyMap::default();
entry.insert(refs)
}
};
match refs.entry(val.chunk_group_ukey) {
std::collections::hash_map::Entry::Occupied(entry) => {
let r = entry.get();
ToNapiValue::to_napi_value(env, r)
}
std::collections::hash_map::Entry::Vacant(entry) => {
let js_module = ChunkGroup {
chunk_group_ukey: val.chunk_group_ukey,
compilation: val.compilation,
};
let r = entry.insert(OneShotRef::new(env, js_module)?);
ToNapiValue::to_napi_value(env, r)
}
}
})
}
}
}
#[napi(object, object_from_js = false)]
pub struct JsChunkGroupOrigin<'a> {
#[napi(ts_type = "Module | undefined")]
pub module: Option<ModuleObject>,
pub request: Option<JsString<'a>>,
pub loc: Option<Either<JsString<'a>, RealDependencyLocation>>,
}