ResolverCachePlugin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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[]} */
  208. (yieldResult).push(result);
  209. }
  210. if (!snapshot) {
  211. if (resolveResult) {
  212. return callback(
  213. null,
  214. /** @type {ResolveRequest} */
  215. (resolveResult)
  216. );
  217. }
  218. return callback();
  219. }
  220. itemCache.store(
  221. new CacheEntry(
  222. /** @type {ResolveRequest} */
  223. (resolveResult),
  224. snapshot
  225. ),
  226. storeErr => {
  227. if (storeErr) return callback(storeErr);
  228. if (resolveResult) {
  229. return callback(
  230. null,
  231. /** @type {ResolveRequest} */
  232. (resolveResult)
  233. );
  234. }
  235. callback();
  236. }
  237. );
  238. }
  239. );
  240. }
  241. );
  242. };
  243. compiler.resolverFactory.hooks.resolver.intercept({
  244. factory(type, _hook) {
  245. /** @typedef {(err?: Error, resolveRequest?: ResolveRequest) => void} ActiveRequest */
  246. /** @type {Map<string, ActiveRequest[]>} */
  247. const activeRequests = new Map();
  248. /** @type {Map<string, [ActiveRequest[], Yield[]]>} */
  249. const activeRequestsWithYield = new Map();
  250. const hook =
  251. /** @type {SyncHook<[Resolver, ResolveOptions, ResolveOptionsWithDependencyType]>} */
  252. (_hook);
  253. hook.tap(PLUGIN_NAME, (resolver, options, userOptions) => {
  254. if (
  255. /** @type {ResolveOptions & { cache: boolean }} */
  256. (options).cache !== true
  257. ) {
  258. return;
  259. }
  260. const optionsIdent = objectToString(userOptions, false);
  261. const cacheWithContext =
  262. options.cacheWithContext !== undefined
  263. ? options.cacheWithContext
  264. : false;
  265. resolver.hooks.resolve.tapAsync(
  266. {
  267. name: PLUGIN_NAME,
  268. stage: -100
  269. },
  270. (request, resolveContext, callback) => {
  271. if (
  272. /** @type {ResolveRequestWithCacheMiss} */
  273. (request)._ResolverCachePluginCacheMiss ||
  274. !fileSystemInfo
  275. ) {
  276. return callback();
  277. }
  278. const withYield = typeof resolveContext.yield === "function";
  279. const identifier = `${type}${
  280. withYield ? "|yield" : "|default"
  281. }${optionsIdent}${objectToString(request, !cacheWithContext)}`;
  282. if (withYield) {
  283. const activeRequest = activeRequestsWithYield.get(identifier);
  284. if (activeRequest) {
  285. activeRequest[0].push(callback);
  286. activeRequest[1].push(
  287. /** @type {Yield} */
  288. (resolveContext.yield)
  289. );
  290. return;
  291. }
  292. } else {
  293. const activeRequest = activeRequests.get(identifier);
  294. if (activeRequest) {
  295. activeRequest.push(callback);
  296. return;
  297. }
  298. }
  299. const itemCache = cache.getItemCache(identifier, null);
  300. /** @type {Callback[] | false | undefined} */
  301. let callbacks;
  302. /** @type {Yield[] | undefined} */
  303. let yields;
  304. /**
  305. * @type {(err?: Error | null, result?: ResolveRequest | ResolveRequest[] | null) => void}
  306. */
  307. const done = withYield
  308. ? (err, result) => {
  309. if (callbacks === undefined) {
  310. if (err) {
  311. callback(err);
  312. } else {
  313. if (result) {
  314. for (const r of /** @type {ResolveRequest[]} */ (
  315. result
  316. )) {
  317. /** @type {Yield} */
  318. (resolveContext.yield)(r);
  319. }
  320. }
  321. callback(null, null);
  322. }
  323. yields = undefined;
  324. callbacks = false;
  325. } else {
  326. const definedCallbacks =
  327. /** @type {Callback[]} */
  328. (callbacks);
  329. if (err) {
  330. for (const cb of definedCallbacks) cb(err);
  331. } else {
  332. for (let i = 0; i < definedCallbacks.length; i++) {
  333. const cb = definedCallbacks[i];
  334. const yield_ = /** @type {Yield[]} */ (yields)[i];
  335. if (result) {
  336. for (const r of /** @type {ResolveRequest[]} */ (
  337. result
  338. )) {
  339. yield_(r);
  340. }
  341. }
  342. cb(null, null);
  343. }
  344. }
  345. activeRequestsWithYield.delete(identifier);
  346. yields = undefined;
  347. callbacks = false;
  348. }
  349. }
  350. : (err, result) => {
  351. if (callbacks === undefined) {
  352. callback(err, /** @type {ResolveRequest} */ (result));
  353. callbacks = false;
  354. } else {
  355. for (const callback of /** @type {Callback[]} */ (
  356. callbacks
  357. )) {
  358. callback(err, /** @type {ResolveRequest} */ (result));
  359. }
  360. activeRequests.delete(identifier);
  361. callbacks = false;
  362. }
  363. };
  364. /**
  365. * @param {(Error | null)=} err error if any
  366. * @param {(CacheEntry | null)=} cacheEntry cache entry
  367. * @returns {void}
  368. */
  369. const processCacheResult = (err, cacheEntry) => {
  370. if (err) return done(err);
  371. if (cacheEntry) {
  372. const { snapshot, result } = cacheEntry;
  373. fileSystemInfo.checkSnapshotValid(snapshot, (err, valid) => {
  374. if (err || !valid) {
  375. cacheInvalidResolves++;
  376. return doRealResolve(
  377. itemCache,
  378. resolver,
  379. resolveContext,
  380. request,
  381. done
  382. );
  383. }
  384. cachedResolves++;
  385. if (resolveContext.missingDependencies) {
  386. addAllToSet(
  387. /** @type {Set<string>} */
  388. (resolveContext.missingDependencies),
  389. snapshot.getMissingIterable()
  390. );
  391. }
  392. if (resolveContext.fileDependencies) {
  393. addAllToSet(
  394. /** @type {Set<string>} */
  395. (resolveContext.fileDependencies),
  396. snapshot.getFileIterable()
  397. );
  398. }
  399. if (resolveContext.contextDependencies) {
  400. addAllToSet(
  401. /** @type {Set<string>} */
  402. (resolveContext.contextDependencies),
  403. snapshot.getContextIterable()
  404. );
  405. }
  406. done(null, result);
  407. });
  408. } else {
  409. doRealResolve(
  410. itemCache,
  411. resolver,
  412. resolveContext,
  413. request,
  414. done
  415. );
  416. }
  417. };
  418. itemCache.get(processCacheResult);
  419. if (withYield && callbacks === undefined) {
  420. callbacks = [callback];
  421. yields = [/** @type {Yield} */ (resolveContext.yield)];
  422. activeRequestsWithYield.set(identifier, [callbacks, yields]);
  423. } else if (callbacks === undefined) {
  424. callbacks = [callback];
  425. activeRequests.set(identifier, callbacks);
  426. }
  427. }
  428. );
  429. });
  430. return hook;
  431. }
  432. });
  433. }
  434. }
  435. module.exports = ResolverCachePlugin;