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