8
8
const Factory = require ( "enhanced-resolve" ) . ResolverFactory ;
9
9
const { HookMap, SyncHook, SyncWaterfallHook } = require ( "tapable" ) ;
10
10
11
+ /** @typedef {import("enhanced-resolve/lib/Resolver") } Resolver */
12
+
11
13
module . exports = class ResolverFactory {
12
14
constructor ( ) {
13
15
this . hooks = Object . freeze ( {
@@ -16,21 +18,43 @@ module.exports = class ResolverFactory {
16
18
) ,
17
19
resolver : new HookMap ( ( ) => new SyncHook ( [ "resolver" , "resolveOptions" ] ) )
18
20
} ) ;
19
- this . cache1 = new WeakMap ( ) ;
20
- this . cache2 = new Map ( ) ;
21
+ /** @type { Map<string, { direct: WeakMap<Object, Resolver>, stringified: Map<string, Resolver> }> } */
22
+ this . cache = new Map ( ) ;
21
23
}
22
24
25
+ /**
26
+ * @param {string } type type of resolver
27
+ * @param {Object } resolveOptions options
28
+ * @returns {Resolver } the resolver
29
+ */
23
30
get ( type , resolveOptions ) {
24
- const cachedResolver = this . cache1 . get ( resolveOptions ) ;
25
- if ( cachedResolver ) return cachedResolver ( ) ;
26
- const ident = `${ type } |${ JSON . stringify ( resolveOptions ) } ` ;
27
- const resolver = this . cache2 . get ( ident ) ;
28
- if ( resolver ) return resolver ;
31
+ let typedCaches = this . cache . get ( type ) ;
32
+ if ( ! typedCaches ) {
33
+ typedCaches = {
34
+ direct : new WeakMap ( ) ,
35
+ stringified : new Map ( )
36
+ } ;
37
+ this . cache . set ( type , typedCaches ) ;
38
+ }
39
+ const cachedResolver = typedCaches . direct . get ( resolveOptions ) ;
40
+ if ( cachedResolver ) return cachedResolver ;
41
+ const ident = JSON . stringify ( resolveOptions ) ;
42
+ const resolver = typedCaches . stringified . get ( ident ) ;
43
+ if ( resolver ) {
44
+ typedCaches . direct . set ( resolveOptions , resolver ) ;
45
+ return resolver ;
46
+ }
29
47
const newResolver = this . _create ( type , resolveOptions ) ;
30
- this . cache2 . set ( ident , newResolver ) ;
48
+ typedCaches . direct . set ( resolveOptions , newResolver ) ;
49
+ typedCaches . stringified . set ( ident , newResolver ) ;
31
50
return newResolver ;
32
51
}
33
52
53
+ /**
54
+ * @param {string } type type of resolver
55
+ * @param {Object } resolveOptions options
56
+ * @returns {Resolver } the resolver
57
+ */
34
58
_create ( type , resolveOptions ) {
35
59
resolveOptions = this . hooks . resolveOptions . for ( type ) . call ( resolveOptions ) ;
36
60
const resolver = Factory . createResolver ( resolveOptions ) ;
0 commit comments