mirror of https://github.com/webpack/webpack.git
fix resolver cache
This commit is contained in:
parent
f60c9a7070
commit
9964a8283f
|
@ -8,6 +8,8 @@
|
||||||
const Factory = require("enhanced-resolve").ResolverFactory;
|
const Factory = require("enhanced-resolve").ResolverFactory;
|
||||||
const { HookMap, SyncHook, SyncWaterfallHook } = require("tapable");
|
const { HookMap, SyncHook, SyncWaterfallHook } = require("tapable");
|
||||||
|
|
||||||
|
/** @typedef {import("enhanced-resolve/lib/Resolver")} Resolver */
|
||||||
|
|
||||||
module.exports = class ResolverFactory {
|
module.exports = class ResolverFactory {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.hooks = Object.freeze({
|
this.hooks = Object.freeze({
|
||||||
|
@ -16,21 +18,43 @@ module.exports = class ResolverFactory {
|
||||||
),
|
),
|
||||||
resolver: new HookMap(() => new SyncHook(["resolver", "resolveOptions"]))
|
resolver: new HookMap(() => new SyncHook(["resolver", "resolveOptions"]))
|
||||||
});
|
});
|
||||||
this.cache1 = new WeakMap();
|
/** @type {Map<string, { direct: WeakMap<Object, Resolver>, stringified: Map<string, Resolver> }>} */
|
||||||
this.cache2 = new Map();
|
this.cache = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} type type of resolver
|
||||||
|
* @param {Object} resolveOptions options
|
||||||
|
* @returns {Resolver} the resolver
|
||||||
|
*/
|
||||||
get(type, resolveOptions) {
|
get(type, resolveOptions) {
|
||||||
const cachedResolver = this.cache1.get(resolveOptions);
|
let typedCaches = this.cache.get(type);
|
||||||
if (cachedResolver) return cachedResolver();
|
if (!typedCaches) {
|
||||||
const ident = `${type}|${JSON.stringify(resolveOptions)}`;
|
typedCaches = {
|
||||||
const resolver = this.cache2.get(ident);
|
direct: new WeakMap(),
|
||||||
if (resolver) return resolver;
|
stringified: new Map()
|
||||||
|
};
|
||||||
|
this.cache.set(type, typedCaches);
|
||||||
|
}
|
||||||
|
const cachedResolver = typedCaches.direct.get(resolveOptions);
|
||||||
|
if (cachedResolver) return cachedResolver;
|
||||||
|
const ident = JSON.stringify(resolveOptions);
|
||||||
|
const resolver = typedCaches.stringified.get(ident);
|
||||||
|
if (resolver) {
|
||||||
|
typedCaches.direct.set(resolveOptions, resolver);
|
||||||
|
return resolver;
|
||||||
|
}
|
||||||
const newResolver = this._create(type, resolveOptions);
|
const newResolver = this._create(type, resolveOptions);
|
||||||
this.cache2.set(ident, newResolver);
|
typedCaches.direct.set(resolveOptions, newResolver);
|
||||||
|
typedCaches.stringified.set(ident, newResolver);
|
||||||
return newResolver;
|
return newResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} type type of resolver
|
||||||
|
* @param {Object} resolveOptions options
|
||||||
|
* @returns {Resolver} the resolver
|
||||||
|
*/
|
||||||
_create(type, resolveOptions) {
|
_create(type, resolveOptions) {
|
||||||
resolveOptions = this.hooks.resolveOptions.for(type).call(resolveOptions);
|
resolveOptions = this.hooks.resolveOptions.for(type).call(resolveOptions);
|
||||||
const resolver = Factory.createResolver(resolveOptions);
|
const resolver = Factory.createResolver(resolveOptions);
|
||||||
|
|
Loading…
Reference in New Issue