gameStatusUpdate/node_modules/webpack/lib/cache/ResolverCachePlugin.js

441 lines
13 KiB
JavaScript
Raw Normal View History

2024-09-18 08:27:46 -07:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const LazySet = require("../util/LazySet");
const makeSerializable = require("../util/makeSerializable");
2024-11-04 11:06:36 -08:00
/** @typedef {import("enhanced-resolve").ResolveContext} ResolveContext */
/** @typedef {import("enhanced-resolve").ResolveOptions} ResolveOptions */
/** @typedef {import("enhanced-resolve").ResolveRequest} ResolveRequest */
2024-09-18 08:27:46 -07:00
/** @typedef {import("enhanced-resolve").Resolver} Resolver */
/** @typedef {import("../CacheFacade").ItemCacheFacade} ItemCacheFacade */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../FileSystemInfo")} FileSystemInfo */
/** @typedef {import("../FileSystemInfo").Snapshot} Snapshot */
2024-11-04 11:06:36 -08:00
/** @typedef {import("../FileSystemInfo").SnapshotOptions} SnapshotOptions */
/** @typedef {import("../ResolverFactory").ResolveOptionsWithDependencyType} ResolveOptionsWithDependencyType */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/**
* @template T
* @typedef {import("tapable").SyncHook<T>} SyncHook
*/
2024-09-18 08:27:46 -07:00
class CacheEntry {
2024-11-04 11:06:36 -08:00
/**
* @param {ResolveRequest} result result
* @param {Snapshot} snapshot snapshot
*/
2024-09-18 08:27:46 -07:00
constructor(result, snapshot) {
this.result = result;
this.snapshot = snapshot;
}
2024-11-04 11:06:36 -08:00
/**
* @param {ObjectSerializerContext} context context
*/
2024-09-18 08:27:46 -07:00
serialize({ write }) {
write(this.result);
write(this.snapshot);
}
2024-11-04 11:06:36 -08:00
/**
* @param {ObjectDeserializerContext} context context
*/
2024-09-18 08:27:46 -07:00
deserialize({ read }) {
this.result = read();
this.snapshot = read();
}
}
makeSerializable(CacheEntry, "webpack/lib/cache/ResolverCachePlugin");
/**
* @template T
* @param {Set<T> | LazySet<T>} set set to add items to
2024-11-04 11:06:36 -08:00
* @param {Set<T> | LazySet<T> | Iterable<T>} otherSet set to add items from
2024-09-18 08:27:46 -07:00
* @returns {void}
*/
const addAllToSet = (set, otherSet) => {
if (set instanceof LazySet) {
set.addAll(otherSet);
} else {
for (const item of otherSet) {
set.add(item);
}
}
};
/**
2024-11-04 11:06:36 -08:00
* @template {object} T
* @param {T} object an object
2024-09-18 08:27:46 -07:00
* @param {boolean} excludeContext if true, context is not included in string
* @returns {string} stringified version
*/
const objectToString = (object, excludeContext) => {
let str = "";
for (const key in object) {
if (excludeContext && key === "context") continue;
const value = object[key];
str +=
typeof value === "object" && value !== null
? `|${key}=[${objectToString(value, false)}|]`
: `|${key}=|${value}`;
}
return str;
};
class ResolverCachePlugin {
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
const cache = compiler.getCache("ResolverCachePlugin");
/** @type {FileSystemInfo} */
let fileSystemInfo;
2024-11-04 11:06:36 -08:00
/** @type {SnapshotOptions | undefined} */
2024-09-18 08:27:46 -07:00
let snapshotOptions;
let realResolves = 0;
let cachedResolves = 0;
let cacheInvalidResolves = 0;
let concurrentResolves = 0;
compiler.hooks.thisCompilation.tap("ResolverCachePlugin", compilation => {
snapshotOptions = compilation.options.snapshot.resolve;
fileSystemInfo = compilation.fileSystemInfo;
compilation.hooks.finishModules.tap("ResolverCachePlugin", () => {
if (realResolves + cachedResolves > 0) {
const logger = compilation.getLogger("webpack.ResolverCachePlugin");
logger.log(
`${Math.round(
(100 * realResolves) / (realResolves + cachedResolves)
)}% really resolved (${realResolves} real resolves with ${cacheInvalidResolves} cached but invalid, ${cachedResolves} cached valid, ${concurrentResolves} concurrent)`
);
realResolves = 0;
cachedResolves = 0;
cacheInvalidResolves = 0;
concurrentResolves = 0;
}
});
});
2024-11-04 11:06:36 -08:00
/** @typedef {function((Error | null)=, ResolveRequest=): void} Callback */
/** @typedef {ResolveRequest & { _ResolverCachePluginCacheMiss: true }} ResolveRequestWithCacheMiss */
2024-09-18 08:27:46 -07:00
/**
* @param {ItemCacheFacade} itemCache cache
* @param {Resolver} resolver the resolver
2024-11-04 11:06:36 -08:00
* @param {ResolveContext} resolveContext context for resolving meta info
* @param {ResolveRequest} request the request info object
* @param {Callback} callback callback function
2024-09-18 08:27:46 -07:00
* @returns {void}
*/
const doRealResolve = (
itemCache,
resolver,
resolveContext,
request,
callback
) => {
realResolves++;
2024-11-04 11:06:36 -08:00
const newRequest =
/** @type {ResolveRequestWithCacheMiss} */
({
_ResolverCachePluginCacheMiss: true,
...request
});
/** @type {ResolveContext} */
2024-09-18 08:27:46 -07:00
const newResolveContext = {
...resolveContext,
stack: new Set(),
/** @type {LazySet<string>} */
missingDependencies: new LazySet(),
/** @type {LazySet<string>} */
fileDependencies: new LazySet(),
/** @type {LazySet<string>} */
contextDependencies: new LazySet()
};
2024-11-04 11:06:36 -08:00
/** @type {ResolveRequest[] | undefined} */
2024-09-18 08:27:46 -07:00
let yieldResult;
let withYield = false;
if (typeof newResolveContext.yield === "function") {
yieldResult = [];
withYield = true;
2024-11-04 11:06:36 -08:00
newResolveContext.yield = obj =>
/** @type {ResolveRequest[]} */
(yieldResult).push(obj);
2024-09-18 08:27:46 -07:00
}
2024-11-04 11:06:36 -08:00
/**
* @param {"fileDependencies" | "contextDependencies" | "missingDependencies"} key key
*/
2024-09-18 08:27:46 -07:00
const propagate = key => {
if (resolveContext[key]) {
2024-11-04 11:06:36 -08:00
addAllToSet(
/** @type {Set<string>} */ (resolveContext[key]),
/** @type {Set<string>} */ (newResolveContext[key])
);
2024-09-18 08:27:46 -07:00
}
};
const resolveTime = Date.now();
resolver.doResolve(
resolver.hooks.resolve,
newRequest,
"Cache miss",
newResolveContext,
(err, result) => {
propagate("fileDependencies");
propagate("contextDependencies");
propagate("missingDependencies");
if (err) return callback(err);
const fileDependencies = newResolveContext.fileDependencies;
const contextDependencies = newResolveContext.contextDependencies;
const missingDependencies = newResolveContext.missingDependencies;
fileSystemInfo.createSnapshot(
resolveTime,
2024-11-04 11:06:36 -08:00
/** @type {Set<string>} */
(fileDependencies),
/** @type {Set<string>} */
(contextDependencies),
/** @type {Set<string>} */
(missingDependencies),
2024-09-18 08:27:46 -07:00
snapshotOptions,
(err, snapshot) => {
if (err) return callback(err);
const resolveResult = withYield ? yieldResult : result;
// since we intercept resolve hook
// we still can get result in callback
2024-11-04 11:06:36 -08:00
if (withYield && result)
/** @type {ResolveRequest[]} */ (yieldResult).push(result);
2024-09-18 08:27:46 -07:00
if (!snapshot) {
2024-11-04 11:06:36 -08:00
if (resolveResult)
return callback(
null,
/** @type {ResolveRequest} */
(resolveResult)
);
2024-09-18 08:27:46 -07:00
return callback();
}
itemCache.store(
2024-11-04 11:06:36 -08:00
new CacheEntry(
/** @type {ResolveRequest} */
(resolveResult),
snapshot
),
2024-09-18 08:27:46 -07:00
storeErr => {
if (storeErr) return callback(storeErr);
2024-11-04 11:06:36 -08:00
if (resolveResult)
return callback(
null,
/** @type {ResolveRequest} */
(resolveResult)
);
2024-09-18 08:27:46 -07:00
callback();
}
);
}
);
}
);
};
compiler.resolverFactory.hooks.resolver.intercept({
factory(type, hook) {
2024-11-04 11:06:36 -08:00
/** @type {Map<string, (function(Error=, ResolveRequest=): void)[]>} */
2024-09-18 08:27:46 -07:00
const activeRequests = new Map();
2024-11-04 11:06:36 -08:00
/** @type {Map<string, [function(Error=, ResolveRequest=): void, NonNullable<ResolveContext["yield"]>][]>} */
2024-09-18 08:27:46 -07:00
const activeRequestsWithYield = new Map();
2024-11-04 11:06:36 -08:00
/** @type {SyncHook<[Resolver, ResolveOptions, ResolveOptionsWithDependencyType]>} */
(hook).tap("ResolverCachePlugin", (resolver, options, userOptions) => {
if (/** @type {TODO} */ (options).cache !== true) return;
const optionsIdent = objectToString(userOptions, false);
const cacheWithContext =
options.cacheWithContext !== undefined
? options.cacheWithContext
: false;
resolver.hooks.resolve.tapAsync(
{
name: "ResolverCachePlugin",
stage: -100
},
(request, resolveContext, callback) => {
if (
/** @type {ResolveRequestWithCacheMiss} */
(request)._ResolverCachePluginCacheMiss ||
!fileSystemInfo
) {
return callback();
}
const withYield = typeof resolveContext.yield === "function";
const identifier = `${type}${
withYield ? "|yield" : "|default"
}${optionsIdent}${objectToString(request, !cacheWithContext)}`;
2024-09-18 08:27:46 -07:00
2024-11-04 11:06:36 -08:00
if (withYield) {
const activeRequest = activeRequestsWithYield.get(identifier);
if (activeRequest) {
activeRequest[0].push(callback);
activeRequest[1].push(
/** @type {NonNullable<ResolveContext["yield"]>} */
(resolveContext.yield)
);
return;
2024-09-18 08:27:46 -07:00
}
2024-11-04 11:06:36 -08:00
} else {
const activeRequest = activeRequests.get(identifier);
if (activeRequest) {
activeRequest.push(callback);
return;
}
}
const itemCache = cache.getItemCache(identifier, null);
/** @type {Callback[] | false | undefined} */
let callbacks;
/** @type {NonNullable<ResolveContext["yield"]>[] | undefined} */
let yields;
/**
* @type {function((Error | null)=, ResolveRequest | ResolveRequest[]=): void}
*/
const done = withYield
? (err, result) => {
if (callbacks === undefined) {
if (err) {
callback(err);
2024-09-18 08:27:46 -07:00
} else {
2024-11-04 11:06:36 -08:00
if (result)
for (const r of /** @type {ResolveRequest[]} */ (
result
)) {
/** @type {NonNullable<ResolveContext["yield"]>} */
(resolveContext.yield)(r);
2024-09-18 08:27:46 -07:00
}
2024-11-04 11:06:36 -08:00
callback(null, null);
2024-09-18 08:27:46 -07:00
}
2024-11-04 11:06:36 -08:00
yields = undefined;
callbacks = false;
} else {
const definedCallbacks =
/** @type {Callback[]} */
(callbacks);
2024-09-18 08:27:46 -07:00
2024-11-04 11:06:36 -08:00
if (err) {
for (const cb of definedCallbacks) cb(err);
} else {
for (let i = 0; i < definedCallbacks.length; i++) {
const cb = definedCallbacks[i];
const yield_ =
/** @type {NonNullable<ResolveContext["yield"]>[]} */
(yields)[i];
if (result)
for (const r of /** @type {ResolveRequest[]} */ (
result
))
yield_(r);
cb(null, null);
2024-09-18 08:27:46 -07:00
}
}
2024-11-04 11:06:36 -08:00
activeRequestsWithYield.delete(identifier);
yields = undefined;
callbacks = false;
}
2024-09-18 08:27:46 -07:00
}
2024-11-04 11:06:36 -08:00
: (err, result) => {
if (callbacks === undefined) {
callback(err, /** @type {ResolveRequest} */ (result));
callbacks = false;
} else {
for (const callback of /** @type {Callback[]} */ (
callbacks
)) {
callback(err, /** @type {ResolveRequest} */ (result));
}
activeRequests.delete(identifier);
callbacks = false;
}
};
/**
* @param {(Error | null)=} err error if any
* @param {(CacheEntry | null)=} cacheEntry cache entry
* @returns {void}
*/
const processCacheResult = (err, cacheEntry) => {
if (err) return done(err);
if (cacheEntry) {
const { snapshot, result } = cacheEntry;
fileSystemInfo.checkSnapshotValid(snapshot, (err, valid) => {
if (err || !valid) {
cacheInvalidResolves++;
return doRealResolve(
itemCache,
resolver,
resolveContext,
request,
done
);
}
cachedResolves++;
if (resolveContext.missingDependencies) {
addAllToSet(
/** @type {Set<string>} */
(resolveContext.missingDependencies),
snapshot.getMissingIterable()
);
}
if (resolveContext.fileDependencies) {
addAllToSet(
/** @type {Set<string>} */
(resolveContext.fileDependencies),
snapshot.getFileIterable()
);
}
if (resolveContext.contextDependencies) {
addAllToSet(
/** @type {Set<string>} */
(resolveContext.contextDependencies),
snapshot.getContextIterable()
);
}
done(null, result);
});
} else {
doRealResolve(
itemCache,
resolver,
resolveContext,
request,
done
2024-09-18 08:27:46 -07:00
);
}
2024-11-04 11:06:36 -08:00
};
itemCache.get(processCacheResult);
if (withYield && callbacks === undefined) {
callbacks = [callback];
yields = [
/** @type {NonNullable<ResolveContext["yield"]>} */
(resolveContext.yield)
];
activeRequestsWithYield.set(
identifier,
/** @type {[any, any]} */ ([callbacks, yields])
);
} else if (callbacks === undefined) {
callbacks = [callback];
activeRequests.set(identifier, callbacks);
2024-09-18 08:27:46 -07:00
}
2024-11-04 11:06:36 -08:00
}
);
});
2024-09-18 08:27:46 -07:00
return hook;
}
});
}
}
module.exports = ResolverCachePlugin;