| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:55 +08:00
										 |  |  | const LazySet = require("../util/LazySet"); | 
					
						
							| 
									
										
										
										
											2020-01-28 18:01:36 +08:00
										 |  |  | const makeSerializable = require("../util/makeSerializable"); | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | /** @typedef {import("enhanced-resolve/lib/Resolver")} Resolver */ | 
					
						
							| 
									
										
										
										
											2020-07-15 17:14:28 +08:00
										 |  |  | /** @typedef {import("../CacheFacade").ItemCacheFacade} ItemCacheFacade */ | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | /** @typedef {import("../FileSystemInfo")} FileSystemInfo */ | 
					
						
							| 
									
										
										
										
											2019-07-18 05:35:05 +08:00
										 |  |  | /** @typedef {import("../FileSystemInfo").Snapshot} Snapshot */ | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-28 18:01:36 +08:00
										 |  |  | class CacheEntry { | 
					
						
							| 
									
										
										
										
											2020-08-23 03:54:34 +08:00
										 |  |  | 	constructor(result, snapshot) { | 
					
						
							| 
									
										
										
										
											2020-01-28 18:01:36 +08:00
										 |  |  | 		this.result = result; | 
					
						
							|  |  |  | 		this.snapshot = snapshot; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	serialize({ write }) { | 
					
						
							|  |  |  | 		write(this.result); | 
					
						
							|  |  |  | 		write(this.snapshot); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	deserialize({ read }) { | 
					
						
							|  |  |  | 		this.result = read(); | 
					
						
							|  |  |  | 		this.snapshot = read(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | makeSerializable(CacheEntry, "webpack/lib/cache/ResolverCachePlugin"); | 
					
						
							| 
									
										
										
										
											2019-07-18 05:35:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  * @param {Set<T> | LazySet<T>} set set to add items to | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:55 +08:00
										 |  |  |  * @param {Set<T> | LazySet<T>} otherSet set to add items from | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  |  * @returns {void} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const addAllToSet = (set, otherSet) => { | 
					
						
							| 
									
										
										
										
											2021-03-12 02:59:44 +08:00
										 |  |  | 	if (set instanceof LazySet) { | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 		set.addAll(otherSet); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		for (const item of otherSet) { | 
					
						
							|  |  |  | 			set.add(item); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 05:35:05 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-11-08 17:13:46 +08:00
										 |  |  |  * @param {Object} object an object | 
					
						
							| 
									
										
										
										
											2019-11-07 23:33:29 +08:00
										 |  |  |  * @param {boolean} excludeContext if true, context is not included in string | 
					
						
							| 
									
										
										
										
											2019-07-18 05:35:05 +08:00
										 |  |  |  * @returns {string} stringified version | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-11-08 17:13:46 +08:00
										 |  |  | const objectToString = (object, excludeContext) => { | 
					
						
							| 
									
										
										
										
											2019-01-05 04:09:36 +08:00
										 |  |  | 	let str = ""; | 
					
						
							| 
									
										
										
										
											2019-11-08 17:13:46 +08:00
										 |  |  | 	for (const key in object) { | 
					
						
							| 
									
										
										
										
											2019-11-07 23:33:29 +08:00
										 |  |  | 		if (excludeContext && key === "context") continue; | 
					
						
							| 
									
										
										
										
											2019-11-08 17:13:46 +08:00
										 |  |  | 		const value = object[key]; | 
					
						
							| 
									
										
										
										
											2019-01-05 04:09:36 +08:00
										 |  |  | 		if (typeof value === "object" && value !== null) { | 
					
						
							| 
									
										
										
										
											2020-07-15 17:14:28 +08:00
										 |  |  | 			str += `|${key}=[${objectToString(value, false)}|]`; | 
					
						
							| 
									
										
										
										
											2019-01-05 04:09:36 +08:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2020-07-15 17:14:28 +08:00
										 |  |  | 			str += `|${key}=|${value}`; | 
					
						
							| 
									
										
										
										
											2019-01-05 04:09:36 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return str; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | class ResolverCachePlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2020-07-15 17:14:28 +08:00
										 |  |  | 		const cache = compiler.getCache("ResolverCachePlugin"); | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 		/** @type {FileSystemInfo} */ | 
					
						
							|  |  |  | 		let fileSystemInfo; | 
					
						
							| 
									
										
										
										
											2020-08-26 06:36:16 +08:00
										 |  |  | 		let snapshotOptions; | 
					
						
							| 
									
										
										
										
											2019-07-26 16:42:57 +08:00
										 |  |  | 		let realResolves = 0; | 
					
						
							|  |  |  | 		let cachedResolves = 0; | 
					
						
							|  |  |  | 		let cacheInvalidResolves = 0; | 
					
						
							| 
									
										
										
										
											2019-11-08 00:29:23 +08:00
										 |  |  | 		let concurrentResolves = 0; | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap("ResolverCachePlugin", compilation => { | 
					
						
							| 
									
										
										
										
											2020-08-26 06:36:16 +08:00
										 |  |  | 			snapshotOptions = compilation.options.snapshot.resolve; | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 			fileSystemInfo = compilation.fileSystemInfo; | 
					
						
							| 
									
										
										
										
											2019-07-26 16:42:57 +08:00
										 |  |  | 			compilation.hooks.finishModules.tap("ResolverCachePlugin", () => { | 
					
						
							| 
									
										
										
										
											2019-11-01 19:17:35 +08:00
										 |  |  | 				if (realResolves + cachedResolves > 0) { | 
					
						
							|  |  |  | 					const logger = compilation.getLogger("webpack.ResolverCachePlugin"); | 
					
						
							| 
									
										
										
										
											2020-08-14 12:29:10 +08:00
										 |  |  | 					logger.log( | 
					
						
							| 
									
										
										
										
											2019-11-01 19:17:35 +08:00
										 |  |  | 						`${Math.round( | 
					
						
							|  |  |  | 							(100 * realResolves) / (realResolves + cachedResolves) | 
					
						
							| 
									
										
										
										
											2019-11-08 00:29:23 +08:00
										 |  |  | 						)}% really resolved (${realResolves} real resolves with ${cacheInvalidResolves} cached but invalid, ${cachedResolves} cached valid, ${concurrentResolves} concurrent)`
 | 
					
						
							| 
									
										
										
										
											2019-11-01 19:17:35 +08:00
										 |  |  | 					); | 
					
						
							|  |  |  | 					realResolves = 0; | 
					
						
							|  |  |  | 					cachedResolves = 0; | 
					
						
							|  |  |  | 					cacheInvalidResolves = 0; | 
					
						
							| 
									
										
										
										
											2019-11-08 00:29:23 +08:00
										 |  |  | 					concurrentResolves = 0; | 
					
						
							| 
									
										
										
										
											2019-11-01 19:17:35 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-07-26 16:42:57 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-07-18 05:35:05 +08:00
										 |  |  | 		/** | 
					
						
							| 
									
										
										
										
											2020-07-15 17:14:28 +08:00
										 |  |  | 		 * @param {ItemCacheFacade} itemCache cache | 
					
						
							| 
									
										
										
										
											2019-07-18 05:35:05 +08:00
										 |  |  | 		 * @param {Resolver} resolver the resolver | 
					
						
							|  |  |  | 		 * @param {Object} resolveContext context for resolving meta info | 
					
						
							|  |  |  | 		 * @param {Object} request the request info object | 
					
						
							| 
									
										
										
										
											2021-12-24 20:27:31 +08:00
										 |  |  | 		 * @param {function((Error | null)=, Object=): void} callback callback function | 
					
						
							| 
									
										
										
										
											2019-07-18 05:35:05 +08:00
										 |  |  | 		 * @returns {void} | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 		const doRealResolve = ( | 
					
						
							| 
									
										
										
										
											2020-07-15 17:14:28 +08:00
										 |  |  | 			itemCache, | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 			resolver, | 
					
						
							|  |  |  | 			resolveContext, | 
					
						
							|  |  |  | 			request, | 
					
						
							|  |  |  | 			callback | 
					
						
							|  |  |  | 		) => { | 
					
						
							| 
									
										
										
										
											2019-07-26 16:42:57 +08:00
										 |  |  | 			realResolves++; | 
					
						
							| 
									
										
										
										
											2019-06-19 19:16:05 +08:00
										 |  |  | 			const newRequest = { | 
					
						
							|  |  |  | 				_ResolverCachePluginCacheMiss: true, | 
					
						
							|  |  |  | 				...request | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			const newResolveContext = { | 
					
						
							|  |  |  | 				...resolveContext, | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 				stack: new Set(), | 
					
						
							| 
									
										
										
										
											2023-04-22 07:50:02 +08:00
										 |  |  | 				/** @type {LazySet<string>} */ | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:55 +08:00
										 |  |  | 				missingDependencies: new LazySet(), | 
					
						
							| 
									
										
										
										
											2023-04-22 07:50:02 +08:00
										 |  |  | 				/** @type {LazySet<string>} */ | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:55 +08:00
										 |  |  | 				fileDependencies: new LazySet(), | 
					
						
							| 
									
										
										
										
											2023-04-22 07:50:02 +08:00
										 |  |  | 				/** @type {LazySet<string>} */ | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:55 +08:00
										 |  |  | 				contextDependencies: new LazySet() | 
					
						
							| 
									
										
										
										
											2019-06-19 19:16:05 +08:00
										 |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2022-02-21 16:58:44 +08:00
										 |  |  | 			let yieldResult; | 
					
						
							| 
									
										
										
										
											2022-02-21 18:25:30 +08:00
										 |  |  | 			let withYield = false; | 
					
						
							| 
									
										
										
										
											2022-02-21 16:58:44 +08:00
										 |  |  | 			if (typeof newResolveContext.yield === "function") { | 
					
						
							|  |  |  | 				yieldResult = []; | 
					
						
							| 
									
										
										
										
											2022-02-21 18:25:30 +08:00
										 |  |  | 				withYield = true; | 
					
						
							| 
									
										
										
										
											2022-02-21 16:58:44 +08:00
										 |  |  | 				newResolveContext.yield = obj => yieldResult.push(obj); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 			const propagate = key => { | 
					
						
							|  |  |  | 				if (resolveContext[key]) { | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:55 +08:00
										 |  |  | 					addAllToSet(resolveContext[key], newResolveContext[key]); | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			const resolveTime = Date.now(); | 
					
						
							|  |  |  | 			resolver.doResolve( | 
					
						
							|  |  |  | 				resolver.hooks.resolve, | 
					
						
							|  |  |  | 				newRequest, | 
					
						
							|  |  |  | 				"Cache miss", | 
					
						
							|  |  |  | 				newResolveContext, | 
					
						
							|  |  |  | 				(err, result) => { | 
					
						
							|  |  |  | 					propagate("fileDependencies"); | 
					
						
							|  |  |  | 					propagate("contextDependencies"); | 
					
						
							| 
									
										
										
										
											2019-07-05 06:41:30 +08:00
										 |  |  | 					propagate("missingDependencies"); | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 					if (err) return callback(err); | 
					
						
							| 
									
										
										
										
											2019-01-05 21:58:06 +08:00
										 |  |  | 					const fileDependencies = newResolveContext.fileDependencies; | 
					
						
							|  |  |  | 					const contextDependencies = newResolveContext.contextDependencies; | 
					
						
							| 
									
										
										
										
											2019-07-05 06:41:30 +08:00
										 |  |  | 					const missingDependencies = newResolveContext.missingDependencies; | 
					
						
							| 
									
										
										
										
											2019-01-05 21:58:06 +08:00
										 |  |  | 					fileSystemInfo.createSnapshot( | 
					
						
							|  |  |  | 						resolveTime, | 
					
						
							|  |  |  | 						fileDependencies, | 
					
						
							|  |  |  | 						contextDependencies, | 
					
						
							|  |  |  | 						missingDependencies, | 
					
						
							| 
									
										
										
										
											2020-08-26 06:36:16 +08:00
										 |  |  | 						snapshotOptions, | 
					
						
							| 
									
										
										
										
											2019-01-05 21:58:06 +08:00
										 |  |  | 						(err, snapshot) => { | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 							if (err) return callback(err); | 
					
						
							| 
									
										
										
										
											2022-02-21 18:25:30 +08:00
										 |  |  | 							const resolveResult = withYield ? yieldResult : result; | 
					
						
							|  |  |  | 							// since we intercept resolve hook
 | 
					
						
							|  |  |  | 							// we still can get result in callback
 | 
					
						
							|  |  |  | 							if (withYield && result) yieldResult.push(result); | 
					
						
							| 
									
										
										
										
											2020-07-07 23:38:03 +08:00
										 |  |  | 							if (!snapshot) { | 
					
						
							| 
									
										
										
										
											2022-02-21 16:58:44 +08:00
										 |  |  | 								if (resolveResult) return callback(null, resolveResult); | 
					
						
							| 
									
										
										
										
											2020-07-07 23:38:03 +08:00
										 |  |  | 								return callback(); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2022-02-21 16:58:44 +08:00
										 |  |  | 							itemCache.store( | 
					
						
							|  |  |  | 								new CacheEntry(resolveResult, snapshot), | 
					
						
							|  |  |  | 								storeErr => { | 
					
						
							|  |  |  | 									if (storeErr) return callback(storeErr); | 
					
						
							|  |  |  | 									if (resolveResult) return callback(null, resolveResult); | 
					
						
							|  |  |  | 									callback(); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		compiler.resolverFactory.hooks.resolver.intercept({ | 
					
						
							|  |  |  | 			factory(type, hook) { | 
					
						
							| 
									
										
										
										
											2019-11-08 00:29:23 +08:00
										 |  |  | 				/** @type {Map<string, (function(Error=, Object=): void)[]>} */ | 
					
						
							|  |  |  | 				const activeRequests = new Map(); | 
					
						
							| 
									
										
										
										
											2022-02-21 16:58:44 +08:00
										 |  |  | 				/** @type {Map<string, [function(Error=, Object=): void, function(Error=, Object=): void][]>} */ | 
					
						
							|  |  |  | 				const activeRequestsWithYield = new Map(); | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 				hook.tap( | 
					
						
							|  |  |  | 					"ResolverCachePlugin", | 
					
						
							|  |  |  | 					/** | 
					
						
							|  |  |  | 					 * @param {Resolver} resolver the resolver | 
					
						
							|  |  |  | 					 * @param {Object} options resolve options | 
					
						
							| 
									
										
										
										
											2019-11-08 17:13:46 +08:00
										 |  |  | 					 * @param {Object} userOptions resolve options passed by the user | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 					 * @returns {void} | 
					
						
							|  |  |  | 					 */ | 
					
						
							| 
									
										
										
										
											2019-11-08 17:13:46 +08:00
										 |  |  | 					(resolver, options, userOptions) => { | 
					
						
							| 
									
										
										
										
											2018-10-31 20:53:38 +08:00
										 |  |  | 						if (options.cache !== true) return; | 
					
						
							| 
									
										
										
										
											2019-11-08 17:13:46 +08:00
										 |  |  | 						const optionsIdent = objectToString(userOptions, false); | 
					
						
							| 
									
										
										
										
											2019-11-07 23:33:29 +08:00
										 |  |  | 						const cacheWithContext = | 
					
						
							|  |  |  | 							options.cacheWithContext !== undefined | 
					
						
							|  |  |  | 								? options.cacheWithContext | 
					
						
							|  |  |  | 								: false; | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 						resolver.hooks.resolve.tapAsync( | 
					
						
							|  |  |  | 							{ | 
					
						
							|  |  |  | 								name: "ResolverCachePlugin", | 
					
						
							|  |  |  | 								stage: -100 | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							(request, resolveContext, callback) => { | 
					
						
							|  |  |  | 								if (request._ResolverCachePluginCacheMiss || !fileSystemInfo) { | 
					
						
							|  |  |  | 									return callback(); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2022-02-21 16:58:44 +08:00
										 |  |  | 								const withYield = typeof resolveContext.yield === "function"; | 
					
						
							|  |  |  | 								const identifier = `${type}${ | 
					
						
							|  |  |  | 									withYield ? "|yield" : "|default" | 
					
						
							|  |  |  | 								}${optionsIdent}${objectToString(request, !cacheWithContext)}`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								if (withYield) { | 
					
						
							|  |  |  | 									const activeRequest = activeRequestsWithYield.get(identifier); | 
					
						
							|  |  |  | 									if (activeRequest) { | 
					
						
							|  |  |  | 										activeRequest[0].push(callback); | 
					
						
							|  |  |  | 										activeRequest[1].push(resolveContext.yield); | 
					
						
							|  |  |  | 										return; | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									const activeRequest = activeRequests.get(identifier); | 
					
						
							|  |  |  | 									if (activeRequest) { | 
					
						
							|  |  |  | 										activeRequest.push(callback); | 
					
						
							|  |  |  | 										return; | 
					
						
							|  |  |  | 									} | 
					
						
							| 
									
										
										
										
											2019-11-08 00:29:23 +08:00
										 |  |  | 								} | 
					
						
							| 
									
										
										
										
											2020-07-15 17:14:28 +08:00
										 |  |  | 								const itemCache = cache.getItemCache(identifier, null); | 
					
						
							| 
									
										
										
										
											2022-02-21 16:58:44 +08:00
										 |  |  | 								let callbacks, yields; | 
					
						
							|  |  |  | 								const done = withYield | 
					
						
							|  |  |  | 									? (err, result) => { | 
					
						
							|  |  |  | 											if (callbacks === undefined) { | 
					
						
							|  |  |  | 												if (err) { | 
					
						
							|  |  |  | 													callback(err); | 
					
						
							|  |  |  | 												} else { | 
					
						
							|  |  |  | 													if (result) | 
					
						
							|  |  |  | 														for (const r of result) resolveContext.yield(r); | 
					
						
							|  |  |  | 													callback(null, null); | 
					
						
							|  |  |  | 												} | 
					
						
							|  |  |  | 												yields = undefined; | 
					
						
							|  |  |  | 												callbacks = false; | 
					
						
							|  |  |  | 											} else { | 
					
						
							| 
									
										
										
										
											2022-02-21 18:25:30 +08:00
										 |  |  | 												if (err) { | 
					
						
							|  |  |  | 													for (const cb of callbacks) cb(err); | 
					
						
							|  |  |  | 												} else { | 
					
						
							|  |  |  | 													for (let i = 0; i < callbacks.length; i++) { | 
					
						
							|  |  |  | 														const cb = callbacks[i]; | 
					
						
							|  |  |  | 														const yield_ = yields[i]; | 
					
						
							|  |  |  | 														if (result) for (const r of result) yield_(r); | 
					
						
							|  |  |  | 														cb(null, null); | 
					
						
							|  |  |  | 													} | 
					
						
							| 
									
										
										
										
											2022-02-21 16:58:44 +08:00
										 |  |  | 												} | 
					
						
							|  |  |  | 												activeRequestsWithYield.delete(identifier); | 
					
						
							|  |  |  | 												yields = undefined; | 
					
						
							|  |  |  | 												callbacks = false; | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 									  } | 
					
						
							|  |  |  | 									: (err, result) => { | 
					
						
							|  |  |  | 											if (callbacks === undefined) { | 
					
						
							|  |  |  | 												callback(err, result); | 
					
						
							|  |  |  | 												callbacks = false; | 
					
						
							|  |  |  | 											} else { | 
					
						
							|  |  |  | 												for (const callback of callbacks) { | 
					
						
							|  |  |  | 													callback(err, result); | 
					
						
							|  |  |  | 												} | 
					
						
							|  |  |  | 												activeRequests.delete(identifier); | 
					
						
							|  |  |  | 												callbacks = false; | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 									  }; | 
					
						
							| 
									
										
										
										
											2019-07-18 05:35:05 +08:00
										 |  |  | 								/** | 
					
						
							|  |  |  | 								 * @param {Error=} err error if any | 
					
						
							|  |  |  | 								 * @param {CacheEntry=} cacheEntry cache entry | 
					
						
							|  |  |  | 								 * @returns {void} | 
					
						
							|  |  |  | 								 */ | 
					
						
							| 
									
										
										
										
											2019-01-05 21:58:06 +08:00
										 |  |  | 								const processCacheResult = (err, cacheEntry) => { | 
					
						
							| 
									
										
										
										
											2019-11-08 00:29:23 +08:00
										 |  |  | 									if (err) return done(err); | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 									if (cacheEntry) { | 
					
						
							| 
									
										
										
										
											2020-08-23 03:54:34 +08:00
										 |  |  | 										const { snapshot, result } = cacheEntry; | 
					
						
							| 
									
										
										
										
											2019-01-05 21:58:06 +08:00
										 |  |  | 										fileSystemInfo.checkSnapshotValid( | 
					
						
							| 
									
										
										
										
											2020-08-23 03:54:34 +08:00
										 |  |  | 											snapshot, | 
					
						
							| 
									
										
										
										
											2019-01-05 21:58:06 +08:00
										 |  |  | 											(err, valid) => { | 
					
						
							|  |  |  | 												if (err || !valid) { | 
					
						
							| 
									
										
										
										
											2019-07-26 16:42:57 +08:00
										 |  |  | 													cacheInvalidResolves++; | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 													return doRealResolve( | 
					
						
							| 
									
										
										
										
											2020-07-15 17:14:28 +08:00
										 |  |  | 														itemCache, | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 														resolver, | 
					
						
							|  |  |  | 														resolveContext, | 
					
						
							|  |  |  | 														request, | 
					
						
							| 
									
										
										
										
											2019-11-08 00:29:23 +08:00
										 |  |  | 														done | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 													); | 
					
						
							|  |  |  | 												} | 
					
						
							| 
									
										
										
										
											2019-07-26 16:42:57 +08:00
										 |  |  | 												cachedResolves++; | 
					
						
							| 
									
										
										
										
											2019-07-05 06:41:30 +08:00
										 |  |  | 												if (resolveContext.missingDependencies) { | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 													addAllToSet( | 
					
						
							|  |  |  | 														resolveContext.missingDependencies, | 
					
						
							| 
									
										
										
										
											2020-08-23 03:54:34 +08:00
										 |  |  | 														snapshot.getMissingIterable() | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 													); | 
					
						
							| 
									
										
										
										
											2019-01-05 21:58:06 +08:00
										 |  |  | 												} | 
					
						
							|  |  |  | 												if (resolveContext.fileDependencies) { | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 													addAllToSet( | 
					
						
							|  |  |  | 														resolveContext.fileDependencies, | 
					
						
							| 
									
										
										
										
											2020-08-23 03:54:34 +08:00
										 |  |  | 														snapshot.getFileIterable() | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 													); | 
					
						
							| 
									
										
										
										
											2019-01-05 21:58:06 +08:00
										 |  |  | 												} | 
					
						
							|  |  |  | 												if (resolveContext.contextDependencies) { | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 													addAllToSet( | 
					
						
							|  |  |  | 														resolveContext.contextDependencies, | 
					
						
							| 
									
										
										
										
											2020-08-23 03:54:34 +08:00
										 |  |  | 														snapshot.getContextIterable() | 
					
						
							| 
									
										
										
										
											2019-08-07 15:54:43 +08:00
										 |  |  | 													); | 
					
						
							| 
									
										
										
										
											2019-01-05 21:58:06 +08:00
										 |  |  | 												} | 
					
						
							| 
									
										
										
										
											2020-08-23 03:54:34 +08:00
										 |  |  | 												done(null, result); | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 											} | 
					
						
							|  |  |  | 										); | 
					
						
							|  |  |  | 									} else { | 
					
						
							|  |  |  | 										doRealResolve( | 
					
						
							| 
									
										
										
										
											2020-07-15 17:14:28 +08:00
										 |  |  | 											itemCache, | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 											resolver, | 
					
						
							|  |  |  | 											resolveContext, | 
					
						
							|  |  |  | 											request, | 
					
						
							| 
									
										
										
										
											2019-11-08 00:29:23 +08:00
										 |  |  | 											done | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 										); | 
					
						
							|  |  |  | 									} | 
					
						
							| 
									
										
										
										
											2019-01-05 21:58:06 +08:00
										 |  |  | 								}; | 
					
						
							| 
									
										
										
										
											2020-07-15 17:14:28 +08:00
										 |  |  | 								itemCache.get(processCacheResult); | 
					
						
							| 
									
										
										
										
											2022-02-21 16:58:44 +08:00
										 |  |  | 								if (withYield && callbacks === undefined) { | 
					
						
							|  |  |  | 									callbacks = [callback]; | 
					
						
							|  |  |  | 									yields = [resolveContext.yield]; | 
					
						
							|  |  |  | 									activeRequestsWithYield.set( | 
					
						
							|  |  |  | 										identifier, | 
					
						
							|  |  |  | 										/** @type {[any, any]} */ ([callbacks, yields]) | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 								} else if (callbacks === undefined) { | 
					
						
							| 
									
										
										
										
											2019-11-08 00:29:23 +08:00
										 |  |  | 									callbacks = [callback]; | 
					
						
							|  |  |  | 									activeRequests.set(identifier, callbacks); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2018-10-23 13:22:44 +08:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				return hook; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ResolverCachePlugin; |