Skip to content

Commit 9ee88a8

Browse files
committed
native: fix resolver PackagePathNotExported error
1 parent d5ee5f6 commit 9ee88a8

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

native/src/main.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,44 +105,50 @@ fn resolve(wd: &str, specifier: &str, containing_filename: Option<String>) -> Re
105105
.to_owned()
106106
};
107107

108-
if (fullpath.ends_with(".js") || fullpath.ends_with(".cjs") || fullpath.ends_with(".json"))
109-
&& file_exists(&fullpath).expect("Can't check existence of file")
108+
if (fullpath.ends_with(".js") || fullpath.ends_with(".cjs") || fullpath.ends_with(".json")) && file_exists(&fullpath)?
110109
{
111110
return Ok(fullpath);
112111
}
113112
if fullpath.ends_with(".js") {
113+
// path/to/file.js -> path/to/file.cjs
114114
let maybe_exists = fullpath[..fullpath.len() - 3].to_owned() + ".cjs";
115-
if file_exists(&maybe_exists).expect("Can't check existence of file") {
115+
if file_exists(&maybe_exists)? {
116116
return Ok(maybe_exists);
117117
}
118118
}
119119
if fullpath.ends_with(".cjs") {
120+
// path/to/file.cjs -> path/to/file.js
120121
let maybe_exists = fullpath[..fullpath.len() - 4].to_owned() + ".js";
121-
if file_exists(&maybe_exists).expect("Can't check existence of file") {
122+
if file_exists(&maybe_exists)? {
122123
return Ok(maybe_exists);
123124
}
124125
}
125-
let maybe_exists = fullpath.to_owned() + ".cjs";
126-
if file_exists(&maybe_exists).expect("Can't check existence of file")
127-
&& !dir_exists(&fullpath).expect("Can't check existence of directory")
128-
{
129-
return Ok(maybe_exists);
130-
}
131-
132-
// `/path/to/wd/node_modules/react/index` -> `react/index`
133-
if specifier.starts_with("/") {
134-
let rel_path = Path::new(&specifier)
135-
.strip_prefix(Path::new(wd).join("node_modules"))
136-
.unwrap();
137-
specifier = rel_path.to_str().unwrap().to_owned();
138-
}
139126

127+
// otherwise, let oxc_resolver do the job
140128
let resolver = Resolver::new(ResolveOptions {
141129
condition_names: vec!["node".to_owned(), "require".to_owned()],
142130
..Default::default()
143131
});
144-
let ret = resolver.resolve(wd, &specifier)?;
145-
Ok(ret.path().to_str().unwrap().to_owned())
132+
let ret = match resolver.resolve(wd, &specifier) {
133+
Ok(ret) => Ok(ret),
134+
Err(err) => match err {
135+
ResolveError::PackagePathNotExported(_, _) => {
136+
// path/to/foo -> path/to/foo.cjs
137+
let maybe_exists = fullpath.to_owned() + ".cjs";
138+
if file_exists(&maybe_exists)? {
139+
return Ok(maybe_exists);
140+
}
141+
// path/to/foo -> path/to/foo.js
142+
let maybe_exists = fullpath.to_owned() + ".js";
143+
if file_exists(&maybe_exists)? {
144+
return Ok(maybe_exists);
145+
}
146+
Err(err)
147+
}
148+
_ => Err(err),
149+
},
150+
};
151+
Ok(ret?.path().to_str().unwrap().to_owned())
146152
}
147153

148154
pub fn dir_exists(path: &str) -> io::Result<bool> {

0 commit comments

Comments
 (0)