| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 17:03:10 +08:00
										 |  |  | const Cache = require("../Cache"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2019-11-12 20:55:13 +08:00
										 |  |  | /** @typedef {import("../Cache").Etag} Etag */ | 
					
						
							| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | /** @typedef {import("../Module")} Module */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MemoryCachePlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2019-11-12 20:55:13 +08:00
										 |  |  | 		/** @type {Map<string, { etag: Etag | null, data: any }>} */ | 
					
						
							| 
									
										
										
										
											2018-10-11 16:46:48 +08:00
										 |  |  | 		const cache = new Map(); | 
					
						
							|  |  |  | 		compiler.cache.hooks.store.tap( | 
					
						
							| 
									
										
										
										
											2019-05-13 17:03:10 +08:00
										 |  |  | 			{ name: "MemoryCachePlugin", stage: Cache.STAGE_MEMORY }, | 
					
						
							| 
									
										
										
										
											2018-10-11 16:46:48 +08:00
										 |  |  | 			(identifier, etag, data) => { | 
					
						
							|  |  |  | 				cache.set(identifier, { etag, data }); | 
					
						
							| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2019-01-19 19:31:24 +08:00
										 |  |  | 		compiler.cache.hooks.get.tap( | 
					
						
							| 
									
										
										
										
											2019-05-13 17:03:10 +08:00
										 |  |  | 			{ name: "MemoryCachePlugin", stage: Cache.STAGE_MEMORY }, | 
					
						
							| 
									
										
										
										
											2019-01-19 19:31:24 +08:00
										 |  |  | 			(identifier, etag, gotHandlers) => { | 
					
						
							|  |  |  | 				const cacheEntry = cache.get(identifier); | 
					
						
							| 
									
										
										
										
											2019-11-12 20:55:13 +08:00
										 |  |  | 				if (cacheEntry === null) { | 
					
						
							|  |  |  | 					return null; | 
					
						
							|  |  |  | 				} else if (cacheEntry !== undefined) { | 
					
						
							|  |  |  | 					return cacheEntry.etag === etag ? cacheEntry.data : null; | 
					
						
							| 
									
										
										
										
											2018-12-28 03:57:54 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-01-19 19:31:24 +08:00
										 |  |  | 				gotHandlers.push((result, callback) => { | 
					
						
							| 
									
										
										
										
											2019-11-12 20:55:13 +08:00
										 |  |  | 					if (result === undefined) { | 
					
						
							|  |  |  | 						cache.set(identifier, null); | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2019-01-19 19:31:24 +08:00
										 |  |  | 						cache.set(identifier, { etag, data: result }); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					return callback(); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2018-12-28 03:57:54 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = MemoryCachePlugin; |