2020-12-02 16:48:35 +03:00
|
|
|
use std::{
|
2021-08-13 13:03:04 +03:00
|
|
|
env::current_dir,
|
2020-12-02 16:48:35 +03:00
|
|
|
fs::{create_dir_all, read_to_string, write},
|
2021-08-13 13:03:04 +03:00
|
|
|
io::Write,
|
2020-12-02 16:48:35 +03:00
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
|
|
|
use anyhow::{bail, Context, Error};
|
|
|
|
use path_clean::PathClean;
|
|
|
|
use reqwest::Url;
|
|
|
|
use sha1::{Digest, Sha1};
|
2020-12-02 16:48:35 +03:00
|
|
|
use swc_bundler::{Load, ModuleData, Resolve};
|
2021-08-13 13:03:04 +03:00
|
|
|
use swc_common::{
|
|
|
|
comments::SingleThreadedComments,
|
|
|
|
errors::{ColorConfig, Handler},
|
|
|
|
sync::Lrc,
|
2021-10-07 09:13:21 +03:00
|
|
|
FileName, Mark, SourceMap,
|
2021-08-13 13:03:04 +03:00
|
|
|
};
|
2021-10-31 15:25:15 +03:00
|
|
|
use swc_ecma_ast::EsVersion;
|
2022-02-26 09:41:14 +03:00
|
|
|
use swc_ecma_parser::{parse_file_as_module, Syntax, TsConfig};
|
2022-03-20 10:53:53 +03:00
|
|
|
use swc_ecma_transforms_base::{
|
|
|
|
helpers::{inject_helpers, Helpers, HELPERS},
|
2022-04-26 10:38:50 +03:00
|
|
|
resolver,
|
2022-03-20 10:53:53 +03:00
|
|
|
};
|
|
|
|
use swc_ecma_transforms_proposal::decorators;
|
2021-10-09 11:08:13 +03:00
|
|
|
use swc_ecma_transforms_react::react;
|
|
|
|
use swc_ecma_transforms_typescript::strip;
|
2020-12-02 16:48:35 +03:00
|
|
|
use swc_ecma_visit::FoldWith;
|
2021-10-09 11:08:13 +03:00
|
|
|
|
2020-12-02 16:48:35 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-03-02 11:33:03 +03:00
|
|
|
fn calc_cache_path(cache_dir: &Path, url: &Url) -> PathBuf {
|
2022-02-08 13:50:22 +03:00
|
|
|
let hash = calc_hash(url.as_ref());
|
2021-03-02 11:33:03 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-12-02 16:48:35 +03:00
|
|
|
/// Load url. This method does caching.
|
|
|
|
fn load_url(url: Url) -> Result<String, Error> {
|
2022-01-13 17:06:11 +03:00
|
|
|
let cache_dir = current_dir()
|
|
|
|
.expect("the test requires an environment variable named `CARGO_MANIFEST_DIR`")
|
|
|
|
.join("tests")
|
|
|
|
.join(".cache");
|
2020-12-02 16:48:35 +03:00
|
|
|
|
2021-03-02 11:33:03 +03:00
|
|
|
let cache_path = calc_cache_path(&cache_dir, &url).with_extension("ts");
|
2020-12-02 16:48:35 +03:00
|
|
|
|
2021-03-02 11:33:03 +03:00
|
|
|
create_dir_all(cache_path.parent().unwrap()).context("failed to create cache dir")?;
|
2020-12-02 16:48:35 +03:00
|
|
|
|
2022-01-15 10:43:34 +03:00
|
|
|
if let Ok(v) = read_to_string(&cache_path) {
|
|
|
|
return Ok(v);
|
2020-12-02 16:48:35 +03:00
|
|
|
}
|
|
|
|
|
2021-03-22 13:42:42 +03:00
|
|
|
if let Ok("1") = std::env::var("CI").as_deref() {
|
|
|
|
panic!("The bundler test suite should not download files from CI")
|
|
|
|
}
|
|
|
|
|
2021-03-02 11:33:03 +03:00
|
|
|
eprintln!("Storing `{}` at `{}`", url, cache_path.display());
|
|
|
|
|
2020-12-02 16:48:35 +03:00
|
|
|
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))?;
|
|
|
|
|
2021-03-02 11:33:03 +03:00
|
|
|
let mut content = vec![];
|
|
|
|
write!(content, "// Loaded from {}\n\n\n", url).unwrap();
|
|
|
|
content.extend_from_slice(&bytes);
|
|
|
|
|
|
|
|
write(&cache_path, &content)?;
|
2020-12-02 16:48:35 +03:00
|
|
|
|
|
|
|
return Ok(String::from_utf8_lossy(&bytes).to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Load for Loader {
|
|
|
|
fn load(&self, f: &FileName) -> Result<ModuleData, Error> {
|
|
|
|
eprintln!("load: {}", f);
|
|
|
|
|
2022-04-26 10:38:50 +03:00
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
2021-10-07 09:13:21 +03:00
|
|
|
|
2020-12-02 16:48:35 +03:00
|
|
|
let tsx;
|
|
|
|
let fm = match f {
|
|
|
|
FileName::Real(path) => {
|
|
|
|
tsx = path.to_string_lossy().ends_with(".tsx");
|
2022-01-13 17:06:11 +03:00
|
|
|
self.cm.load_file(path)?
|
2020-12-02 16:48:35 +03:00
|
|
|
}
|
|
|
|
FileName::Custom(url) => {
|
|
|
|
tsx = url.ends_with(".tsx");
|
2021-01-11 13:38:20 +03:00
|
|
|
// Hack for jszip, which depends on invalid url.
|
|
|
|
let url = url.replace("https://deno.land/std@v", "https://deno.land/std@");
|
2020-12-02 16:48:35 +03:00
|
|
|
|
|
|
|
let url = Url::parse(&url).context("failed to parse url")?;
|
|
|
|
|
|
|
|
let src = load_url(url.clone())?;
|
|
|
|
|
|
|
|
self.cm
|
2022-01-13 17:06:11 +03:00
|
|
|
.new_source_file(FileName::Custom(url.to_string()), src)
|
2020-12-02 16:48:35 +03:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2022-02-26 09:41:14 +03:00
|
|
|
let module = parse_file_as_module(
|
|
|
|
&fm,
|
2020-12-02 16:48:35 +03:00
|
|
|
Syntax::Typescript(TsConfig {
|
|
|
|
decorators: true,
|
|
|
|
tsx,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-10-31 15:25:15 +03:00
|
|
|
EsVersion::Es2020,
|
2020-12-02 16:48:35 +03:00
|
|
|
None,
|
2022-02-26 09:41:14 +03:00
|
|
|
&mut vec![],
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|err| {
|
2021-01-11 13:38:20 +03:00
|
|
|
let handler =
|
|
|
|
Handler::with_tty_emitter(ColorConfig::Always, false, false, Some(self.cm.clone()));
|
|
|
|
err.into_diagnostic(&handler).emit();
|
|
|
|
panic!("failed to parse")
|
|
|
|
});
|
2021-12-04 05:35:16 +03:00
|
|
|
|
2022-03-20 10:53:53 +03:00
|
|
|
let module = HELPERS.set(&Helpers::new(false), || {
|
|
|
|
module
|
2022-04-26 10:38:50 +03:00
|
|
|
.fold_with(&mut resolver(unresolved_mark, top_level_mark, false))
|
2022-03-20 10:53:53 +03:00
|
|
|
.fold_with(&mut decorators(decorators::Config {
|
|
|
|
legacy: true,
|
|
|
|
emit_metadata: Default::default(),
|
|
|
|
}))
|
2022-04-26 10:38:50 +03:00
|
|
|
.fold_with(&mut strip(top_level_mark))
|
2022-03-20 10:53:53 +03:00
|
|
|
.fold_with(&mut react::<SingleThreadedComments>(
|
|
|
|
self.cm.clone(),
|
|
|
|
None,
|
|
|
|
Default::default(),
|
|
|
|
top_level_mark,
|
|
|
|
))
|
|
|
|
.fold_with(&mut inject_helpers())
|
|
|
|
});
|
2020-12-02 16:48:35 +03:00
|
|
|
|
|
|
|
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> {
|
2021-10-08 10:47:01 +03:00
|
|
|
let path = path.clean();
|
|
|
|
Ok(FileName::Real(path))
|
2020-12-02 16:48:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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());
|
|
|
|
}
|
|
|
|
|
2021-12-16 14:37:22 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 16:48:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2022-01-14 01:20:45 +03:00
|
|
|
fn resolve_as_directory(&self, path: &Path) -> Result<PathBuf, Error> {
|
2020-12-02 16:48:35 +03:00
|
|
|
// 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.
|
2022-01-14 01:20:45 +03:00
|
|
|
fn resolve_package_main(&self, _: &Path) -> Result<PathBuf, Error> {
|
2020-12-02 16:48:35 +03:00
|
|
|
bail!("package.json is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve a directory to its index.EXT.
|
2022-01-14 01:20:45 +03:00
|
|
|
fn resolve_index(&self, path: &Path) -> Result<PathBuf, Error> {
|
2020-12-02 16:48:35 +03:00
|
|
|
// 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> {
|
2022-01-14 01:20:45 +03:00
|
|
|
if let Ok(v) = Url::parse(target) {
|
|
|
|
return Ok(FileName::Custom(v.to_string()));
|
2020-12-02 16:48:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let base = match base {
|
|
|
|
FileName::Real(v) => v,
|
|
|
|
FileName::Custom(base_url) => {
|
2022-01-13 17:06:11 +03:00
|
|
|
let base_url = Url::parse(base_url).context("failed to parse url")?;
|
2020-12-02 16:48:35 +03:00
|
|
|
|
|
|
|
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
|
2022-01-13 17:06:11 +03:00
|
|
|
if target.starts_with('/') {
|
2020-12-02 16:48:35 +03:00
|
|
|
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(".");
|
2022-01-13 17:06:11 +03:00
|
|
|
let mut base_dir = base.parent().unwrap_or(cwd);
|
2020-12-02 16:48:35 +03:00
|
|
|
|
|
|
|
if target.starts_with("./") || target.starts_with("../") {
|
2021-03-22 13:42:42 +03:00
|
|
|
let win_target;
|
|
|
|
let target = if cfg!(target_os = "windows") {
|
2022-02-08 13:50:22 +03:00
|
|
|
let t = if let Some(s) = target.strip_prefix("./") {
|
|
|
|
s
|
2021-03-22 13:42:42 +03:00
|
|
|
} else {
|
|
|
|
base_dir = base_dir.parent().unwrap();
|
|
|
|
&target[3..]
|
|
|
|
};
|
2022-02-08 13:50:22 +03:00
|
|
|
win_target = t.replace('/', "\\");
|
2021-03-22 13:42:42 +03:00
|
|
|
&*win_target
|
|
|
|
} else {
|
|
|
|
target
|
|
|
|
};
|
|
|
|
|
2020-12-02 16:48:35 +03:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|