Skip to content

Commit eaaf32d

Browse files
authored
feat(es/loader): Add more logics to tsconfig.paths handler (#1860)
1 parent 7488950 commit eaaf32d

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

ecmascript/loader/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2018"
66
license = "Apache-2.0/MIT"
77
name = "swc_ecma_loader"
88
repository = "https://github.com/swc-project/swc.git"
9-
version = "0.9.0"
9+
version = "0.9.1"
1010

1111
[package.metadata.docs.rs]
1212
all-features = true

ecmascript/loader/src/resolvers/tsc.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use anyhow::{bail, Context, Error};
33
use dashmap::DashMap;
44
use once_cell::sync::Lazy;
55
use regex::Regex;
6-
use std::path::PathBuf;
6+
use std::path::{Component, PathBuf};
77
use swc_common::FileName;
88

99
#[derive(Debug)]
@@ -94,6 +94,29 @@ where
9494
R: Resolve,
9595
{
9696
fn resolve(&self, base: &FileName, src: &str) -> Result<FileName, Error> {
97+
if src.starts_with(".") {
98+
if src == ".." || src.starts_with("./") || src.starts_with("../") {
99+
return self
100+
.inner
101+
.resolve(base, src)
102+
.context("not processed by tsc resolver because it's relative import");
103+
}
104+
}
105+
106+
match base {
107+
FileName::Real(v) => {
108+
if v.components().any(|c| match c {
109+
Component::Normal(v) => v == "node_modules",
110+
_ => false,
111+
}) {
112+
return self.inner.resolve(base, src).context(
113+
"not processed by tsc resolver because base module is in node_modules",
114+
);
115+
}
116+
}
117+
_ => {}
118+
}
119+
97120
// https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping
98121
for (from, to) in &self.paths {
99122
match from {

src/builder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
129129
/// - fixer if enabled
130130
pub fn finalize<'cmt>(
131131
self,
132+
base_url: String,
132133
paths: CompiledPaths,
133134
base: &FileName,
134135
syntax: Syntax,
@@ -198,6 +199,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
198199
Optional::new(helpers::inject_helpers(), self.inject_helpers),
199200
ModuleConfig::build(
200201
self.cm.clone(),
202+
base_url,
201203
paths,
202204
base,
203205
self.global_mark,

src/config.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ impl Options {
191191
target,
192192
loose,
193193
keep_class_names,
194+
base_url,
194195
paths,
195196
} = config.jsc;
196197
let target = target.unwrap_or_default();
@@ -269,6 +270,7 @@ impl Options {
269270
.fixer(!self.disable_fixer)
270271
.preset_env(config.env)
271272
.finalize(
273+
base_url,
272274
paths.into_iter().collect(),
273275
base,
274276
syntax,
@@ -603,6 +605,9 @@ pub struct JscConfig {
603605
#[serde(default)]
604606
pub keep_class_names: bool,
605607

608+
#[serde(default)]
609+
pub base_url: String,
610+
606611
#[serde(default)]
607612
pub paths: Paths,
608613
}
@@ -628,6 +633,7 @@ pub enum ModuleConfig {
628633
impl ModuleConfig {
629634
pub fn build(
630635
cm: Arc<SourceMap>,
636+
base_url: String,
631637
paths: CompiledPaths,
632638
base: &FileName,
633639
root_mark: Mark,
@@ -651,7 +657,7 @@ impl ModuleConfig {
651657
Some(scope),
652658
))
653659
} else {
654-
let resolver = build_resolver(paths);
660+
let resolver = build_resolver(base_url, paths);
655661

656662
Box::new(modules::common_js::common_js_with_resolver(
657663
resolver,
@@ -666,7 +672,7 @@ impl ModuleConfig {
666672
if paths.is_empty() {
667673
Box::new(modules::umd::umd(cm, root_mark, config))
668674
} else {
669-
let resolver = build_resolver(paths);
675+
let resolver = build_resolver(base_url, paths);
670676

671677
Box::new(modules::umd::umd_with_resolver(
672678
resolver, base, cm, root_mark, config,
@@ -677,7 +683,7 @@ impl ModuleConfig {
677683
if paths.is_empty() {
678684
Box::new(modules::amd::amd(config))
679685
} else {
680-
let resolver = build_resolver(paths);
686+
let resolver = build_resolver(base_url, paths);
681687

682688
Box::new(modules::amd::amd_with_resolver(resolver, base, config))
683689
}
@@ -961,23 +967,27 @@ impl Merge for ConstModulesConfig {
961967
}
962968
}
963969

964-
fn build_resolver(paths: CompiledPaths) -> SwcImportResolver {
965-
static CACHE: Lazy<DashMap<CompiledPaths, SwcImportResolver>> =
970+
fn build_resolver(base_url: String, paths: CompiledPaths) -> SwcImportResolver {
971+
static CACHE: Lazy<DashMap<(String, CompiledPaths), SwcImportResolver>> =
966972
Lazy::new(|| Default::default());
967973

968-
if let Some(cached) = CACHE.get(&paths) {
974+
if let Some(cached) = CACHE.get(&(base_url.clone(), paths.clone())) {
969975
return (*cached).clone();
970976
}
971977

972978
let r = {
973-
let r = TsConfigResolver::new(NodeResolver::default(), ".".into(), paths.clone());
979+
let r = TsConfigResolver::new(
980+
NodeResolver::default(),
981+
base_url.clone().into(),
982+
paths.clone(),
983+
);
974984
let r = CachingResolver::new(40, r);
975985

976986
let r = NodeImportResolver::new(r);
977987
Arc::new(r)
978988
};
979989

980-
CACHE.insert(paths, r.clone());
990+
CACHE.insert((base_url, paths), r.clone());
981991

982992
r
983993
}

0 commit comments

Comments
 (0)