Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion fixtures/pnpm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"axios": "1.6.2",
"ipaddr.js": "2.2.0",
"postcss": "8.4.33",
"styled-components": "6.1.1"
"styled-components": "6.1.1",
"mathjs": "13.2.0",
"decimal.js": "10.4.3"
}
}
77 changes: 77 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,22 +1050,23 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
let path = cached_path.path();
let Some(filename) = path.file_name() else { return Ok(None) };
let path_without_extension = path.with_extension("");

ctx.with_fully_specified(true);
for extension in extensions {
let mut path_with_extension = path_without_extension.clone().into_os_string();
path_with_extension.reserve_exact(extension.len());
path_with_extension.push(extension);
let cached_path = self.cache.value(Path::new(&path_with_extension));
// Bail if path is module directory such as `ipaddr.js`
if cached_path.is_dir(&self.cache.fs, ctx) {
ctx.with_fully_specified(false);
return Ok(None);
}
if let Some(path) = self.load_alias_or_file(&cached_path, ctx)? {
ctx.with_fully_specified(false);
return Ok(Some(path));
}
}
// Bail if path is module directory such as `ipaddr.js`
if !cached_path.is_file(&self.cache.fs, ctx) {
ctx.with_fully_specified(false);
return Ok(None);
}
// Create a meaningful error message.
let dir = path.parent().unwrap().to_path_buf();
let filename_without_extension = Path::new(filename).with_extension("");
Expand Down
54 changes: 54 additions & 0 deletions tests/resolve_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,57 @@ fn ipaddr_js() {
assert_eq!(resolution, Ok(module_path.clone()));
}
}

#[test]
fn decimal_js() {
let dir = dir();
let path = dir.join("fixtures/pnpm");
let module_path =
dir.join("node_modules/.pnpm/[email protected]/node_modules/decimal.js/decimal.mjs");

let resolvers = [
// with `extension_alias`
Resolver::new(ResolveOptions {
extension_alias: vec![(".js".into(), vec![".js".into(), ".ts".into(), ".tsx".into()])],
condition_names: vec!["import".into()],
..ResolveOptions::default()
}),
// default
Resolver::new(ResolveOptions {
condition_names: vec!["import".into()],
..ResolveOptions::default()
}),
];

for resolver in resolvers {
let resolution = resolver.resolve(&path, "decimal.js").map(|r| r.full_path());
assert_eq!(resolution, Ok(module_path.clone()));
}
}

#[test]
fn decimal_js_from_mathjs() {
let dir = dir();
let path = dir.join("node_modules/.pnpm/[email protected]/node_modules/mathjs/lib/esm");
let module_path =
dir.join("node_modules/.pnpm/[email protected]/node_modules/decimal.js/decimal.mjs");

let resolvers = [
// with `extension_alias`
Resolver::new(ResolveOptions {
extension_alias: vec![(".js".into(), vec![".js".into(), ".ts".into(), ".tsx".into()])],
condition_names: vec!["import".into()],
..ResolveOptions::default()
}),
// default
Resolver::new(ResolveOptions {
condition_names: vec!["import".into()],
..ResolveOptions::default()
}),
];

for resolver in resolvers {
let resolution = resolver.resolve(&path, "decimal.js").map(|r| r.full_path());
assert_eq!(resolution, Ok(module_path.clone()));
}
}