ResolverCachePlugin.js 14 KB

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