Skip to content

Commit 42cbdea

Browse files
committed
fix: fix when resolving mathjs
1 parent ae343c1 commit 42cbdea

File tree

4 files changed

+144
-7
lines changed

4 files changed

+144
-7
lines changed

fixtures/pnpm/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"axios": "1.6.2",
77
"ipaddr.js": "2.2.0",
88
"postcss": "8.4.33",
9-
"styled-components": "6.1.1"
9+
"styled-components": "6.1.1",
10+
"mathjs": "13.2.0"
1011
}
1112
}

pnpm-lock.yaml

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,22 +1050,30 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
10501050
let path = cached_path.path();
10511051
let Some(filename) = path.file_name() else { return Ok(None) };
10521052
let path_without_extension = path.with_extension("");
1053-
ctx.with_fully_specified(true);
1053+
10541054
for extension in extensions {
10551055
let mut path_with_extension = path_without_extension.clone().into_os_string();
10561056
path_with_extension.reserve_exact(extension.len());
10571057
path_with_extension.push(extension);
10581058
let cached_path = self.cache.value(Path::new(&path_with_extension));
1059-
// Bail if path is module directory such as `ipaddr.js`
1060-
if cached_path.is_dir(&self.cache.fs, ctx) {
1061-
ctx.with_fully_specified(false);
1062-
return Ok(None);
1063-
}
10641059
if let Some(path) = self.load_alias_or_file(&cached_path, ctx)? {
10651060
ctx.with_fully_specified(false);
10661061
return Ok(Some(path));
10671062
}
10681063
}
1064+
// Bail if path is module directory such as `ipaddr.js`
1065+
ctx.with_fully_specified(true);
1066+
if cached_path.is_dir(&self.cache.fs, ctx) {
1067+
ctx.with_fully_specified(false);
1068+
return Ok(None);
1069+
} else if !cached_path.is_file(&self.cache.fs, ctx) {
1070+
if let Ok(path) =
1071+
self.load_package_exports(filename.to_string_lossy().as_ref(), "", cached_path, ctx)
1072+
{
1073+
ctx.with_fully_specified(false);
1074+
return Ok(path);
1075+
}
1076+
}
10691077
// Create a meaningful error message.
10701078
let dir = path.parent().unwrap().to_path_buf();
10711079
let filename_without_extension = Path::new(filename).with_extension("");

tests/resolve_test.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,57 @@ fn ipaddr_js() {
130130
assert_eq!(resolution, Ok(module_path.clone()));
131131
}
132132
}
133+
134+
#[test]
135+
fn mathjs() {
136+
let dir = dir();
137+
let path = dir.join("fixtures/pnpm");
138+
let module_path =
139+
dir.join("node_modules/.pnpm/[email protected]/node_modules/mathjs/lib/esm/index.js");
140+
141+
let resolvers = [
142+
// with `extension_alias`
143+
Resolver::new(ResolveOptions {
144+
extension_alias: vec![(".js".into(), vec![".js".into(), ".ts".into(), ".tsx".into()])],
145+
condition_names: vec!["import".into()],
146+
..ResolveOptions::default()
147+
}),
148+
// default
149+
Resolver::new(ResolveOptions {
150+
condition_names: vec!["import".into()],
151+
..ResolveOptions::default()
152+
}),
153+
];
154+
155+
for resolver in resolvers {
156+
let resolution = resolver.resolve(&path, "mathjs").map(|r| r.full_path());
157+
assert_eq!(resolution, Ok(module_path.clone()));
158+
}
159+
}
160+
161+
// resolve decimal.js
162+
#[test]
163+
fn decimal_js() {
164+
let dir = dir();
165+
let path = dir.join("node_modules/.pnpm/[email protected]/node_modules/mathjs/lib/esm");
166+
let module_path = dir.join("node_modules/.pnpm/[email protected]/node_modules/decimal.js/decimal.mjs");
167+
168+
let resolvers = [
169+
// with `extension_alias`
170+
Resolver::new(ResolveOptions {
171+
extension_alias: vec![(".js".into(), vec![".js".into(), ".ts".into(), ".tsx".into()])],
172+
condition_names: vec!["import".into()],
173+
..ResolveOptions::default()
174+
}),
175+
// default
176+
Resolver::new(ResolveOptions {
177+
condition_names: vec!["import".into()],
178+
..ResolveOptions::default()
179+
}),
180+
];
181+
182+
for resolver in resolvers {
183+
let resolution = resolver.resolve(&path, "decimal.js").map(|r| r.full_path());
184+
assert_eq!(resolution, Ok(module_path.clone()));
185+
}
186+
}

0 commit comments

Comments
 (0)