@@ -17,7 +17,7 @@ import type { TsConfigResult } from 'get-tsconfig'
1717import type { Version } from 'is-bun-module'
1818
1919const fileTsJsConfigCache : Map < string , TsConfigResult > = new Map ( )
20- const possiblePathsBySpecifier : Map < { specifier : string ; configPath : TsConfigResult [ 'path' ] } , string [ ] > = new Map ( )
20+ const possiblePathsBySpecifier : Map < string , { paths : string [ ] ; path : TsConfigResult [ 'path' ] } > = new Map ( )
2121
2222/**
2323 * Resolve an import specifier to a path
@@ -129,10 +129,9 @@ function getMappedPath(
129129 if ( isFile ( resolved ) ) {
130130 return resolved
131131 }
132+ return undefined
132133 }
133134
134- let paths : string [ ] = [ ]
135-
136135 let projectConfig : TsConfigResult | null = null
137136
138137 const { ext, dir } = path . parse ( file )
@@ -155,37 +154,47 @@ function getMappedPath(
155154 logger ( 'found closest mapper by config path:' , closestMapper . path )
156155 }
157156
158- const possiblePaths = (
159- closestMapper
160- ? closestMapper . matcher ( specifier )
161- : currentMatchersOfCwd . map ( ( mapper ) => mapper ?. matcher ( specifier ) ) . flat ( )
162- )
157+ const matcherResult = closestMapper
158+ ? closestMapper . matcher ( specifier )
159+ : currentMatchersOfCwd . map ( ( mapper ) => mapper ? .matcher ( specifier ) ) . flat ( )
160+
161+ const matcherPaths = matcherResult
163162 . map ( ( item ) => [
164163 ...extensions . map ( ( ext ) => `${ item } ${ ext } ` ) ,
165164 ...originalExtensions . map ( ( ext ) => `${ item } /index${ ext } ` ) ,
166165 ] )
167166 . flat ( )
168167
169- logger ( 'possible paths:' , possiblePaths )
170-
171- paths = projectConfig ?. path
172- ? ( possiblePathsBySpecifier . get ( { specifier, configPath : projectConfig . path } ) ?? [ ] )
173- : possiblePaths . filter ( ( mappedPath ) => {
174- try {
175- const stat = fs . statSync ( mappedPath , { throwIfNoEntry : false } )
176- if ( stat === undefined ) return false
177- if ( stat . isFile ( ) ) return true
178- if ( stat . isDirectory ( ) ) {
179- return isModule ( mappedPath )
180- }
181- } catch {
182- return false
183- }
184-
185- return false
186- } )
187-
188- if ( projectConfig ?. path ) possiblePathsBySpecifier . set ( { specifier, configPath : projectConfig . path } , paths )
168+ logger ( 'matcher paths:' , matcherPaths )
169+
170+ const possiblePaths = possiblePathsBySpecifier . get ( specifier )
171+
172+ if ( possiblePaths && possiblePaths . path === closestMapper ?. path ) {
173+ logger (
174+ `found matching paths from cache by specifier ${ specifier } and config ${ possiblePaths . path } path:` ,
175+ possiblePaths . paths
176+ )
177+ return possiblePaths . paths [ 0 ]
178+ }
179+
180+ const paths = matcherPaths . filter ( ( mappedPath ) => {
181+ try {
182+ const stat = fs . statSync ( mappedPath , { throwIfNoEntry : false } )
183+ if ( stat === undefined ) return false
184+ if ( stat . isFile ( ) ) return true
185+ if ( stat . isDirectory ( ) ) {
186+ return isModule ( mappedPath )
187+ }
188+ } catch {
189+ return false
190+ }
191+
192+ return false
193+ } )
194+
195+ if ( closestMapper ) {
196+ possiblePathsBySpecifier . set ( specifier , { paths, path : closestMapper . path } )
197+ }
189198
190199 if ( paths . length > 1 ) {
191200 logger ( 'found multiple matching paths:' , paths )
0 commit comments