diff --git a/.changeset/dirty-panthers-reflect.md b/.changeset/dirty-panthers-reflect.md new file mode 100644 index 000000000000..463b6b4320d4 --- /dev/null +++ b/.changeset/dirty-panthers-reflect.md @@ -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 diff --git a/crates/swc_ecma_loader/src/resolvers/tsc.rs b/crates/swc_ecma_loader/src/resolvers/tsc.rs index a05773209770..1c78f4343662 100644 --- a/crates/swc_ecma_loader/src/resolvers/tsc.rs +++ b/crates/swc_ecma_loader/src/resolvers/tsc.rs @@ -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() { diff --git a/crates/swc_ecma_loader/tests/tsc_resolver.rs b/crates/swc_ecma_loader/tests/tsc_resolver.rs index 36cda70a385a..5d02a683f6cf 100644 --- a/crates/swc_ecma_loader/tests/tsc_resolver.rs +++ b/crates/swc_ecma_loader/tests/tsc_resolver.rs @@ -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();