-
-
Notifications
You must be signed in to change notification settings - Fork 805
Expand file tree
/
Copy pathentries.rs
More file actions
320 lines (281 loc) · 8.27 KB
/
entries.rs
File metadata and controls
320 lines (281 loc) · 8.27 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use napi_derive::napi;
use rspack_core::{ChunkLoading, Compilation, EntryData, EntryOptions, EntryRuntime};
use rspack_napi::napi::bindgen_prelude::*;
use crate::{
dependency::{Dependency, DependencyWrapper},
options::{entry::JsEntryOptions, library::JsLibraryOptions},
raw_options::{RawChunkLoading, WithFalse},
};
#[napi]
pub struct EntryOptionsDTO(EntryOptions);
impl EntryOptionsDTO {
pub fn new(options: EntryOptions) -> Self {
Self(options)
}
}
#[napi]
impl EntryOptionsDTO {
#[napi(getter)]
pub fn name(&self) -> Either<&str, ()> {
self.0.name.as_ref().map(|s| s.as_ref()).into()
}
#[napi(setter)]
pub fn set_name(&mut self, name: Either<String, ()>) {
self.0.name = match name {
Either::A(s) => Some(s.into()),
Either::B(_) => None,
};
}
#[napi(getter, ts_return_type = "false | string | undefined")]
pub fn runtime(&self) -> Either3<bool, &String, ()> {
match &self.0.runtime {
Some(rt) => match rt {
EntryRuntime::String(s) => Either3::B(s),
EntryRuntime::False => Either3::A(false),
},
None => Either3::C(()),
}
}
#[napi(setter)]
pub fn set_runtime(&mut self, chunk_loading: Either3<bool, String, ()>) {
self.0.chunk_loading = match chunk_loading {
Either3::A(_) => Some(ChunkLoading::Disable),
Either3::B(s) => Some(ChunkLoading::Enable(s.as_str().into())),
Either3::C(_) => None,
};
}
#[napi(getter)]
pub fn chunk_loading(&self) -> Either<&str, ()> {
match &self.0.chunk_loading {
Some(c) => Either::A(c.as_str()),
None => Either::B(()),
}
}
#[napi(setter, ts_type = "(chunkLoading: string | false | undefined)")]
pub fn set_chunk_loading(&mut self, chunk_loading: Either<RawChunkLoading, ()>) {
match chunk_loading {
Either::A(WithFalse::False) => self.0.chunk_loading = Some(ChunkLoading::Disable),
Either::A(WithFalse::True(s)) => {
self.0.chunk_loading = Some(ChunkLoading::Enable(s.as_str().into()))
}
Either::B(_) => self.0.chunk_loading = None,
}
}
#[napi(getter)]
pub fn async_chunks(&self) -> Either<bool, ()> {
self.0.async_chunks.into()
}
#[napi(setter)]
pub fn set_async_chunks(&mut self, async_chunks: Either<bool, ()>) {
self.0.async_chunks = match async_chunks {
Either::A(b) => Some(b),
Either::B(_) => None,
};
}
#[napi(getter)]
pub fn base_uri(&self) -> Either<&String, ()> {
self.0.base_uri.as_ref().into()
}
#[napi(setter)]
pub fn set_base_uri(&mut self, base_uri: Either<String, ()>) {
self.0.base_uri = match base_uri {
Either::A(s) => Some(s),
Either::B(_) => None,
};
}
#[napi(getter)]
pub fn library(&self) -> Either<JsLibraryOptions, ()> {
self.0.library.clone().map(Into::into).into()
}
#[napi(setter)]
pub fn set_library(&mut self, library: Either<JsLibraryOptions, ()>) {
self.0.library = match library {
Either::A(l) => Some(l.into()),
Either::B(_) => None,
};
}
#[napi(getter)]
pub fn depend_on(&self) -> Either<&Vec<String>, ()> {
self.0.depend_on.as_ref().into()
}
#[napi(setter)]
pub fn set_depend_on(&mut self, depend_on: Either<Vec<String>, ()>) {
self.0.depend_on = match depend_on {
Either::A(vec) => Some(vec),
Either::B(_) => None,
};
}
#[napi(getter)]
pub fn layer(&self) -> Either<&String, ()> {
self.0.layer.as_ref().into()
}
#[napi(setter)]
pub fn set_layer(&mut self, layer: Either<String, ()>) {
self.0.layer = match layer {
Either::A(s) => Some(s),
Either::B(_) => None,
};
}
// #[napi(getter)]
// pub fn public_path(&self) -> Either3<String, JsFunction, ()> {
// unimplemented!()
// }
// #[napi(setter)]
// pub fn set_public_path(&self, _public_path: Option<Either<String, JsFunction>>) {
// unimplemented!()
// }
// #[napi(getter)]
// pub fn filename(&self) -> Either3<String, JsFunction, ()> {
// unimplemented!()
// }
// #[napi(setter)]
// pub fn set_filename(&self, _filename: Option<Either<String, JsFunction>>) {
// unimplemented!()
// }
}
#[napi(object, object_to_js = false)]
pub struct JsEntryData {
pub dependencies: Vec<ClassInstance<'static, Dependency>>,
pub include_dependencies: Vec<ClassInstance<'static, Dependency>>,
pub options: JsEntryOptions,
}
impl From<JsEntryData> for EntryData {
fn from(value: JsEntryData) -> Self {
Self {
dependencies: value
.dependencies
.into_iter()
.map(|dep| dep.dependency_id)
.collect::<Vec<_>>(),
include_dependencies: value
.include_dependencies
.into_iter()
.map(|dep| dep.dependency_id)
.collect::<Vec<_>>(),
options: value.options.into(),
}
}
}
#[napi]
pub struct EntryDataDTO {
compilation: &'static mut Compilation,
entry_data: EntryData,
}
#[napi]
impl EntryDataDTO {
#[napi(getter, ts_return_type = "Dependency[]")]
pub fn dependencies(&'static self) -> Vec<DependencyWrapper> {
let module_graph = self.compilation.get_module_graph();
self
.entry_data
.dependencies
.iter()
.map(|dependency_id| {
let dep = module_graph.dependency_by_id(dependency_id);
DependencyWrapper::new(dep.as_ref(), self.compilation.id(), Some(self.compilation))
})
.collect::<Vec<_>>()
}
#[napi(getter, ts_return_type = "Dependency[]")]
pub fn include_dependencies(&'static self) -> Vec<DependencyWrapper> {
let module_graph = self.compilation.get_module_graph();
self
.entry_data
.include_dependencies
.iter()
.map(|dependency_id| {
let dep = module_graph.dependency_by_id(dependency_id);
DependencyWrapper::new(dep.as_ref(), self.compilation.id(), Some(self.compilation))
})
.collect::<Vec<_>>()
}
#[napi(getter)]
pub fn options<'scope>(
&self,
env: &'scope Env,
) -> Result<ClassInstance<'scope, EntryOptionsDTO>> {
EntryOptionsDTO::new(self.entry_data.options.clone()).into_instance(env)
}
}
#[napi]
pub struct JsEntries {
compilation: &'static mut Compilation,
}
impl JsEntries {
pub fn new(compilation: &'static mut Compilation) -> Self {
Self { compilation }
}
}
#[napi]
impl JsEntries {
#[napi]
pub fn clear(&mut self) {
self.compilation.entries.drain(..);
}
#[napi(getter)]
pub fn size(&mut self) -> u32 {
self.compilation.entries.len() as u32
}
#[napi]
pub fn has(&self, key: String) -> bool {
self.compilation.entries.contains_key(key.as_str())
}
#[napi]
pub fn set(&mut self, key: String, value: Either<JsEntryData, ClassInstance<EntryDataDTO>>) {
let entry_data = match value {
Either::A(js) => js.into(),
Either::B(dto) => {
assert!(
std::ptr::eq(dto.compilation, self.compilation),
"The set() method cannot accept entry data from a different compilation instance."
);
dto.entry_data.clone()
}
};
self.compilation.entries.insert(key.into(), entry_data);
}
#[napi]
pub fn delete(&mut self, key: String) -> bool {
let r = self.compilation.entries.swap_remove(key.as_str());
r.is_some()
}
#[napi]
pub fn get(&'static mut self, key: String) -> Result<Either<EntryDataDTO, ()>> {
Ok(match self.compilation.entries.get(key.as_str()) {
Some(entry_data) => Either::A(EntryDataDTO {
entry_data: entry_data.clone(),
compilation: self.compilation,
}),
None => Either::B(()),
})
}
#[napi]
pub fn keys(&self) -> Vec<&str> {
self
.compilation
.entries
.keys()
.map(|s| s.as_ref())
.collect()
}
#[napi]
pub fn values(&'static self) -> Vec<EntryDataDTO> {
self
.compilation
.entries
.values()
.cloned()
.map(|value| {
// To resolve the lifetime issue, `&'static self` is converted to `&'static mut self`.
// Since JS is single-threaded, data races theoretically should not occur, making this safe.
// However, this approach is highly hacky. It is recommended to look for a better solution in the future.
let compilation_ptr = self.compilation as *const Compilation as *mut Compilation;
let compilation = unsafe { &mut *compilation_ptr };
EntryDataDTO {
entry_data: value,
compilation,
}
})
.collect()
}
}