-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(swc): Add import resolvers (#1834)
swc_ecma_loader: - Add `Resolve`. - Add `TsConfigResolver`. swc_ecma_transforms_module: - Use `Resolve` for remapping import paths. - Add `ImportResolver`. - Add `NodeImprortResolver`. swc: - Add `paths` to `.swcrc`. - Use `paths`. (#379, #702) - Canonicalize file names.
- Loading branch information
Showing
46 changed files
with
899 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ jobs: | |
tool: "cargo" | ||
# Where the output from the benchmark tool is stored | ||
output-file-path: output.txt | ||
external-data-json-path: ./raw-data/benchmark-data.json | ||
external-data-json-path: ./raw-data/$GITHUB_SHA/benchmark-data.json | ||
# Workflow will fail when an alert happens | ||
fail-on-alert: true | ||
# GitHub API token to make a commit comment | ||
|
@@ -67,8 +67,7 @@ jobs: | |
token: ${{ secrets.GH_TOKEN }} | ||
branch: gh-pages | ||
folder: raw-data | ||
clean: true | ||
git-config-email: [email protected] | ||
git-config-email: [email protected] | ||
repository-name: swc-project/raw-data | ||
commit-message: "Update" | ||
single-commit: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,7 +247,7 @@ jobs: | |
branch: gh-pages | ||
folder: target/doc | ||
clean: true | ||
git-config-email: [email protected] | ||
git-config-email: [email protected] | ||
repository-name: swc-project/rustdoc | ||
commit-message: "Update" | ||
single-commit: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1 @@ | ||
use anyhow::Error; | ||
use swc_common::FileName; | ||
|
||
pub trait Resolve: swc_common::sync::Send + swc_common::sync::Sync { | ||
fn resolve(&self, base: &FileName, module_specifier: &str) -> Result<FileName, Error>; | ||
} | ||
|
||
impl<T: ?Sized + Resolve> Resolve for Box<T> { | ||
fn resolve(&self, base: &FileName, module_specifier: &str) -> Result<FileName, Error> { | ||
(**self).resolve(base, module_specifier) | ||
} | ||
} | ||
|
||
impl<'a, T: ?Sized + Resolve> Resolve for &'a T { | ||
fn resolve(&self, base: &FileName, module_specifier: &str) -> Result<FileName, Error> { | ||
(**self).resolve(base, module_specifier) | ||
} | ||
} | ||
pub use swc_ecma_loader::resolve::Resolve; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,2 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn it_works() { | ||
assert_eq!(2 + 2, 4); | ||
} | ||
} | ||
pub mod resolve; | ||
pub mod resolvers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use anyhow::Error; | ||
use std::sync::Arc; | ||
use swc_common::sync::{Send, Sync}; | ||
use swc_common::FileName; | ||
|
||
pub trait Resolve: Send + Sync { | ||
fn resolve(&self, base: &FileName, module_specifier: &str) -> Result<FileName, Error>; | ||
} | ||
|
||
macro_rules! impl_ref { | ||
($R:ident, $T:ty) => { | ||
impl<$R> Resolve for $T | ||
where | ||
R: ?Sized + Resolve, | ||
{ | ||
fn resolve(&self, base: &FileName, src: &str) -> Result<FileName, Error> { | ||
(**self).resolve(base, src) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_ref!(R, &'_ R); | ||
impl_ref!(R, Box<R>); | ||
impl_ref!(R, Arc<R>); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::resolve::Resolve; | ||
use anyhow::Error; | ||
use lru::LruCache; | ||
use std::sync::Mutex; | ||
use swc_common::FileName; | ||
|
||
#[derive(Debug)] | ||
pub struct CachingResolver<R> | ||
where | ||
R: Resolve, | ||
{ | ||
cache: Mutex<LruCache<(FileName, String), FileName>>, | ||
inner: R, | ||
} | ||
|
||
impl<R> Default for CachingResolver<R> | ||
where | ||
R: Resolve + Default, | ||
{ | ||
fn default() -> Self { | ||
Self::new(40, Default::default()) | ||
} | ||
} | ||
|
||
impl<R> CachingResolver<R> | ||
where | ||
R: Resolve, | ||
{ | ||
pub fn new(cap: usize, inner: R) -> Self { | ||
Self { | ||
cache: Mutex::new(LruCache::new(cap)), | ||
inner, | ||
} | ||
} | ||
} | ||
|
||
impl<R> Resolve for CachingResolver<R> | ||
where | ||
R: Resolve, | ||
{ | ||
fn resolve(&self, base: &FileName, src: &str) -> Result<FileName, Error> { | ||
{ | ||
let lock = self.cache.lock(); | ||
match lock { | ||
Ok(mut lock) => { | ||
// | ||
if let Some(v) = lock.get(&(base.clone(), src.to_string())) { | ||
return Ok(v.clone()); | ||
} | ||
} | ||
Err(_) => {} | ||
} | ||
} | ||
|
||
let resolved = self.inner.resolve(base, src)?; | ||
{ | ||
let lock = self.cache.lock(); | ||
match lock { | ||
Ok(mut lock) => { | ||
lock.put((base.clone(), src.to_string()), resolved.clone()); | ||
} | ||
Err(_) => {} | ||
} | ||
} | ||
|
||
Ok(resolved) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#[cfg(feature = "lru")] | ||
pub mod lru; | ||
#[cfg(feature = "node")] | ||
pub mod node; | ||
#[cfg(feature = "tsc")] | ||
pub mod tsc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.