Apply module bundling
[platform/framework/web/wrtjs.git] / node_modules / webpack / lib / cache / MemoryWithGcCachePlugin.js
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("webpack-sources").Source} Source */
11 /** @typedef {import("../Cache").Etag} Etag */
12 /** @typedef {import("../Compiler")} Compiler */
13 /** @typedef {import("../Module")} Module */
14
15 class MemoryWithGcCachePlugin {
16         constructor({ maxGenerations }) {
17                 this._maxGenerations = maxGenerations;
18         }
19         /**
20          * Apply the plugin
21          * @param {Compiler} compiler the compiler instance
22          * @returns {void}
23          */
24         apply(compiler) {
25                 const maxGenerations = this._maxGenerations;
26                 /** @type {Map<string, { etag: Etag | null, data: any }>} */
27                 const cache = new Map();
28                 /** @type {Map<string, { entry: { etag: Etag | null, data: any }, until: number }>} */
29                 const oldCache = new Map();
30                 let generation = 0;
31                 let cachePosition = 0;
32                 const logger = compiler.getInfrastructureLogger("MemoryWithGcCachePlugin");
33                 compiler.hooks.afterDone.tap("MemoryWithGcCachePlugin", () => {
34                         generation++;
35                         let clearedEntries = 0;
36                         let lastClearedIdentifier;
37                         for (const [identifier, entry] of oldCache) {
38                                 if (entry.until > generation) break;
39
40                                 oldCache.delete(identifier);
41                                 if (cache.get(identifier) === undefined) {
42                                         cache.delete(identifier);
43                                         clearedEntries++;
44                                         lastClearedIdentifier = identifier;
45                                 }
46                         }
47                         if (clearedEntries > 0 || oldCache.size > 0) {
48                                 logger.log(
49                                         `${cache.size - oldCache.size} active entries, ${
50                                                 oldCache.size
51                                         } recently unused cached entries${
52                                                 clearedEntries > 0
53                                                         ? `, ${clearedEntries} old unused cache entries removed e. g. ${lastClearedIdentifier}`
54                                                         : ""
55                                         }`
56                                 );
57                         }
58                         let i = (cache.size / maxGenerations) | 0;
59                         let j = cachePosition >= cache.size ? 0 : cachePosition;
60                         cachePosition = j + i;
61                         for (const [identifier, entry] of cache) {
62                                 if (j !== 0) {
63                                         j--;
64                                         continue;
65                                 }
66                                 if (entry !== undefined) {
67                                         // We don't delete the cache entry, but set it to undefined instead
68                                         // This reserves the location in the data table and avoids rehashing
69                                         // when constantly adding and removing entries.
70                                         // It will be deleted when removed from oldCache.
71                                         cache.set(identifier, undefined);
72                                         oldCache.delete(identifier);
73                                         oldCache.set(identifier, {
74                                                 entry,
75                                                 until: generation + maxGenerations
76                                         });
77                                         if (i-- === 0) break;
78                                 }
79                         }
80                 });
81                 compiler.cache.hooks.store.tap(
82                         { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
83                         (identifier, etag, data) => {
84                                 cache.set(identifier, { etag, data });
85                         }
86                 );
87                 compiler.cache.hooks.get.tap(
88                         { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
89                         (identifier, etag, gotHandlers) => {
90                                 const cacheEntry = cache.get(identifier);
91                                 if (cacheEntry === null) {
92                                         return null;
93                                 } else if (cacheEntry !== undefined) {
94                                         return cacheEntry.etag === etag ? cacheEntry.data : null;
95                                 }
96                                 const oldCacheEntry = oldCache.get(identifier);
97                                 if (oldCacheEntry !== undefined) {
98                                         const cacheEntry = oldCacheEntry.entry;
99                                         if (cacheEntry === null) {
100                                                 oldCache.delete(identifier);
101                                                 cache.set(identifier, cacheEntry);
102                                                 return null;
103                                         } else {
104                                                 if (cacheEntry.etag !== etag) return null;
105                                                 oldCache.delete(identifier);
106                                                 cache.set(identifier, cacheEntry);
107                                                 return cacheEntry.data;
108                                         }
109                                 }
110                                 gotHandlers.push((result, callback) => {
111                                         if (result === undefined) {
112                                                 cache.set(identifier, null);
113                                         } else {
114                                                 cache.set(identifier, { etag, data: result });
115                                         }
116                                         return callback();
117                                 });
118                         }
119                 );
120                 compiler.cache.hooks.shutdown.tap(
121                         { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
122                         () => {
123                                 cache.clear();
124                                 oldCache.clear();
125                         }
126                 );
127         }
128 }
129 module.exports = MemoryWithGcCachePlugin;