| [9af201e] | 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 Cache = require("../Cache");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("../Cache").Data} Data */
|
|---|
| 11 | /** @typedef {import("../Cache").Etag} Etag */
|
|---|
| 12 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 13 |
|
|---|
| 14 | class MemoryCachePlugin {
|
|---|
| 15 | /**
|
|---|
| 16 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 17 | * @param {Compiler} compiler the compiler instance
|
|---|
| 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;
|
|---|