Add { Load, Resolve }

This commit is contained in:
강동윤 2020-02-15 12:52:06 +09:00
parent 4550f7e1af
commit f17e49934c
4 changed files with 43 additions and 0 deletions

View File

@ -15,6 +15,7 @@ swc_ecma_ast = { version = "0.17.0", path ="../ast" }
swc_atoms = { version = "0.2.0", path ="../../atoms" }
swc_common = { version = "0.5.0", path ="../../common" }
swc_ecma_parser = { version = "0.19", path ="../parser", features = ["verify"] }
anyhow = "1.0.26"
once_cell = "1"
scoped-tls = "1"
unicode-xid = "0.2"

View File

@ -36,7 +36,9 @@ mod macros;
pub mod constructor;
mod factory;
pub mod ident;
pub mod load;
pub mod options;
pub mod resolve;
mod value;
pub mod var;

View File

@ -0,0 +1,20 @@
use anyhow::Error;
use std::{path::Path, sync::Arc};
use swc_common::SourceFile;
use swc_ecma_ast::Module;
pub trait Load {
fn load(&self, path: &Path) -> Result<(Arc<SourceFile>, Module), Error>;
}
impl<T: ?Sized + Load> Load for Box<T> {
fn load(&self, path: &Path) -> Result<(Arc<SourceFile>, Module), Error> {
T::load(self, path)
}
}
impl<'a, T: ?Sized + Load> Load for &'a T {
fn load(&self, path: &Path) -> Result<(Arc<SourceFile>, Module), Error> {
T::load(self, path)
}
}

View File

@ -0,0 +1,20 @@
use anyhow::Error;
use std::path::{Path, PathBuf};
pub trait Resolve {
/// Returned filename will be hashed if possible and used to generate module
/// id.
fn resolve(&self, base: &Path, import: &str) -> Result<PathBuf, Error>;
}
impl<T: ?Sized + Resolve> Resolve for Box<T> {
fn resolve(&self, base: &Path, import: &str) -> Result<PathBuf, Error> {
T::resolve(self, base, import)
}
}
impl<'a, T: ?Sized + Resolve> Resolve for &'a T {
fn resolve(&self, base: &Path, import: &str) -> Result<PathBuf, Error> {
T::resolve(self, base, import)
}
}