Skip to content

Commit

Permalink
fix(es/module): Fix jsc.paths using absolute paths with dots in a f…
Browse files Browse the repository at this point in the history
…ilename for an alias (#9595)

**Description:**

- Resolves `jsc.paths` using an absolute filename with dots

**Related issue:**

- Closes #9591
  • Loading branch information
victorfreitas authored Oct 1, 2024
1 parent b94a0e1 commit 74e3d04
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/dirty-panthers-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_loader: patch
---

fix(es/module): Fix jsc.paths using absolute paths with dots in a filename for an alias
2 changes: 1 addition & 1 deletion crates/swc_ecma_loader/src/resolvers/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ where
.split([std::path::MAIN_SEPARATOR, '/'])
.last()
.filter(|&slug| slug != "index.ts" && slug != "index.tsx")
.map(|v| v.split_once('.').map(|v| v.0).unwrap_or(v))
.map(|v| v.rsplit_once('.').map(|v| v.0).unwrap_or(v))
.map(From::from);

if tp.is_absolute() {
Expand Down
56 changes: 56 additions & 0 deletions crates/swc_ecma_loader/tests/tsc_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,62 @@ fn pattern_1() {
}
}

#[test]
fn base_url_works_for_resolves_full_filenames_with_dot_suffix() {
let mut map = HashMap::default();
map.insert(
"./common/folder1/file1.suffix1.ts".to_string(),
"suffix1".to_string(),
);
map.insert(
"./common/folder2/file2-suffix2.ts".to_string(),
"suffix2".to_string(),
);

let r = TsConfigResolver::new(
TestResolver(map),
".".into(),
vec![
(
"@common/file1-suffix1".into(),
vec!["common/folder1/file1.suffix1.ts".into()],
),
(
"@common/file2-suffix2".into(),
vec!["common/folder2/file2-suffix2.ts".into()],
),
],
);

{
let resolved = r
.resolve(&FileName::Anon, "@common/file1-suffix1")
.expect("should resolve");

assert_eq!(
resolved,
Resolution {
filename: FileName::Custom("suffix1".into()),
slug: Some("file1.suffix1".into())
}
);
}

{
let resolved = r
.resolve(&FileName::Anon, "@common/file2-suffix2")
.expect("should resolve");

assert_eq!(
resolved,
Resolution {
filename: FileName::Custom("suffix2".into()),
slug: Some("file2-suffix2".into())
}
);
}
}

#[test]
fn base_url_works_for_bare_specifier() {
let mut map = HashMap::default();
Expand Down

0 comments on commit 74e3d04

Please sign in to comment.