1
1
import { isBuiltin } from 'node:module'
2
2
import path from 'node:path'
3
3
4
- import { resolve as oxcResolve } from 'eslint-import-resolver-oxc'
5
4
import { type Version , isBunModule } from 'is-bun-module'
6
5
import { ResolverFactory } from 'oxc-resolver'
6
+ import stableHash_ from 'stable-hash'
7
7
8
8
import { JS_EXT_PATTERN } from './constants.js'
9
9
import { mangleScopedPackage , removeQuerystring } from './helpers.js'
@@ -16,6 +16,28 @@ export * from './helpers.js'
16
16
export * from './normalize-options.js'
17
17
export type * from './types.js'
18
18
19
+ // CJS <-> ESM interop
20
+ const stableHash = 'default' in stableHash_ ? stableHash_ . default : stableHash_
21
+
22
+ const resolverCache = new Map < string , ResolverFactory > ( )
23
+
24
+ const oxcResolve = (
25
+ source : string ,
26
+ file : string ,
27
+ resolver : ResolverFactory ,
28
+ ) => {
29
+ const result = resolver . sync ( path . dirname ( file ) , source )
30
+ if ( result . path ) {
31
+ return {
32
+ found : true ,
33
+ path : result . path ,
34
+ }
35
+ }
36
+ return {
37
+ found : false ,
38
+ }
39
+ }
40
+
19
41
export const resolve = (
20
42
source : string ,
21
43
file : string ,
@@ -39,20 +61,28 @@ export const resolve = (
39
61
}
40
62
}
41
63
42
- source = removeQuerystring ( source )
43
-
44
64
if ( resolver == null ) {
45
- options = normalizeOptions ( options )
65
+ const optionsHash = stableHash ( options )
66
+ // take `cwd` into account -- #217
67
+ const cacheKey = `${ optionsHash } :${ process . cwd ( ) } `
68
+ let cached = resolverCache . get ( cacheKey )
69
+ if ( ! cached ) {
70
+ options = normalizeOptions ( options )
71
+ resolverCache . set ( cacheKey , ( cached = new ResolverFactory ( options ) ) )
72
+ }
73
+ resolver = cached
46
74
}
47
75
48
- const resolved = oxcResolve ( source , file , options , resolver )
76
+ source = removeQuerystring ( source )
77
+
78
+ const resolved = oxcResolve ( source , file , resolver )
49
79
50
80
const foundPath = resolved . path
51
81
52
82
// naive attempt at `@types/*` resolution,
53
83
// if path is neither absolute nor relative
54
84
if (
55
- ( JS_EXT_PATTERN . test ( foundPath ! ) ||
85
+ ( ( foundPath && JS_EXT_PATTERN . test ( foundPath ) ) ||
56
86
( options ?. alwaysTryTypes && ! foundPath ) ) &&
57
87
! / ^ @ t y p e s [ / \\ ] / . test ( source ) &&
58
88
! path . isAbsolute ( source ) &&
@@ -61,27 +91,21 @@ export const resolve = (
61
91
const definitelyTyped = oxcResolve (
62
92
'@types/' + mangleScopedPackage ( source ) ,
63
93
file ,
64
- options ,
94
+ resolver ,
65
95
)
96
+
66
97
if ( definitelyTyped . found ) {
67
98
return definitelyTyped
68
99
}
69
100
}
70
101
71
102
if ( foundPath ) {
72
103
log ( 'matched path:' , foundPath )
73
-
74
- return {
75
- found : true ,
76
- path : foundPath ,
77
- }
104
+ } else {
105
+ log ( "didn't find" , source )
78
106
}
79
107
80
- log ( "didn't find " , source )
81
-
82
- return {
83
- found : false ,
84
- }
108
+ return resolved
85
109
}
86
110
87
111
export const createTypeScriptImportResolver = (
0 commit comments