Cache.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { AsyncParallelHook, AsyncSeriesBailHook, SyncHook } = require("tapable");
  7. const {
  8. makeWebpackError,
  9. makeWebpackErrorCallback
  10. } = require("./HookWebpackError");
  11. /** @typedef {import("./WebpackError")} WebpackError */
  12. /**
  13. * Cache validation token whose string representation identifies the build
  14. * inputs associated with a cached value.
  15. * @typedef {object} Etag
  16. * @property {() => string} toString
  17. */
  18. /**
  19. * Completion callback used by cache operations that either fail with a
  20. * `WebpackError` or resolve with a typed result.
  21. * @template T
  22. * @callback CallbackCache
  23. * @param {WebpackError | null} err
  24. * @param {T=} result
  25. * @returns {void}
  26. */
  27. /** @typedef {EXPECTED_ANY} Data */
  28. /**
  29. * Handler invoked after a cache read succeeds so additional cache layers can
  30. * react to the retrieved value.
  31. * @template T
  32. * @callback GotHandler
  33. * @param {T} result
  34. * @param {() => void} callback
  35. * @returns {void}
  36. */
  37. /**
  38. * Creates a callback wrapper that waits for a fixed number of completions and
  39. * forwards the first error immediately.
  40. * @param {number} times times
  41. * @param {(err?: Error | null) => void} callback callback
  42. * @returns {(err?: Error | null) => void} callback
  43. */
  44. const needCalls = (times, callback) => (err) => {
  45. if (--times === 0) {
  46. return callback(err);
  47. }
  48. if (err && times > 0) {
  49. times = 0;
  50. return callback(err);
  51. }
  52. };
  53. /**
  54. * Abstract cache interface backed by tapable hooks for reading, writing, idle
  55. * transitions, and shutdown across webpack cache implementations.
  56. */
  57. class Cache {
  58. /**
  59. * Initializes the cache lifecycle hooks implemented by cache backends.
  60. */
  61. constructor() {
  62. this.hooks = {
  63. /** @type {AsyncSeriesBailHook<[string, Etag | null, GotHandler<EXPECTED_ANY>[]], Data>} */
  64. get: new AsyncSeriesBailHook(["identifier", "etag", "gotHandlers"]),
  65. /** @type {AsyncParallelHook<[string, Etag | null, Data]>} */
  66. store: new AsyncParallelHook(["identifier", "etag", "data"]),
  67. /** @type {AsyncParallelHook<[Iterable<string>]>} */
  68. storeBuildDependencies: new AsyncParallelHook(["dependencies"]),
  69. /** @type {SyncHook<[]>} */
  70. beginIdle: new SyncHook([]),
  71. /** @type {AsyncParallelHook<[]>} */
  72. endIdle: new AsyncParallelHook([]),
  73. /** @type {AsyncParallelHook<[]>} */
  74. shutdown: new AsyncParallelHook([])
  75. };
  76. }
  77. /**
  78. * Retrieves a cached value and lets registered `gotHandlers` observe the
  79. * result before the caller receives it.
  80. * @template T
  81. * @param {string} identifier the cache identifier
  82. * @param {Etag | null} etag the etag
  83. * @param {CallbackCache<T>} callback signals when the value is retrieved
  84. * @returns {void}
  85. */
  86. get(identifier, etag, callback) {
  87. /** @type {GotHandler<T>[]} */
  88. const gotHandlers = [];
  89. this.hooks.get.callAsync(identifier, etag, gotHandlers, (err, result) => {
  90. if (err) {
  91. callback(makeWebpackError(err, "Cache.hooks.get"));
  92. return;
  93. }
  94. if (result === null) {
  95. result = undefined;
  96. }
  97. if (gotHandlers.length > 1) {
  98. const innerCallback = needCalls(gotHandlers.length, () =>
  99. callback(null, result)
  100. );
  101. for (const gotHandler of gotHandlers) {
  102. gotHandler(result, innerCallback);
  103. }
  104. } else if (gotHandlers.length === 1) {
  105. gotHandlers[0](result, () => callback(null, result));
  106. } else {
  107. callback(null, result);
  108. }
  109. });
  110. }
  111. /**
  112. * Stores a cache entry for the identifier and etag through the registered
  113. * cache backend hooks.
  114. * @template T
  115. * @param {string} identifier the cache identifier
  116. * @param {Etag | null} etag the etag
  117. * @param {T} data the value to store
  118. * @param {CallbackCache<void>} callback signals when the value is stored
  119. * @returns {void}
  120. */
  121. store(identifier, etag, data, callback) {
  122. this.hooks.store.callAsync(
  123. identifier,
  124. etag,
  125. data,
  126. makeWebpackErrorCallback(callback, "Cache.hooks.store")
  127. );
  128. }
  129. /**
  130. * Persists the set of build dependencies required to determine whether the
  131. * cache can be restored in a future compilation.
  132. * @param {Iterable<string>} dependencies list of all build dependencies
  133. * @param {CallbackCache<void>} callback signals when the dependencies are stored
  134. * @returns {void}
  135. */
  136. storeBuildDependencies(dependencies, callback) {
  137. this.hooks.storeBuildDependencies.callAsync(
  138. dependencies,
  139. makeWebpackErrorCallback(callback, "Cache.hooks.storeBuildDependencies")
  140. );
  141. }
  142. /**
  143. * Signals that webpack is entering an idle phase and cache backends may flush
  144. * or compact pending work.
  145. * @returns {void}
  146. */
  147. beginIdle() {
  148. this.hooks.beginIdle.call();
  149. }
  150. /**
  151. * Signals that webpack is leaving the idle phase and waits for cache
  152. * backends to finish any asynchronous resume work.
  153. * @param {CallbackCache<void>} callback signals when the call finishes
  154. * @returns {void}
  155. */
  156. endIdle(callback) {
  157. this.hooks.endIdle.callAsync(
  158. makeWebpackErrorCallback(callback, "Cache.hooks.endIdle")
  159. );
  160. }
  161. /**
  162. * Shuts down every registered cache backend and waits for cleanup to finish.
  163. * @param {CallbackCache<void>} callback signals when the call finishes
  164. * @returns {void}
  165. */
  166. shutdown(callback) {
  167. this.hooks.shutdown.callAsync(
  168. makeWebpackErrorCallback(callback, "Cache.hooks.shutdown")
  169. );
  170. }
  171. }
  172. Cache.STAGE_MEMORY = -10;
  173. Cache.STAGE_DEFAULT = 0;
  174. Cache.STAGE_DISK = 10;
  175. Cache.STAGE_NETWORK = 20;
  176. module.exports = Cache;