ResolverCachePlugin.js 13 KB

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