Skip to content

Commit 9964a82

Browse files
committed
fix resolver cache
1 parent f60c9a7 commit 9964a82

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

lib/ResolverFactory.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
const Factory = require("enhanced-resolve").ResolverFactory;
99
const { HookMap, SyncHook, SyncWaterfallHook } = require("tapable");
1010

11+
/** @typedef {import("enhanced-resolve/lib/Resolver")} Resolver */
12+
1113
module.exports = class ResolverFactory {
1214
constructor() {
1315
this.hooks = Object.freeze({
@@ -16,21 +18,43 @@ module.exports = class ResolverFactory {
1618
),
1719
resolver: new HookMap(() => new SyncHook(["resolver", "resolveOptions"]))
1820
});
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();
2123
}
2224

25+
/**
26+
* @param {string} type type of resolver
27+
* @param {Object} resolveOptions options
28+
* @returns {Resolver} the resolver
29+
*/
2330
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+
}
2947
const newResolver = this._create(type, resolveOptions);
30-
this.cache2.set(ident, newResolver);
48+
typedCaches.direct.set(resolveOptions, newResolver);
49+
typedCaches.stringified.set(ident, newResolver);
3150
return newResolver;
3251
}
3352

53+
/**
54+
* @param {string} type type of resolver
55+
* @param {Object} resolveOptions options
56+
* @returns {Resolver} the resolver
57+
*/
3458
_create(type, resolveOptions) {
3559
resolveOptions = this.hooks.resolveOptions.for(type).call(resolveOptions);
3660
const resolver = Factory.createResolver(resolveOptions);

0 commit comments

Comments
 (0)