@@ -4,17 +4,18 @@ import { CachedInputFileSystem, ResolverFactory } from 'enhanced-resolve'
44
55import { defaultConditionNames , defaultExtensionAlias , defaultExtensions , defaultMainFields } from './default'
66import { digestHashObject } from './helpers'
7+ import { init } from './init'
78import { logger } from './logger'
89import { resolveImport } from './resolveImport'
910
10- import type { InternalResolverOptions , ResolvedResult , ResolverOptions } from './types'
11+ import type { InternalResolverOptions , Matcher , ResolvedResult , ResolverOptions } from './types'
1112import type { Resolver } from 'enhanced-resolve'
1213
1314const times = [ ]
1415
1516let cachedOptions : InternalResolverOptions
16- let previousOptionsHash : ReturnType < typeof digestHashObject >
17- let optionsHash : ReturnType < typeof digestHashObject >
17+ let previousOptionsHash : ReturnType < typeof digestHashObject > | undefined
18+ let optionsHash : ReturnType < typeof digestHashObject > | undefined
1819let resolverCachedOptions : InternalResolverOptions
1920let resolver : Resolver | undefined
2021
@@ -25,7 +26,7 @@ export const interfaceVersion = 2
2526 * @param file the importing file's full path; i.e. '/usr/local/bin/file.js'
2627 * @param options
2728 */
28- export function resolve ( specifier : string , file : string , options ?: ResolverOptions | undefined | null ) : ResolvedResult {
29+ export function resolve ( specifier : string , file : string , options ?: ResolverOptions ) : ResolvedResult {
2930 let t0 : DOMHighResTimeStamp = 0
3031
3132 if ( options ?. performanceToLog ) t0 = performance . now ( )
@@ -43,12 +44,14 @@ export function resolve(specifier: string, file: string, options?: ResolverOptio
4344 }
4445 }
4546
47+ const matchers : Matcher [ ] = init ( options ?? { } , optionsHash ?? digestHashObject ( options ) )
48+
4649 if ( ! resolver || resolverCachedOptions !== cachedOptions ) {
4750 resolver = ResolverFactory . createResolver ( cachedOptions )
4851 resolverCachedOptions = cachedOptions
4952 }
5053
51- const result = resolveImport ( specifier , file , cachedOptions , resolver )
54+ const result = resolveImport ( matchers , specifier , file , cachedOptions , resolver )
5255
5356 if ( options ?. performanceToLog ) {
5457 const t1 = performance . now ( )
@@ -60,15 +63,21 @@ export function resolve(specifier: string, file: string, options?: ResolverOptio
6063}
6164
6265export function createImportResolver ( options : ResolverOptions ) {
63- const resolver = ResolverFactory . createResolver ( {
64- ...options ,
65- conditionNames : options ?. conditionNames ?? defaultConditionNames ,
66- extensions : options ?. extensions ?? defaultExtensions ,
67- extensionAlias : options ?. extensionAlias ?? defaultExtensionAlias ,
68- mainFields : options ?. mainFields ?? defaultMainFields ,
69- fileSystem : new CachedInputFileSystem ( fs , 5 * 1000 ) ,
70- useSyncFileSystemCalls : true ,
71- } )
66+ if ( ! cachedOptions || previousOptionsHash !== ( optionsHash = digestHashObject ( options ) ) ) {
67+ previousOptionsHash = optionsHash
68+ cachedOptions = {
69+ ...options ,
70+ conditionNames : options ?. conditionNames ?? defaultConditionNames ,
71+ extensions : options ?. extensions ?? defaultExtensions ,
72+ extensionAlias : options ?. extensionAlias ?? defaultExtensionAlias ,
73+ mainFields : options ?. mainFields ?? defaultMainFields ,
74+ fileSystem : new CachedInputFileSystem ( fs , 5 * 1000 ) ,
75+ useSyncFileSystemCalls : true ,
76+ }
77+ }
78+
79+ const matchers : Matcher [ ] = init ( options , optionsHash ?? digestHashObject ( options ) )
80+ const resolver = ResolverFactory . createResolver ( cachedOptions )
7281
7382 return {
7483 name : 'eslint-import-resolver-x' ,
@@ -78,7 +87,7 @@ export function createImportResolver(options: ResolverOptions) {
7887
7988 if ( options ?. performanceToLog ) t0 = performance . now ( )
8089
81- const result = resolveImport ( modulePath , source , cachedOptions , resolver )
90+ const result = resolveImport ( matchers , modulePath , source , cachedOptions , resolver )
8291
8392 if ( options ?. performanceToLog ) {
8493 const t1 = performance . now ( )
0 commit comments