/
mod.rs
198 lines (167 loc) · 5.41 KB
/
mod.rs
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
use crate::{
complete_output, get_compiler,
util::{CtxtExt, MapErr},
};
use anyhow::{anyhow, bail, Context, Error};
use napi::{CallContext, JsObject, Task};
use once_cell::sync::Lazy;
use serde::Deserialize;
use std::{collections::HashMap, path::PathBuf, sync::Arc};
use swc::{config::SourceMapsConfig, try_with_handler, TransformOutput};
use swc_atoms::JsWord;
use swc_bundler::{Bundler, ModuleData, ModuleRecord};
use swc_common::{collections::AHashMap, errors::Handler, BytePos, FileName, SourceMap, Span};
use swc_ecma_loader::{
resolvers::{lru::CachingResolver, node::NodeModulesResolver},
TargetEnv, NODE_BUILTINS,
};
use swc_ecmascript::{
ast::*,
parser::{lexer::Lexer, EsConfig, Parser, StringInput, Syntax},
visit::{noop_visit_type, Visit, VisitWith},
};
#[js_function(1)]
pub fn bundle(cx: CallContext) -> napi::Result<JsObject> {
let option = cx.get_buffer_as_string(0)?;
let task = BundleTask {
c: get_compiler(&cx),
config: option,
};
cx.env.spawn(task).map(|t| t.promise_object())
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
struct BundleOption {
entry: PathBuf,
}
struct BundleTask {
c: Arc<swc::Compiler>,
config: String,
}
impl Task for BundleTask {
type Output = TransformOutput;
type JsValue = JsObject;
fn compute(&mut self) -> napi::Result<Self::Output> {
let option: BundleOption = crate::util::deserialize_json(&self.config).convert_err()?;
try_with_handler(self.c.cm.clone(), true, |handler| {
let builtins = NODE_BUILTINS
.iter()
.copied()
.map(JsWord::from)
.collect::<Vec<_>>();
let comments = self.c.comments().clone();
//
let mut bundler = Bundler::new(
self.c.globals(),
self.c.cm.clone(),
CustomLoader {
cm: self.c.cm.clone(),
handler,
},
make_resolver(),
swc_bundler::Config {
require: true,
disable_inliner: false,
external_modules: builtins,
module: swc_bundler::ModuleType::Es,
..Default::default()
},
Box::new(CustomHook),
);
let mut entries = HashMap::default();
let path: PathBuf = option.entry;
let path = path
.canonicalize()
.context("failed to canonicalize entry file")?;
entries.insert("main".to_string(), FileName::Real(path));
let outputs = bundler.bundle(entries)?;
let output = outputs
.into_iter()
.next()
.ok_or_else(|| anyhow!("swc_bundler::Bundle::bundle returned empty result"))?;
let source_map_names = {
let mut v = SourceMapIdentCollector {
names: Default::default(),
};
output.module.visit_with(&mut v);
v.names
};
let code = self.c.print(
&output.module,
None,
None,
true,
EsVersion::Es5,
SourceMapsConfig::Bool(true),
&source_map_names,
None,
false,
Some(&comments),
)?;
Ok(code)
})
.convert_err()
}
fn resolve(self, env: napi::Env, output: Self::Output) -> napi::Result<Self::JsValue> {
complete_output(&env, output, Default::default())
}
}
type Resolver = Arc<CachingResolver<NodeModulesResolver>>;
fn make_resolver() -> Resolver {
static CACHE: Lazy<Resolver> = Lazy::new(|| {
// TODO: Make target env and alias configurable
let r = NodeModulesResolver::new(TargetEnv::Node, Default::default(), true);
let r = CachingResolver::new(256, r);
Arc::new(r)
});
(*CACHE).clone()
}
struct CustomLoader<'a> {
handler: &'a Handler,
cm: Arc<SourceMap>,
}
impl swc_bundler::Load for CustomLoader<'_> {
fn load(&self, f: &FileName) -> Result<ModuleData, Error> {
let fm = match f {
FileName::Real(path) => self.cm.load_file(path)?,
_ => unreachable!(),
};
let lexer = Lexer::new(
Syntax::Es(EsConfig {
..Default::default()
}),
EsVersion::Es2020,
StringInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let module = parser.parse_module().map_err(|err| {
err.into_diagnostic(self.handler).emit();
anyhow!("failed to parse")
})?;
Ok(ModuleData {
fm,
module,
helpers: Default::default(),
})
}
}
struct CustomHook;
impl swc_bundler::Hook for CustomHook {
fn get_import_meta_props(
&self,
_span: Span,
_module_record: &ModuleRecord,
) -> Result<Vec<KeyValueProp>, Error> {
bail!("`import.meta` is not supported yet")
}
}
pub struct SourceMapIdentCollector {
names: AHashMap<BytePos, JsWord>,
}
impl Visit for SourceMapIdentCollector {
noop_visit_type!();
fn visit_ident(&mut self, ident: &Ident) {
self.names.insert(ident.span.lo, ident.sym.clone());
}
}