@@ -181,22 +181,30 @@ export function resolve(
181
181
182
182
initMappers ( cachedOptions )
183
183
184
- const mappedPath = getMappedPath ( source , file , cachedOptions . extensions , true )
185
- if ( mappedPath ) {
186
- log ( 'matched ts path:' , mappedPath )
184
+ let mappedPaths = getMappedPaths ( source , file , cachedOptions . extensions , true )
185
+
186
+ if ( mappedPaths . length > 0 ) {
187
+ log ( 'matched ts path:' , ...mappedPaths )
188
+ } else {
189
+ mappedPaths = [ source ]
187
190
}
188
191
189
192
// note that even if we map the path, we still need to do a final resolve
190
- let foundNodePath : string | null
191
- try {
192
- foundNodePath =
193
- resolver . resolveSync (
193
+ let foundNodePath : string | undefined
194
+ for ( const mappedPath of mappedPaths ) {
195
+ try {
196
+ const resolved = resolver . resolveSync (
194
197
{ } ,
195
198
path . dirname ( path . resolve ( file ) ) ,
196
- mappedPath ?? source ,
197
- ) || null
198
- } catch {
199
- foundNodePath = null
199
+ mappedPath ,
200
+ )
201
+ if ( resolved ) {
202
+ foundNodePath = resolved
203
+ break
204
+ }
205
+ } catch {
206
+ log ( 'failed to resolve with' , mappedPath )
207
+ }
200
208
}
201
209
202
210
// naive attempt at `@types/*` resolution,
@@ -286,16 +294,16 @@ const isModule = (modulePath?: string | undefined): modulePath is string => {
286
294
* @returns The mapped path of the module or undefined
287
295
*/
288
296
// eslint-disable-next-line sonarjs/cognitive-complexity
289
- function getMappedPath (
297
+ function getMappedPaths (
290
298
source : string ,
291
299
file : string ,
292
300
extensions : string [ ] = defaultExtensions ,
293
301
retry ?: boolean ,
294
- ) : string | undefined {
302
+ ) : string [ ] {
295
303
const originalExtensions = extensions
296
304
extensions = [ '' , ...extensions ]
297
305
298
- let paths : Array < string | undefined > | undefined = [ ]
306
+ let paths : string [ ] = [ ]
299
307
300
308
if ( RELATIVE_PATH_PATTERN . test ( source ) ) {
301
309
const resolved = path . resolve ( path . dirname ( file ) , source )
@@ -341,34 +349,35 @@ function getMappedPath(
341
349
const tsExt = jsExt . replace ( 'js' , 'ts' )
342
350
const basename = source . replace ( JS_EXT_PATTERN , '' )
343
351
344
- const resolved =
345
- getMappedPath ( basename + tsExt , file ) ||
346
- getMappedPath (
347
- basename + '.d' + ( tsExt === '.tsx' ? '.ts' : tsExt ) ,
348
- file ,
349
- )
352
+ const mappedPaths = getMappedPaths ( basename + tsExt , file )
350
353
351
- if ( resolved ) {
354
+ const resolved =
355
+ mappedPaths . length > 0
356
+ ? mappedPaths
357
+ : getMappedPaths (
358
+ basename + '.d' + ( tsExt === '.tsx' ? '.ts' : tsExt ) ,
359
+ file ,
360
+ )
361
+
362
+ if ( resolved . length > 0 ) {
352
363
return resolved
353
364
}
354
365
}
355
366
356
367
for ( const ext of extensions ) {
368
+ const mappedPaths = isJs ? [ ] : getMappedPaths ( source + ext , file )
357
369
const resolved =
358
- ( isJs ? null : getMappedPath ( source + ext , file ) ) ||
359
- getMappedPath ( source + `/index${ ext } ` , file )
370
+ mappedPaths . length > 0
371
+ ? mappedPaths
372
+ : getMappedPaths ( source + `/index${ ext } ` , file )
360
373
361
- if ( resolved ) {
374
+ if ( resolved . length > 0 ) {
362
375
return resolved
363
376
}
364
377
}
365
378
}
366
379
367
- if ( paths . length > 1 ) {
368
- log ( 'found multiple matching ts paths:' , paths )
369
- }
370
-
371
- return paths [ 0 ]
380
+ return paths
372
381
}
373
382
374
383
// eslint-disable-next-line sonarjs/cognitive-complexity
0 commit comments