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
use std::collections::HashMap;
use anyhow::{Context, Error};
use swc_atoms::JsWord;
use swc_common::{
collections::AHashMap, sync::Lrc, FileName, Globals, Mark, SourceMap, SyntaxContext, GLOBALS,
};
use swc_ecma_ast::Module;
use self::scope::Scope;
use crate::{Hook, Load, ModuleId, Resolve};
mod chunk;
mod export;
mod finalize;
mod helpers;
mod import;
mod keywords;
mod load;
mod optimize;
mod scope;
#[cfg(test)]
pub(crate) mod tests;
#[derive(Debug, Default)]
pub struct Config {
pub require: bool,
pub disable_inliner: bool,
pub disable_hygiene: bool,
pub disable_fixer: bool,
pub disable_dce: bool,
pub external_modules: Vec<JsWord>,
pub module: ModuleType,
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ModuleType {
Es,
Iife,
}
impl Default for ModuleType {
fn default() -> Self {
ModuleType::Es
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum BundleKind {
Named { name: String },
Dynamic,
Lib { name: String },
}
#[derive(Debug)]
pub struct Bundle {
pub kind: BundleKind,
pub id: ModuleId,
pub module: Module,
}
pub struct Bundler<'a, L, R>
where
L: Load,
R: Resolve,
{
config: Config,
unresolved_mark: Mark,
globals: &'a Globals,
cm: Lrc<SourceMap>,
loader: L,
resolver: R,
_helper_ctxt: SyntaxContext,
synthesized_ctxt: SyntaxContext,
pub(crate) injected_ctxt: SyntaxContext,
scope: Scope,
hook: Box<dyn 'a + Hook>,
}
impl<'a, L, R> Bundler<'a, L, R>
where
L: Load,
R: Resolve,
{
pub fn new(
globals: &'a Globals,
cm: Lrc<SourceMap>,
loader: L,
resolver: R,
config: Config,
hook: Box<dyn 'a + Hook>,
) -> Self {
GLOBALS.set(globals, || {
let helper_ctxt = SyntaxContext::empty().apply_mark(Mark::fresh(Mark::root()));
tracing::debug!("Helper ctxt: {:?}", helper_ctxt);
let synthesized_ctxt = SyntaxContext::empty().apply_mark(Mark::fresh(Mark::root()));
tracing::debug!("Synthesized ctxt: {:?}", synthesized_ctxt);
let injected_ctxt = SyntaxContext::empty().apply_mark(Mark::fresh(Mark::root()));
tracing::debug!("Injected ctxt: {:?}", injected_ctxt);
Bundler {
config,
globals,
cm,
loader,
resolver,
_helper_ctxt: helper_ctxt,
synthesized_ctxt,
injected_ctxt,
scope: Default::default(),
hook,
unresolved_mark: Mark::new(),
}
})
}
pub(crate) fn is_external(&self, src: &JsWord) -> bool {
return self.config.external_modules.iter().any(|v| v == src);
}
pub fn bundle(&mut self, entries: HashMap<String, FileName>) -> Result<Vec<Bundle>, Error> {
let results = entries
.into_iter()
.map(|(name, path)| -> Result<_, Error> {
let path = match path {
FileName::Real(path) => {
if cfg!(target_os = "windows") {
let path = path
.canonicalize()
.context("failed to canonicalize entry")?;
FileName::Real(path)
} else {
FileName::Real(path)
}
}
_ => path,
};
let res = self
.load_transformed(&path)
.context("load_transformed failed")?;
Ok((name, res))
})
.collect::<Vec<_>>();
let local = {
let mut output = AHashMap::default();
for res in results {
let (name, m) = res?;
let m = m.unwrap();
output.insert(name, m);
}
output
};
let bundles = self.chunk(local)?;
let bundles = self.finalize(bundles)?;
#[cfg(feature = "concurrent")]
{
let scope = std::mem::take(&mut self.scope);
rayon::spawn(move || drop(scope))
}
Ok(bundles)
}
#[inline]
fn run<F, Ret>(&self, op: F) -> Ret
where
F: FnOnce() -> Ret,
{
GLOBALS.set(self.globals, op)
}
}