| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | var async = require("async"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function CachePlugin(cache) { | 
					
						
							|  |  |  | 	this.cache = cache || {}; | 
					
						
							| 
									
										
										
										
											2016-02-10 03:49:36 +08:00
										 |  |  | 	this.FS_ACCURENCY = 10000; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = CachePlugin; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CachePlugin.prototype.apply = function(compiler) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 	if(Array.isArray(compiler.compilers)) { | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 		compiler.compilers.forEach(function(c, idx) { | 
					
						
							|  |  |  | 			c.apply(new CachePlugin(this.cache[idx] = this.cache[idx] || {})); | 
					
						
							|  |  |  | 		}, this); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2016-02-10 03:49:36 +08:00
										 |  |  | 		var _this = this; | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 		compiler.plugin("compilation", function(compilation) { | 
					
						
							| 
									
										
										
										
											2016-02-10 06:32:04 +08:00
										 |  |  | 			if(!compilation.notCacheable) { | 
					
						
							| 
									
										
										
										
											2016-02-10 05:34:10 +08:00
										 |  |  | 				compilation.cache = _this.cache; | 
					
						
							| 
									
										
										
										
											2016-02-10 06:32:04 +08:00
										 |  |  | 			} else if(_this.watching) { | 
					
						
							|  |  |  | 				compilation.warnings.push( | 
					
						
							|  |  |  | 					new Error("CachePlugin - Cache cannot be used because of: " + compilation.notCacheable) | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2016-02-10 05:34:10 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-02-10 03:49:36 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-02-10 21:52:00 +08:00
										 |  |  | 		compiler.plugin("watch-run", function(compiler, callback) { | 
					
						
							| 
									
										
										
										
											2016-02-10 06:32:04 +08:00
										 |  |  | 			_this.watching = true; | 
					
						
							| 
									
										
										
										
											2016-02-10 21:52:00 +08:00
										 |  |  | 			callback(); | 
					
						
							| 
									
										
										
										
											2016-02-10 06:32:04 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 		compiler.plugin("run", function(compiler, callback) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 			if(!compiler._lastCompilationFileDependencies) return callback(); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 			var fs = compiler.inputFileSystem; | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 			var fileTs = compiler.fileTimestamps = {}; | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 			async.forEach(compiler._lastCompilationFileDependencies, function(file, callback) { | 
					
						
							|  |  |  | 				fs.stat(file, function(err, stat) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 					if(err) { | 
					
						
							|  |  |  | 						if(err.code === "ENOENT") return callback(); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 						return callback(err); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 03:49:36 +08:00
										 |  |  | 					if(stat.mtime) | 
					
						
							|  |  |  | 						_this.applyMtime(+stat.mtime); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 					fileTs[file] = stat.mtime || Infinity; | 
					
						
							|  |  |  | 					callback(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, callback); | 
					
						
							| 
									
										
										
										
											2016-02-10 03:49:36 +08:00
										 |  |  | 			Object.keys(fileTs).forEach(function(key) { | 
					
						
							|  |  |  | 				fileTs[key] += _this.FS_ACCURENCY; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 		compiler.plugin("after-compile", function(compilation, callback) { | 
					
						
							|  |  |  | 			compilation.compiler._lastCompilationFileDependencies = compilation.fileDependencies; | 
					
						
							|  |  |  | 			compilation.compiler._lastCompilationContextDependencies = compilation.contextDependencies; | 
					
						
							|  |  |  | 			callback(); | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-08 20:15:21 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-02-10 03:49:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | CachePlugin.prototype.applyMtime = function applyMtime(mtime) { | 
					
						
							|  |  |  | 	if(this.FS_ACCURENCY > 1 && mtime % 1 !== 0) | 
					
						
							|  |  |  | 		this.FS_ACCURENCY = 1; | 
					
						
							|  |  |  | 	else if(this.FS_ACCURENCY > 10 && mtime % 10 !== 0) | 
					
						
							|  |  |  | 		this.FS_ACCURENCY = 10; | 
					
						
							|  |  |  | 	else if(this.FS_ACCURENCY > 100 && mtime % 100 !== 0) | 
					
						
							|  |  |  | 		this.FS_ACCURENCY = 100; | 
					
						
							|  |  |  | 	else if(this.FS_ACCURENCY > 1000 && mtime % 1000 !== 0) | 
					
						
							|  |  |  | 		this.FS_ACCURENCY = 1000; | 
					
						
							|  |  |  | 	else if(this.FS_ACCURENCY > 2000 && mtime % 2000 !== 0) | 
					
						
							|  |  |  | 		this.FS_ACCURENCY = 2000; | 
					
						
							|  |  |  | }; |