[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const LazySet = require("../util/LazySet");
|
---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 10 |
|
---|
| 11 | /** @typedef {import("enhanced-resolve").ResolveContext} ResolveContext */
|
---|
| 12 | /** @typedef {import("enhanced-resolve").ResolveOptions} ResolveOptions */
|
---|
| 13 | /** @typedef {import("enhanced-resolve").ResolveRequest} ResolveRequest */
|
---|
| 14 | /** @typedef {import("enhanced-resolve").Resolver} Resolver */
|
---|
| 15 | /** @typedef {import("../CacheFacade").ItemCacheFacade} ItemCacheFacade */
|
---|
| 16 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 17 | /** @typedef {import("../FileSystemInfo")} FileSystemInfo */
|
---|
| 18 | /** @typedef {import("../FileSystemInfo").Snapshot} Snapshot */
|
---|
| 19 | /** @typedef {import("../FileSystemInfo").SnapshotOptions} SnapshotOptions */
|
---|
| 20 | /** @typedef {import("../ResolverFactory").ResolveOptionsWithDependencyType} ResolveOptionsWithDependencyType */
|
---|
| 21 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 22 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * @template T
|
---|
| 26 | * @typedef {import("tapable").SyncHook<T>} SyncHook
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | class CacheEntry {
|
---|
| 30 | /**
|
---|
| 31 | * @param {ResolveRequest} result result
|
---|
| 32 | * @param {Snapshot} snapshot snapshot
|
---|
| 33 | */
|
---|
| 34 | constructor(result, snapshot) {
|
---|
| 35 | this.result = result;
|
---|
| 36 | this.snapshot = snapshot;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * @param {ObjectSerializerContext} context context
|
---|
| 41 | */
|
---|
| 42 | serialize({ write }) {
|
---|
| 43 | write(this.result);
|
---|
| 44 | write(this.snapshot);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * @param {ObjectDeserializerContext} context context
|
---|
| 49 | */
|
---|
| 50 | deserialize({ read }) {
|
---|
| 51 | this.result = read();
|
---|
| 52 | this.snapshot = read();
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | makeSerializable(CacheEntry, "webpack/lib/cache/ResolverCachePlugin");
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * @template T
|
---|
| 60 | * @param {Set<T> | LazySet<T>} set set to add items to
|
---|
| 61 | * @param {Set<T> | LazySet<T> | Iterable<T>} otherSet set to add items from
|
---|
| 62 | * @returns {void}
|
---|
| 63 | */
|
---|
| 64 | const addAllToSet = (set, otherSet) => {
|
---|
| 65 | if (set instanceof LazySet) {
|
---|
| 66 | set.addAll(otherSet);
|
---|
| 67 | } else {
|
---|
| 68 | for (const item of otherSet) {
|
---|
| 69 | set.add(item);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | };
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * @template {object} T
|
---|
| 76 | * @param {T} object an object
|
---|
| 77 | * @param {boolean} excludeContext if true, context is not included in string
|
---|
| 78 | * @returns {string} stringified version
|
---|
| 79 | */
|
---|
| 80 | const objectToString = (object, excludeContext) => {
|
---|
| 81 | let str = "";
|
---|
| 82 | for (const key in object) {
|
---|
| 83 | if (excludeContext && key === "context") continue;
|
---|
| 84 | const value = object[key];
|
---|
| 85 | str +=
|
---|
| 86 | typeof value === "object" && value !== null
|
---|
| 87 | ? `|${key}=[${objectToString(value, false)}|]`
|
---|
| 88 | : `|${key}=|${value}`;
|
---|
| 89 | }
|
---|
| 90 | return str;
|
---|
| 91 | };
|
---|
| 92 |
|
---|
| 93 | class ResolverCachePlugin {
|
---|
| 94 | /**
|
---|
| 95 | * Apply the plugin
|
---|
| 96 | * @param {Compiler} compiler the compiler instance
|
---|
| 97 | * @returns {void}
|
---|
| 98 | */
|
---|
| 99 | apply(compiler) {
|
---|
| 100 | const cache = compiler.getCache("ResolverCachePlugin");
|
---|
| 101 | /** @type {FileSystemInfo} */
|
---|
| 102 | let fileSystemInfo;
|
---|
| 103 | /** @type {SnapshotOptions | undefined} */
|
---|
| 104 | let snapshotOptions;
|
---|
| 105 | let realResolves = 0;
|
---|
| 106 | let cachedResolves = 0;
|
---|
| 107 | let cacheInvalidResolves = 0;
|
---|
| 108 | let concurrentResolves = 0;
|
---|
| 109 | compiler.hooks.thisCompilation.tap("ResolverCachePlugin", compilation => {
|
---|
| 110 | snapshotOptions = compilation.options.snapshot.resolve;
|
---|
| 111 | fileSystemInfo = compilation.fileSystemInfo;
|
---|
| 112 | compilation.hooks.finishModules.tap("ResolverCachePlugin", () => {
|
---|
| 113 | if (realResolves + cachedResolves > 0) {
|
---|
| 114 | const logger = compilation.getLogger("webpack.ResolverCachePlugin");
|
---|
| 115 | logger.log(
|
---|
| 116 | `${Math.round(
|
---|
| 117 | (100 * realResolves) / (realResolves + cachedResolves)
|
---|
| 118 | )}% really resolved (${realResolves} real resolves with ${cacheInvalidResolves} cached but invalid, ${cachedResolves} cached valid, ${concurrentResolves} concurrent)`
|
---|
| 119 | );
|
---|
| 120 | realResolves = 0;
|
---|
| 121 | cachedResolves = 0;
|
---|
| 122 | cacheInvalidResolves = 0;
|
---|
| 123 | concurrentResolves = 0;
|
---|
| 124 | }
|
---|
| 125 | });
|
---|
| 126 | });
|
---|
| 127 |
|
---|
| 128 | /** @typedef {function((Error | null)=, ResolveRequest=): void} Callback */
|
---|
| 129 | /** @typedef {ResolveRequest & { _ResolverCachePluginCacheMiss: true }} ResolveRequestWithCacheMiss */
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | * @param {ItemCacheFacade} itemCache cache
|
---|
| 133 | * @param {Resolver} resolver the resolver
|
---|
| 134 | * @param {ResolveContext} resolveContext context for resolving meta info
|
---|
| 135 | * @param {ResolveRequest} request the request info object
|
---|
| 136 | * @param {Callback} callback callback function
|
---|
| 137 | * @returns {void}
|
---|
| 138 | */
|
---|
| 139 | const doRealResolve = (
|
---|
| 140 | itemCache,
|
---|
| 141 | resolver,
|
---|
| 142 | resolveContext,
|
---|
| 143 | request,
|
---|
| 144 | callback
|
---|
| 145 | ) => {
|
---|
| 146 | realResolves++;
|
---|
| 147 | const newRequest =
|
---|
| 148 | /** @type {ResolveRequestWithCacheMiss} */
|
---|
| 149 | ({
|
---|
| 150 | _ResolverCachePluginCacheMiss: true,
|
---|
| 151 | ...request
|
---|
| 152 | });
|
---|
| 153 | /** @type {ResolveContext} */
|
---|
| 154 | const newResolveContext = {
|
---|
| 155 | ...resolveContext,
|
---|
| 156 | stack: new Set(),
|
---|
| 157 | /** @type {LazySet<string>} */
|
---|
| 158 | missingDependencies: new LazySet(),
|
---|
| 159 | /** @type {LazySet<string>} */
|
---|
| 160 | fileDependencies: new LazySet(),
|
---|
| 161 | /** @type {LazySet<string>} */
|
---|
| 162 | contextDependencies: new LazySet()
|
---|
| 163 | };
|
---|
| 164 | /** @type {ResolveRequest[] | undefined} */
|
---|
| 165 | let yieldResult;
|
---|
| 166 | let withYield = false;
|
---|
| 167 | if (typeof newResolveContext.yield === "function") {
|
---|
| 168 | yieldResult = [];
|
---|
| 169 | withYield = true;
|
---|
| 170 | newResolveContext.yield = obj =>
|
---|
| 171 | /** @type {ResolveRequest[]} */
|
---|
| 172 | (yieldResult).push(obj);
|
---|
| 173 | }
|
---|
| 174 | /**
|
---|
| 175 | * @param {"fileDependencies" | "contextDependencies" | "missingDependencies"} key key
|
---|
| 176 | */
|
---|
| 177 | const propagate = key => {
|
---|
| 178 | if (resolveContext[key]) {
|
---|
| 179 | addAllToSet(
|
---|
| 180 | /** @type {Set<string>} */ (resolveContext[key]),
|
---|
| 181 | /** @type {Set<string>} */ (newResolveContext[key])
|
---|
| 182 | );
|
---|
| 183 | }
|
---|
| 184 | };
|
---|
| 185 | const resolveTime = Date.now();
|
---|
| 186 | resolver.doResolve(
|
---|
| 187 | resolver.hooks.resolve,
|
---|
| 188 | newRequest,
|
---|
| 189 | "Cache miss",
|
---|
| 190 | newResolveContext,
|
---|
| 191 | (err, result) => {
|
---|
| 192 | propagate("fileDependencies");
|
---|
| 193 | propagate("contextDependencies");
|
---|
| 194 | propagate("missingDependencies");
|
---|
| 195 | if (err) return callback(err);
|
---|
| 196 | const fileDependencies = newResolveContext.fileDependencies;
|
---|
| 197 | const contextDependencies = newResolveContext.contextDependencies;
|
---|
| 198 | const missingDependencies = newResolveContext.missingDependencies;
|
---|
| 199 | fileSystemInfo.createSnapshot(
|
---|
| 200 | resolveTime,
|
---|
| 201 | /** @type {Set<string>} */
|
---|
| 202 | (fileDependencies),
|
---|
| 203 | /** @type {Set<string>} */
|
---|
| 204 | (contextDependencies),
|
---|
| 205 | /** @type {Set<string>} */
|
---|
| 206 | (missingDependencies),
|
---|
| 207 | snapshotOptions,
|
---|
| 208 | (err, snapshot) => {
|
---|
| 209 | if (err) return callback(err);
|
---|
| 210 | const resolveResult = withYield ? yieldResult : result;
|
---|
| 211 | // since we intercept resolve hook
|
---|
| 212 | // we still can get result in callback
|
---|
| 213 | if (withYield && result)
|
---|
| 214 | /** @type {ResolveRequest[]} */ (yieldResult).push(result);
|
---|
| 215 | if (!snapshot) {
|
---|
| 216 | if (resolveResult)
|
---|
| 217 | return callback(
|
---|
| 218 | null,
|
---|
| 219 | /** @type {ResolveRequest} */
|
---|
| 220 | (resolveResult)
|
---|
| 221 | );
|
---|
| 222 | return callback();
|
---|
| 223 | }
|
---|
| 224 | itemCache.store(
|
---|
| 225 | new CacheEntry(
|
---|
| 226 | /** @type {ResolveRequest} */
|
---|
| 227 | (resolveResult),
|
---|
| 228 | snapshot
|
---|
| 229 | ),
|
---|
| 230 | storeErr => {
|
---|
| 231 | if (storeErr) return callback(storeErr);
|
---|
| 232 | if (resolveResult)
|
---|
| 233 | return callback(
|
---|
| 234 | null,
|
---|
| 235 | /** @type {ResolveRequest} */
|
---|
| 236 | (resolveResult)
|
---|
| 237 | );
|
---|
| 238 | callback();
|
---|
| 239 | }
|
---|
| 240 | );
|
---|
| 241 | }
|
---|
| 242 | );
|
---|
| 243 | }
|
---|
| 244 | );
|
---|
| 245 | };
|
---|
| 246 | compiler.resolverFactory.hooks.resolver.intercept({
|
---|
| 247 | factory(type, hook) {
|
---|
| 248 | /** @type {Map<string, (function(Error=, ResolveRequest=): void)[]>} */
|
---|
| 249 | const activeRequests = new Map();
|
---|
| 250 | /** @type {Map<string, [function(Error=, ResolveRequest=): void, NonNullable<ResolveContext["yield"]>][]>} */
|
---|
| 251 | const activeRequestsWithYield = new Map();
|
---|
| 252 | /** @type {SyncHook<[Resolver, ResolveOptions, ResolveOptionsWithDependencyType]>} */
|
---|
| 253 | (hook).tap("ResolverCachePlugin", (resolver, options, userOptions) => {
|
---|
| 254 | if (/** @type {TODO} */ (options).cache !== true) return;
|
---|
| 255 | const optionsIdent = objectToString(userOptions, false);
|
---|
| 256 | const cacheWithContext =
|
---|
| 257 | options.cacheWithContext !== undefined
|
---|
| 258 | ? options.cacheWithContext
|
---|
| 259 | : false;
|
---|
| 260 | resolver.hooks.resolve.tapAsync(
|
---|
| 261 | {
|
---|
| 262 | name: "ResolverCachePlugin",
|
---|
| 263 | stage: -100
|
---|
| 264 | },
|
---|
| 265 | (request, resolveContext, callback) => {
|
---|
| 266 | if (
|
---|
| 267 | /** @type {ResolveRequestWithCacheMiss} */
|
---|
| 268 | (request)._ResolverCachePluginCacheMiss ||
|
---|
| 269 | !fileSystemInfo
|
---|
| 270 | ) {
|
---|
| 271 | return callback();
|
---|
| 272 | }
|
---|
| 273 | const withYield = typeof resolveContext.yield === "function";
|
---|
| 274 | const identifier = `${type}${
|
---|
| 275 | withYield ? "|yield" : "|default"
|
---|
| 276 | }${optionsIdent}${objectToString(request, !cacheWithContext)}`;
|
---|
| 277 |
|
---|
| 278 | if (withYield) {
|
---|
| 279 | const activeRequest = activeRequestsWithYield.get(identifier);
|
---|
| 280 | if (activeRequest) {
|
---|
| 281 | activeRequest[0].push(callback);
|
---|
| 282 | activeRequest[1].push(
|
---|
| 283 | /** @type {NonNullable<ResolveContext["yield"]>} */
|
---|
| 284 | (resolveContext.yield)
|
---|
| 285 | );
|
---|
| 286 | return;
|
---|
| 287 | }
|
---|
| 288 | } else {
|
---|
| 289 | const activeRequest = activeRequests.get(identifier);
|
---|
| 290 | if (activeRequest) {
|
---|
| 291 | activeRequest.push(callback);
|
---|
| 292 | return;
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 | const itemCache = cache.getItemCache(identifier, null);
|
---|
| 296 | /** @type {Callback[] | false | undefined} */
|
---|
| 297 | let callbacks;
|
---|
| 298 | /** @type {NonNullable<ResolveContext["yield"]>[] | undefined} */
|
---|
| 299 | let yields;
|
---|
| 300 |
|
---|
| 301 | /**
|
---|
| 302 | * @type {function((Error | null)=, ResolveRequest | ResolveRequest[]=): void}
|
---|
| 303 | */
|
---|
| 304 | const done = withYield
|
---|
| 305 | ? (err, result) => {
|
---|
| 306 | if (callbacks === undefined) {
|
---|
| 307 | if (err) {
|
---|
| 308 | callback(err);
|
---|
| 309 | } else {
|
---|
| 310 | if (result)
|
---|
| 311 | for (const r of /** @type {ResolveRequest[]} */ (
|
---|
| 312 | result
|
---|
| 313 | )) {
|
---|
| 314 | /** @type {NonNullable<ResolveContext["yield"]>} */
|
---|
| 315 | (resolveContext.yield)(r);
|
---|
| 316 | }
|
---|
| 317 | callback(null, null);
|
---|
| 318 | }
|
---|
| 319 | yields = undefined;
|
---|
| 320 | callbacks = false;
|
---|
| 321 | } else {
|
---|
| 322 | const definedCallbacks =
|
---|
| 323 | /** @type {Callback[]} */
|
---|
| 324 | (callbacks);
|
---|
| 325 |
|
---|
| 326 | if (err) {
|
---|
| 327 | for (const cb of definedCallbacks) cb(err);
|
---|
| 328 | } else {
|
---|
| 329 | for (let i = 0; i < definedCallbacks.length; i++) {
|
---|
| 330 | const cb = definedCallbacks[i];
|
---|
| 331 | const yield_ =
|
---|
| 332 | /** @type {NonNullable<ResolveContext["yield"]>[]} */
|
---|
| 333 | (yields)[i];
|
---|
| 334 | if (result)
|
---|
| 335 | for (const r of /** @type {ResolveRequest[]} */ (
|
---|
| 336 | result
|
---|
| 337 | ))
|
---|
| 338 | yield_(r);
|
---|
| 339 | cb(null, null);
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 | activeRequestsWithYield.delete(identifier);
|
---|
| 343 | yields = undefined;
|
---|
| 344 | callbacks = false;
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 | : (err, result) => {
|
---|
| 348 | if (callbacks === undefined) {
|
---|
| 349 | callback(err, /** @type {ResolveRequest} */ (result));
|
---|
| 350 | callbacks = false;
|
---|
| 351 | } else {
|
---|
| 352 | for (const callback of /** @type {Callback[]} */ (
|
---|
| 353 | callbacks
|
---|
| 354 | )) {
|
---|
| 355 | callback(err, /** @type {ResolveRequest} */ (result));
|
---|
| 356 | }
|
---|
| 357 | activeRequests.delete(identifier);
|
---|
| 358 | callbacks = false;
|
---|
| 359 | }
|
---|
| 360 | };
|
---|
| 361 | /**
|
---|
| 362 | * @param {(Error | null)=} err error if any
|
---|
| 363 | * @param {(CacheEntry | null)=} cacheEntry cache entry
|
---|
| 364 | * @returns {void}
|
---|
| 365 | */
|
---|
| 366 | const processCacheResult = (err, cacheEntry) => {
|
---|
| 367 | if (err) return done(err);
|
---|
| 368 |
|
---|
| 369 | if (cacheEntry) {
|
---|
| 370 | const { snapshot, result } = cacheEntry;
|
---|
| 371 | fileSystemInfo.checkSnapshotValid(snapshot, (err, valid) => {
|
---|
| 372 | if (err || !valid) {
|
---|
| 373 | cacheInvalidResolves++;
|
---|
| 374 | return doRealResolve(
|
---|
| 375 | itemCache,
|
---|
| 376 | resolver,
|
---|
| 377 | resolveContext,
|
---|
| 378 | request,
|
---|
| 379 | done
|
---|
| 380 | );
|
---|
| 381 | }
|
---|
| 382 | cachedResolves++;
|
---|
| 383 | if (resolveContext.missingDependencies) {
|
---|
| 384 | addAllToSet(
|
---|
| 385 | /** @type {Set<string>} */
|
---|
| 386 | (resolveContext.missingDependencies),
|
---|
| 387 | snapshot.getMissingIterable()
|
---|
| 388 | );
|
---|
| 389 | }
|
---|
| 390 | if (resolveContext.fileDependencies) {
|
---|
| 391 | addAllToSet(
|
---|
| 392 | /** @type {Set<string>} */
|
---|
| 393 | (resolveContext.fileDependencies),
|
---|
| 394 | snapshot.getFileIterable()
|
---|
| 395 | );
|
---|
| 396 | }
|
---|
| 397 | if (resolveContext.contextDependencies) {
|
---|
| 398 | addAllToSet(
|
---|
| 399 | /** @type {Set<string>} */
|
---|
| 400 | (resolveContext.contextDependencies),
|
---|
| 401 | snapshot.getContextIterable()
|
---|
| 402 | );
|
---|
| 403 | }
|
---|
| 404 | done(null, result);
|
---|
| 405 | });
|
---|
| 406 | } else {
|
---|
| 407 | doRealResolve(
|
---|
| 408 | itemCache,
|
---|
| 409 | resolver,
|
---|
| 410 | resolveContext,
|
---|
| 411 | request,
|
---|
| 412 | done
|
---|
| 413 | );
|
---|
| 414 | }
|
---|
| 415 | };
|
---|
| 416 | itemCache.get(processCacheResult);
|
---|
| 417 | if (withYield && callbacks === undefined) {
|
---|
| 418 | callbacks = [callback];
|
---|
| 419 | yields = [
|
---|
| 420 | /** @type {NonNullable<ResolveContext["yield"]>} */
|
---|
| 421 | (resolveContext.yield)
|
---|
| 422 | ];
|
---|
| 423 | activeRequestsWithYield.set(
|
---|
| 424 | identifier,
|
---|
| 425 | /** @type {[any, any]} */ ([callbacks, yields])
|
---|
| 426 | );
|
---|
| 427 | } else if (callbacks === undefined) {
|
---|
| 428 | callbacks = [callback];
|
---|
| 429 | activeRequests.set(identifier, callbacks);
|
---|
| 430 | }
|
---|
| 431 | }
|
---|
| 432 | );
|
---|
| 433 | });
|
---|
| 434 | return hook;
|
---|
| 435 | }
|
---|
| 436 | });
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | module.exports = ResolverCachePlugin;
|
---|