* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 18 | * @returns {void} |
| 19 | */ |
| 20 | apply(compiler) { |
| 21 | /** @type {Map<string, { etag: Etag | null, data: Data } | null>} */ |
| 22 | const cache = new Map(); |
| 23 | compiler.cache.hooks.store.tap( |
| 24 | { name: "MemoryCachePlugin", stage: Cache.STAGE_MEMORY }, |
| 25 | (identifier, etag, data) => { |
| 26 | cache.set(identifier, { etag, data }); |
| 27 | } |
| 28 | ); |
| 29 | compiler.cache.hooks.get.tap( |
| 30 | { name: "MemoryCachePlugin", stage: Cache.STAGE_MEMORY }, |
| 31 | (identifier, etag, gotHandlers) => { |
| 32 | const cacheEntry = cache.get(identifier); |
| 33 | if (cacheEntry === null) { |
| 34 | return null; |
| 35 | } else if (cacheEntry !== undefined) { |
| 36 | return cacheEntry.etag === etag ? cacheEntry.data : null; |
| 37 | } |
| 38 | gotHandlers.push((result, callback) => { |
| 39 | if (result === undefined) { |
| 40 | cache.set(identifier, null); |
| 41 | } else { |
| 42 | cache.set(identifier, { etag, data: result }); |
| 43 | } |
| 44 | return callback(); |
| 45 | }); |
| 46 | } |
| 47 | ); |
| 48 | compiler.cache.hooks.shutdown.tap( |
| 49 | { name: "MemoryCachePlugin", stage: Cache.STAGE_MEMORY }, |
| 50 | () => { |
| 51 | cache.clear(); |
| 52 | } |
| 53 | ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | module.exports = MemoryCachePlugin; |