@@ -105,44 +105,50 @@ fn resolve(wd: &str, specifier: &str, containing_filename: Option<String>) -> Re
105
105
. to_owned ( )
106
106
} ;
107
107
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) ?
110
109
{
111
110
return Ok ( fullpath) ;
112
111
}
113
112
if fullpath. ends_with ( ".js" ) {
113
+ // path/to/file.js -> path/to/file.cjs
114
114
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) ? {
116
116
return Ok ( maybe_exists) ;
117
117
}
118
118
}
119
119
if fullpath. ends_with ( ".cjs" ) {
120
+ // path/to/file.cjs -> path/to/file.js
120
121
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) ? {
122
123
return Ok ( maybe_exists) ;
123
124
}
124
125
}
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
- }
139
126
127
+ // otherwise, let oxc_resolver do the job
140
128
let resolver = Resolver :: new ( ResolveOptions {
141
129
condition_names : vec ! [ "node" . to_owned( ) , "require" . to_owned( ) ] ,
142
130
..Default :: default ( )
143
131
} ) ;
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 ( ) )
146
152
}
147
153
148
154
pub fn dir_exists ( path : & str ) -> io:: Result < bool > {
0 commit comments