/
mod.rs
337 lines (284 loc) · 10.8 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use std::{
env::current_dir,
fs::{create_dir_all, read_to_string, write},
io::Write,
path::{Path, PathBuf},
};
use anyhow::{bail, Context, Error};
use path_clean::PathClean;
use reqwest::Url;
use sha1::{Digest, Sha1};
use swc_bundler::{Load, ModuleData, Resolve};
use swc_common::{
comments::SingleThreadedComments,
errors::{ColorConfig, Handler},
sync::Lrc,
FileName, Mark, SourceMap,
};
use swc_ecma_ast::EsVersion;
use swc_ecma_parser::{parse_file_as_module, Syntax, TsConfig};
use swc_ecma_transforms_base::{
helpers::{inject_helpers, Helpers, HELPERS},
resolver,
};
use swc_ecma_transforms_proposal::decorators;
use swc_ecma_transforms_react::react;
use swc_ecma_transforms_typescript::strip;
use swc_ecma_visit::FoldWith;
pub struct Loader {
pub cm: Lrc<SourceMap>,
}
fn calc_hash(s: &str) -> String {
let mut hasher = Sha1::new();
hasher.update(s.as_bytes());
let sum = hasher.finalize();
hex::encode(sum)
}
fn calc_cache_path(cache_dir: &Path, url: &Url) -> PathBuf {
let hash = calc_hash(url.as_ref());
let s = url.to_string();
if s.starts_with("https://deno.land/") {
return cache_dir.join("deno").join(&hash);
}
cache_dir.join("untrusted").join(hash)
}
/// Load url. This method does caching.
fn load_url(url: Url) -> Result<String, Error> {
let cache_dir = current_dir()
.expect("the test requires an environment variable named `CARGO_MANIFEST_DIR`")
.join("tests")
.join(".cache");
let cache_path = calc_cache_path(&cache_dir, &url).with_extension("ts");
create_dir_all(cache_path.parent().unwrap()).context("failed to create cache dir")?;
if let Ok(v) = read_to_string(&cache_path) {
return Ok(v);
}
if let Ok("1") = std::env::var("CI").as_deref() {
panic!("The bundler test suite should not download files from CI")
}
eprintln!("Storing `{}` at `{}`", url, cache_path.display());
let resp = reqwest::blocking::get(url.clone())
.with_context(|| format!("failed to fetch `{}`", url))?;
let bytes = resp
.bytes()
.with_context(|| format!("failed to read data from `{}`", url))?;
let mut content = vec![];
write!(content, "// Loaded from {}\n\n\n", url).unwrap();
content.extend_from_slice(&bytes);
write(&cache_path, &content)?;
return Ok(String::from_utf8_lossy(&bytes).to_string());
}
impl Load for Loader {
fn load(&self, f: &FileName) -> Result<ModuleData, Error> {
eprintln!("load: {}", f);
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
let tsx;
let fm = match f {
FileName::Real(path) => {
tsx = path.to_string_lossy().ends_with(".tsx");
self.cm.load_file(path)?
}
FileName::Custom(url) => {
tsx = url.ends_with(".tsx");
// Hack for jszip, which depends on invalid url.
let url = url.replace("https://deno.land/std@v", "https://deno.land/std@");
let url = Url::parse(&url).context("failed to parse url")?;
let src = load_url(url.clone())?;
self.cm
.new_source_file(FileName::Custom(url.to_string()), src)
}
_ => unreachable!(),
};
let module = parse_file_as_module(
&fm,
Syntax::Typescript(TsConfig {
decorators: true,
tsx,
..Default::default()
}),
EsVersion::Es2020,
None,
&mut vec![],
)
.unwrap_or_else(|err| {
let handler =
Handler::with_tty_emitter(ColorConfig::Always, false, false, Some(self.cm.clone()));
err.into_diagnostic(&handler).emit();
panic!("failed to parse")
});
let module = HELPERS.set(&Helpers::new(false), || {
module
.fold_with(&mut resolver(unresolved_mark, top_level_mark, false))
.fold_with(&mut decorators(decorators::Config {
legacy: true,
emit_metadata: Default::default(),
use_define_for_class_fields: false,
}))
.fold_with(&mut strip(top_level_mark))
.fold_with(&mut react::<SingleThreadedComments>(
self.cm.clone(),
None,
Default::default(),
top_level_mark,
unresolved_mark,
))
.fold_with(&mut inject_helpers(unresolved_mark))
});
Ok(ModuleData {
fm,
module,
helpers: Default::default(),
})
}
}
pub struct NodeResolver;
static EXTENSIONS: &[&str] = &["ts", "tsx", "js", "jsx", "json", "node"];
impl NodeResolver {
fn wrap(&self, path: PathBuf) -> Result<FileName, Error> {
let path = path.clean();
Ok(FileName::Real(path))
}
/// Resolve a path as a file. If `path` refers to a file, it is
/// returned; otherwise the `path` + each extension is tried.
fn resolve_as_file(&self, path: &Path) -> Result<PathBuf, Error> {
// 1. If X is a file, load X as JavaScript text.
if path.is_file() {
return Ok(path.to_path_buf());
}
if let Some(name) = path.file_name() {
let mut ext_path = path.to_path_buf();
let name = name.to_string_lossy();
for ext in EXTENSIONS {
ext_path.set_file_name(format!("{}.{}", name, ext));
if ext_path.is_file() {
return Ok(ext_path);
}
}
// TypeScript-specific behavior: if the extension is ".js" or ".jsx",
// try replacing it with ".ts" or ".tsx".
ext_path.set_file_name(name.into_owned());
let old_ext = path.extension().and_then(|ext| ext.to_str());
if let Some(old_ext) = old_ext {
let extensions = match old_ext {
// Note that the official compiler code always tries ".ts" before
// ".tsx" even if the original extension was ".jsx".
"js" => ["ts", "tsx"].as_slice(),
"jsx" => ["ts", "tsx"].as_slice(),
"mjs" => ["mts"].as_slice(),
"cjs" => ["cts"].as_slice(),
_ => [].as_slice(),
};
for ext in extensions {
ext_path.set_extension(ext);
if ext_path.is_file() {
return Ok(ext_path);
}
}
}
}
bail!("file not found: {}", path.display())
}
/// Resolve a path as a directory, using the "main" key from a
/// package.json file if it exists, or resolving to the
/// index.EXT file if it exists.
fn resolve_as_directory(&self, path: &Path) -> Result<PathBuf, Error> {
// 1. If X/package.json is a file, use it.
let pkg_path = path.join("package.json");
if pkg_path.is_file() {
let main = self.resolve_package_main(&pkg_path);
if main.is_ok() {
return main;
}
}
// 2. LOAD_INDEX(X)
self.resolve_index(path)
}
/// Resolve using the package.json "main" key.
fn resolve_package_main(&self, _: &Path) -> Result<PathBuf, Error> {
bail!("package.json is not supported")
}
/// Resolve a directory to its index.EXT.
fn resolve_index(&self, path: &Path) -> Result<PathBuf, Error> {
// 1. If X/index.js is a file, load X/index.js as JavaScript text.
// 2. If X/index.json is a file, parse X/index.json to a JavaScript object.
// 3. If X/index.node is a file, load X/index.node as binary addon.
for ext in EXTENSIONS {
let ext_path = path.join(format!("index.{}", ext));
if ext_path.is_file() {
return Ok(ext_path);
}
}
bail!("index not found: {}", path.display())
}
/// Resolve by walking up node_modules folders.
fn resolve_node_modules(&self, base_dir: &Path, target: &str) -> Result<PathBuf, Error> {
let node_modules = base_dir.join("node_modules");
if node_modules.is_dir() {
let path = node_modules.join(target);
let result = self
.resolve_as_file(&path)
.or_else(|_| self.resolve_as_directory(&path));
if result.is_ok() {
return result;
}
}
match base_dir.parent() {
Some(parent) => self.resolve_node_modules(parent, target),
None => bail!("not found"),
}
}
}
impl Resolve for NodeResolver {
fn resolve(&self, base: &FileName, target: &str) -> Result<FileName, Error> {
if let Ok(v) = Url::parse(target) {
return Ok(FileName::Custom(v.to_string()));
}
let base = match base {
FileName::Real(v) => v,
FileName::Custom(base_url) => {
let base_url = Url::parse(base_url).context("failed to parse url")?;
let options = Url::options();
let base_url = options.base_url(Some(&base_url));
let url = base_url
.parse(target)
.with_context(|| format!("failed to resolve `{}`", target))?;
return Ok(FileName::Custom(url.to_string()));
}
_ => bail!("node-resolver supports only files"),
};
// Absolute path
if target.starts_with('/') {
let base_dir = &Path::new("/");
let path = base_dir.join(target);
return self
.resolve_as_file(&path)
.or_else(|_| self.resolve_as_directory(&path))
.and_then(|p| self.wrap(p));
}
let cwd = &Path::new(".");
let mut base_dir = base.parent().unwrap_or(cwd);
if target.starts_with("./") || target.starts_with("../") {
let win_target;
let target = if cfg!(target_os = "windows") {
let t = if let Some(s) = target.strip_prefix("./") {
s
} else {
base_dir = base_dir.parent().unwrap();
&target[3..]
};
win_target = t.replace('/', "\\");
&*win_target
} else {
target
};
let path = base_dir.join(target);
return self
.resolve_as_file(&path)
.or_else(|_| self.resolve_as_directory(&path))
.and_then(|p| self.wrap(p));
}
self.resolve_node_modules(base_dir, target)
.and_then(|p| self.wrap(p))
}
}