2020-12-02 16:48:35 +03:00
|
|
|
use anyhow::{bail, Context, Error};
|
2021-10-08 10:47:01 +03:00
|
|
|
use path_clean::PathClean;
|
2020-12-02 16:48:35 +03:00
|
|
|
use reqwest::Url;
|
|
|
|
use sha1::{Digest, Sha1};
|
|
|
|
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},
|
|
|
|
};
|
|
|
|
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
|
|
|
};
|
2020-12-02 16:48:35 +03:00
|
|
|
use swc_ecma_parser::{lexer::Lexer, JscTarget, Parser, StringInput, Syntax, TsConfig};
|
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 {
|
|
|
|
let hash = calc_hash(&url.to_string());
|
|
|
|
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> {
|
2021-03-02 11:33:03 +03:00
|
|
|
let cache_dir = PathBuf::from(
|
2021-04-22 13:43:35 +03:00
|
|
|
current_dir()
|
2021-03-02 11:33:03 +03:00
|
|
|
.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
|
|
|
|
|
|
|
match read_to_string(&cache_path) {
|
|
|
|
Ok(v) => return Ok(v),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2021-10-07 09:13:21 +03:00
|
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
|
|
|
|
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");
|
|
|
|
self.cm.load_file(&path)?
|
|
|
|
}
|
|
|
|
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
|
|
|
|
.new_source_file(FileName::Custom(url.to_string()), src.to_string())
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let lexer = Lexer::new(
|
|
|
|
Syntax::Typescript(TsConfig {
|
|
|
|
decorators: true,
|
|
|
|
tsx,
|
2020-12-28 18:09:46 +03:00
|
|
|
dynamic_import: true,
|
2020-12-02 16:48:35 +03:00
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
JscTarget::Es2020,
|
|
|
|
StringInput::from(&*fm),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut parser = Parser::new_from(lexer);
|
2021-01-11 13:38:20 +03:00
|
|
|
let module = parser.parse_module().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")
|
|
|
|
});
|
2021-10-09 11:08:13 +03:00
|
|
|
let module =
|
|
|
|
module
|
|
|
|
.fold_with(&mut strip())
|
|
|
|
.fold_with(&mut react::<SingleThreadedComments>(
|
|
|
|
self.cm.clone(),
|
|
|
|
None,
|
|
|
|
Default::default(),
|
|
|
|
top_level_mark,
|
|
|
|
));
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
for ext in EXTENSIONS {
|
|
|
|
let ext_path = path.with_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: &PathBuf) -> 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, _: &PathBuf) -> Result<PathBuf, Error> {
|
|
|
|
bail!("package.json is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve a directory to its index.EXT.
|
|
|
|
fn resolve_index(&self, path: &PathBuf) -> 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> {
|
|
|
|
match Url::parse(target) {
|
|
|
|
Ok(v) => return Ok(FileName::Custom(v.to_string())),
|
|
|
|
Err(_) => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(".");
|
2021-03-22 13:42:42 +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") {
|
|
|
|
let t = if target.starts_with("./") {
|
|
|
|
&target[2..]
|
|
|
|
} else {
|
|
|
|
base_dir = base_dir.parent().unwrap();
|
|
|
|
&target[3..]
|
|
|
|
};
|
|
|
|
win_target = t.replace("/", "\\");
|
|
|
|
&*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))
|
|
|
|
}
|
|
|
|
}
|