@@ -95,7 +95,14 @@ export function resolve(
95
95
found : boolean
96
96
path ?: string | null
97
97
} {
98
- const opts : ResolveOptions & TsResolverOptions = {
98
+ const opts : Required <
99
+ Pick <
100
+ ResolveOptions ,
101
+ 'conditionNames' | 'extensions' | 'mainFields' | 'useSyncFileSystemCalls'
102
+ >
103
+ > &
104
+ ResolveOptions &
105
+ TsResolverOptions = {
99
106
...options ,
100
107
extensions : options ?. extensions ?? defaultExtensions ,
101
108
mainFields : options ?. mainFields ?? defaultMainFields ,
@@ -122,7 +129,7 @@ export function resolve(
122
129
123
130
initMappers ( opts )
124
131
125
- const mappedPath = getMappedPath ( source , file , true )
132
+ const mappedPath = getMappedPath ( source , file , opts . extensions , true )
126
133
if ( mappedPath ) {
127
134
log ( 'matched ts path:' , mappedPath )
128
135
}
@@ -250,12 +257,15 @@ const isFile = (path?: string | undefined): path is string => {
250
257
/**
251
258
* @param {string } source the module to resolve; i.e './some-module'
252
259
* @param {string } file the importing file's full path; i.e. '/usr/local/bin/file.js'
260
+ * @param {string[] } extensions the extensions to try
261
+ * @param {boolean } retry should retry on failed to resolve
253
262
* @returns The mapped path of the module or undefined
254
263
*/
255
264
// eslint-disable-next-line sonarjs/cognitive-complexity
256
265
function getMappedPath (
257
266
source : string ,
258
267
file : string ,
268
+ extensions = defaultExtensions ,
259
269
retry ?: boolean ,
260
270
) : string | undefined {
261
271
let paths : string [ ] | undefined = [ ]
@@ -269,9 +279,7 @@ function getMappedPath(
269
279
paths = mappers !
270
280
. map ( mapper =>
271
281
mapper ?.( source ) . map ( item =>
272
- path . extname ( item )
273
- ? item
274
- : [ 'ts' , 'tsx' , '.d.ts' , 'js' ] . map ( ext => `${ item } .${ ext } ` ) ,
282
+ [ '' , ...extensions ] . map ( ext => `${ item } ${ ext } ` ) ,
275
283
) ,
276
284
)
277
285
. flat ( 2 )
@@ -283,28 +291,28 @@ function getMappedPath(
283
291
const jsExt = path . extname ( source )
284
292
const tsExt = jsExt . replace ( 'js' , 'ts' )
285
293
const basename = source . replace ( JS_EXT_PATTERN , '' )
286
- return (
294
+
295
+ const resolved =
287
296
getMappedPath ( basename + tsExt , file ) ||
288
- getMappedPath ( source + '/index.ts' , file ) ||
289
- getMappedPath ( source + '/index.tsx' , file ) ||
290
- getMappedPath ( source + '/index.d.ts' , file ) ||
291
297
getMappedPath (
292
298
basename + '.d' + ( tsExt === '.tsx' ? '.ts' : tsExt ) ,
293
299
file ,
294
- ) ||
295
- getMappedPath ( source + '/index.js' , file )
296
- )
300
+ )
301
+
302
+ if ( resolved ) {
303
+ return resolved
304
+ }
305
+ }
306
+
307
+ for ( const ext of extensions ) {
308
+ const resolved =
309
+ getMappedPath ( source + ext , file ) ||
310
+ getMappedPath ( source + `/index${ ext } ` , file )
311
+
312
+ if ( resolved ) {
313
+ return resolved
314
+ }
297
315
}
298
- return (
299
- getMappedPath ( source + '.ts' , file ) ||
300
- getMappedPath ( source + '.tsx' , file ) ||
301
- getMappedPath ( source + '.js' , file ) ||
302
- getMappedPath ( source + '.d.ts' , file ) ||
303
- getMappedPath ( source + '/index.ts' , file ) ||
304
- getMappedPath ( source + '/index.tsx' , file ) ||
305
- getMappedPath ( source + '/index.d.ts' , file ) ||
306
- getMappedPath ( source + '/index.js' , file )
307
- )
308
316
}
309
317
310
318
if ( paths . length > 1 ) {
0 commit comments