| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Stats = require("./Stats"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Watching { | 
					
						
							|  |  |  | 	constructor(compiler, watchOptions, handler) { | 
					
						
							|  |  |  | 		this.startTime = null; | 
					
						
							|  |  |  | 		this.invalid = false; | 
					
						
							|  |  |  | 		this.handler = handler; | 
					
						
							|  |  |  | 		this.callbacks = []; | 
					
						
							|  |  |  | 		this.closed = false; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (typeof watchOptions === "number") { | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 			this.watchOptions = { | 
					
						
							|  |  |  | 				aggregateTimeout: watchOptions | 
					
						
							|  |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		} else if (watchOptions && typeof watchOptions === "object") { | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 			this.watchOptions = Object.assign({}, watchOptions); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			this.watchOptions = {}; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		this.watchOptions.aggregateTimeout = | 
					
						
							|  |  |  | 			this.watchOptions.aggregateTimeout || 200; | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 		this.compiler = compiler; | 
					
						
							|  |  |  | 		this.running = true; | 
					
						
							|  |  |  | 		this.compiler.readRecords(err => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			if (err) return this._done(err); | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			this._go(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_go() { | 
					
						
							|  |  |  | 		this.startTime = Date.now(); | 
					
						
							|  |  |  | 		this.running = true; | 
					
						
							|  |  |  | 		this.invalid = false; | 
					
						
							|  |  |  | 		this.compiler.hooks.watchRun.callAsync(this.compiler, err => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			if (err) return this._done(err); | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 			const onCompiled = (err, compilation) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (err) return this._done(err); | 
					
						
							|  |  |  | 				if (this.invalid) return this._done(); | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (this.compiler.hooks.shouldEmit.call(compilation) === false) { | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 					return this._done(null, compilation); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				this.compiler.emitAssets(compilation, err => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					if (err) return this._done(err); | 
					
						
							|  |  |  | 					if (this.invalid) return this._done(); | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					this.compiler.emitRecords(err => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						if (err) return this._done(err); | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						if (compilation.hooks.needAdditionalPass.call()) { | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 							compilation.needAdditionalPass = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							const stats = new Stats(compilation); | 
					
						
							|  |  |  | 							stats.startTime = this.startTime; | 
					
						
							|  |  |  | 							stats.endTime = Date.now(); | 
					
						
							| 
									
										
										
										
											2018-01-25 20:56:50 +08:00
										 |  |  | 							this.compiler.hooks.done.callAsync(stats, err => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								if (err) return this._done(err); | 
					
						
							| 
									
										
										
										
											2018-01-25 20:56:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 								this.compiler.hooks.additionalPass.callAsync(err => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									if (err) return this._done(err); | 
					
						
							| 
									
										
										
										
											2018-01-25 20:56:50 +08:00
										 |  |  | 									this.compiler.compile(onCompiled); | 
					
						
							|  |  |  | 								}); | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 							}); | 
					
						
							|  |  |  | 							return; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						return this._done(null, compilation); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			this.compiler.compile(onCompiled); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_getStats(compilation) { | 
					
						
							|  |  |  | 		const stats = new Stats(compilation); | 
					
						
							|  |  |  | 		stats.startTime = this.startTime; | 
					
						
							|  |  |  | 		stats.endTime = Date.now(); | 
					
						
							|  |  |  | 		return stats; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_done(err, compilation) { | 
					
						
							|  |  |  | 		this.running = false; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.invalid) return this._go(); | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const stats = compilation ? this._getStats(compilation) : null; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (err) { | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 			this.compiler.hooks.failed.call(err); | 
					
						
							|  |  |  | 			this.handler(err, stats); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 20:56:50 +08:00
										 |  |  | 		this.compiler.hooks.done.callAsync(stats, () => { | 
					
						
							|  |  |  | 			this.handler(null, stats); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			if (!this.closed) { | 
					
						
							|  |  |  | 				this.watch( | 
					
						
							|  |  |  | 					Array.from(compilation.fileDependencies), | 
					
						
							|  |  |  | 					Array.from(compilation.contextDependencies), | 
					
						
							|  |  |  | 					Array.from(compilation.missingDependencies) | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-01-25 20:56:50 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			for (const cb of this.callbacks) cb(); | 
					
						
							| 
									
										
										
										
											2018-01-25 20:56:50 +08:00
										 |  |  | 			this.callbacks.length = 0; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	watch(files, dirs, missing) { | 
					
						
							|  |  |  | 		this.pausedWatcher = null; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		this.watcher = this.compiler.watchFileSystem.watch( | 
					
						
							|  |  |  | 			files, | 
					
						
							|  |  |  | 			dirs, | 
					
						
							|  |  |  | 			missing, | 
					
						
							|  |  |  | 			this.startTime, | 
					
						
							|  |  |  | 			this.watchOptions, | 
					
						
							|  |  |  | 			( | 
					
						
							|  |  |  | 				err, | 
					
						
							|  |  |  | 				filesModified, | 
					
						
							|  |  |  | 				contextModified, | 
					
						
							|  |  |  | 				missingModified, | 
					
						
							|  |  |  | 				fileTimestamps, | 
					
						
							|  |  |  | 				contextTimestamps | 
					
						
							|  |  |  | 			) => { | 
					
						
							|  |  |  | 				this.pausedWatcher = this.watcher; | 
					
						
							|  |  |  | 				this.watcher = null; | 
					
						
							|  |  |  | 				if (err) return this.handler(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				this.compiler.fileTimestamps = fileTimestamps; | 
					
						
							|  |  |  | 				this.compiler.contextTimestamps = contextTimestamps; | 
					
						
							|  |  |  | 				this._invalidate(); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			(fileName, changeTime) => { | 
					
						
							|  |  |  | 				this.compiler.hooks.invalid.call(fileName, changeTime); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	invalidate(callback) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (callback) { | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 			this.callbacks.push(callback); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.watcher) { | 
					
						
							| 
									
										
										
										
											2018-01-03 19:48:46 +08:00
										 |  |  | 			this.compiler.fileTimestamps = this.watcher.getFileTimestamps(); | 
					
						
							|  |  |  | 			this.compiler.contextTimestamps = this.watcher.getContextTimestamps(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return this._invalidate(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_invalidate() { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.watcher) { | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 			this.pausedWatcher = this.watcher; | 
					
						
							|  |  |  | 			this.watcher.pause(); | 
					
						
							|  |  |  | 			this.watcher = null; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.running) { | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 			this.invalid = true; | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			this._go(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	close(callback) { | 
					
						
							| 
									
										
										
										
											2018-03-07 17:15:46 +08:00
										 |  |  | 		const finalCallback = () => { | 
					
						
							|  |  |  | 			this.compiler.hooks.watchClose.call(); | 
					
						
							|  |  |  | 			this.compiler.running = false; | 
					
						
							|  |  |  | 			if (callback !== undefined) callback(); | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		this.closed = true; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.watcher) { | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 			this.watcher.close(); | 
					
						
							|  |  |  | 			this.watcher = null; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.pausedWatcher) { | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 			this.pausedWatcher.close(); | 
					
						
							|  |  |  | 			this.pausedWatcher = null; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.running) { | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 			this.invalid = true; | 
					
						
							| 
									
										
										
										
											2018-03-07 17:15:46 +08:00
										 |  |  | 			this._done = finalCallback; | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2018-03-07 17:15:46 +08:00
										 |  |  | 			finalCallback(); | 
					
						
							| 
									
										
										
										
											2017-12-31 23:19:18 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = Watching; |