ResolverCachePlugin.js 14 KB

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