| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 12:27:09 +08:00
										 |  |  | const asyncLib = require("neo-async"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | class CachePlugin { | 
					
						
							|  |  |  | 	constructor(cache) { | 
					
						
							|  |  |  | 		this.cache = cache || {}; | 
					
						
							| 
									
										
										
										
											2018-02-17 19:09:35 +08:00
										 |  |  | 		this.FS_ACCURACY = 2000; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (Array.isArray(compiler.compilers)) { | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | 			compiler.compilers.forEach((c, idx) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				new CachePlugin((this.cache[idx] = this.cache[idx] || {})).apply(c); | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-09-13 22:29:37 +08:00
										 |  |  | 			const registerCacheToCompiler = (compiler, cache) => { | 
					
						
							| 
									
										
										
										
											2018-02-17 19:09:35 +08:00
										 |  |  | 				compiler.hooks.thisCompilation.tap("CachePlugin", compilation => { | 
					
						
							| 
									
										
										
										
											2017-09-14 15:21:28 +08:00
										 |  |  | 					compilation.cache = cache; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					compilation.hooks.childCompiler.tap( | 
					
						
							|  |  |  | 						"CachePlugin", | 
					
						
							|  |  |  | 						(childCompiler, compilerName, compilerIndex) => { | 
					
						
							|  |  |  | 							if (cache) { | 
					
						
							|  |  |  | 								let childCache; | 
					
						
							|  |  |  | 								if (!cache.children) cache.children = {}; | 
					
						
							|  |  |  | 								if (!cache.children[compilerName]) | 
					
						
							|  |  |  | 									cache.children[compilerName] = []; | 
					
						
							|  |  |  | 								if (cache.children[compilerName][compilerIndex]) | 
					
						
							|  |  |  | 									childCache = cache.children[compilerName][compilerIndex]; | 
					
						
							|  |  |  | 								else cache.children[compilerName].push((childCache = {})); | 
					
						
							|  |  |  | 								registerCacheToCompiler(childCompiler, childCache); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2017-09-14 15:21:28 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					); | 
					
						
							| 
									
										
										
										
											2017-09-13 22:29:37 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			registerCacheToCompiler(compiler, this.cache); | 
					
						
							| 
									
										
										
										
											2017-12-06 22:01:25 +08:00
										 |  |  | 			compiler.hooks.watchRun.tap("CachePlugin", () => { | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | 				this.watching = true; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-12-06 22:01:25 +08:00
										 |  |  | 			compiler.hooks.run.tapAsync("CachePlugin", (compiler, callback) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (!compiler._lastCompilationFileDependencies) return callback(); | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | 				const fs = compiler.inputFileSystem; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				const fileTs = (compiler.fileTimestamps = new Map()); | 
					
						
							|  |  |  | 				asyncLib.forEach( | 
					
						
							|  |  |  | 					compiler._lastCompilationFileDependencies, | 
					
						
							|  |  |  | 					(file, callback) => { | 
					
						
							|  |  |  | 						fs.stat(file, (err, stat) => { | 
					
						
							|  |  |  | 							if (err) { | 
					
						
							|  |  |  | 								if (err.code === "ENOENT") return callback(); | 
					
						
							|  |  |  | 								return callback(err); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2016-02-10 03:49:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							if (stat.mtime) this.applyMtime(+stat.mtime); | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							fileTs.set(file, +stat.mtime || Infinity); | 
					
						
							| 
									
										
										
										
											2018-01-23 15:30:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							callback(); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					err => { | 
					
						
							|  |  |  | 						if (err) return callback(err); | 
					
						
							| 
									
										
										
										
											2018-01-24 13:55:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						for (const [file, ts] of fileTs) { | 
					
						
							|  |  |  | 							fileTs.set(file, ts + this.FS_ACCURACY); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-01-24 13:55:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						callback(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2018-02-17 19:09:35 +08:00
										 |  |  | 			compiler.hooks.afterCompile.tap("CachePlugin", compilation => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				compilation.compiler._lastCompilationFileDependencies = | 
					
						
							|  |  |  | 					compilation.fileDependencies; | 
					
						
							|  |  |  | 				compilation.compiler._lastCompilationContextDependencies = | 
					
						
							|  |  |  | 					compilation.contextDependencies; | 
					
						
							| 
									
										
										
										
											2016-02-10 03:49:36 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-10 03:49:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | 	/* istanbul ignore next */ | 
					
						
							|  |  |  | 	applyMtime(mtime) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.FS_ACCURACY > 1 && mtime % 2 !== 0) this.FS_ACCURACY = 1; | 
					
						
							|  |  |  | 		else if (this.FS_ACCURACY > 10 && mtime % 20 !== 0) this.FS_ACCURACY = 10; | 
					
						
							|  |  |  | 		else if (this.FS_ACCURACY > 100 && mtime % 200 !== 0) | 
					
						
							| 
									
										
										
										
											2018-02-17 19:09:35 +08:00
										 |  |  | 			this.FS_ACCURACY = 100; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		else if (this.FS_ACCURACY > 1000 && mtime % 2000 !== 0) | 
					
						
							| 
									
										
										
										
											2018-02-17 19:09:35 +08:00
										 |  |  | 			this.FS_ACCURACY = 1000; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = CachePlugin; |