ResolverCachePlugin.js 13 KB

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