| 
									
										
										
										
											2015-10-29 06:26:52 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2017-01-04 14:20:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const createSchemaValidation = require("./util/create-schema-validation"); | 
					
						
							| 
									
										
										
										
											2017-10-28 05:23:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/WatchIgnorePlugin").WatchIgnorePluginOptions} WatchIgnorePluginOptions */ | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2021-01-12 05:15:31 +08:00
										 |  |  | /** @typedef {import("./util/fs").WatchFileSystem} WatchFileSystem */ | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const validate = createSchemaValidation( | 
					
						
							|  |  |  | 	require("../schemas/plugins/WatchIgnorePlugin.check.js"), | 
					
						
							|  |  |  | 	() => require("../schemas/plugins/WatchIgnorePlugin.json"), | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		name: "Watch Ignore Plugin", | 
					
						
							|  |  |  | 		baseDataPath: "options" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-06 06:46:34 +08:00
										 |  |  | const IGNORE_TIME_ENTRY = "ignore"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 14:20:51 +08:00
										 |  |  | class IgnoringWatchFileSystem { | 
					
						
							| 
									
										
										
										
											2021-01-12 05:15:31 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {WatchFileSystem} wfs original file system | 
					
						
							|  |  |  | 	 * @param {(string|RegExp)[]} paths ignored paths | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-04 14:20:51 +08:00
										 |  |  | 	constructor(wfs, paths) { | 
					
						
							|  |  |  | 		this.wfs = wfs; | 
					
						
							|  |  |  | 		this.paths = paths; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-05 15:18:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 14:20:51 +08:00
										 |  |  | 	watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) { | 
					
						
							| 
									
										
										
										
											2019-10-21 21:35:38 +08:00
										 |  |  | 		files = Array.from(files); | 
					
						
							|  |  |  | 		dirs = Array.from(dirs); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		const ignored = path => | 
					
						
							| 
									
										
										
										
											2019-02-05 17:06:32 +08:00
										 |  |  | 			this.paths.some(p => | 
					
						
							|  |  |  | 				p instanceof RegExp ? p.test(path) : path.indexOf(p) === 0 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2015-03-05 15:18:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 14:20:51 +08:00
										 |  |  | 		const notIgnored = path => !ignored(path); | 
					
						
							| 
									
										
										
										
											2015-03-05 15:18:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 14:20:51 +08:00
										 |  |  | 		const ignoredFiles = files.filter(ignored); | 
					
						
							|  |  |  | 		const ignoredDirs = dirs.filter(ignored); | 
					
						
							| 
									
										
										
										
											2015-08-25 16:32:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		const watcher = this.wfs.watch( | 
					
						
							|  |  |  | 			files.filter(notIgnored), | 
					
						
							|  |  |  | 			dirs.filter(notIgnored), | 
					
						
							|  |  |  | 			missing, | 
					
						
							|  |  |  | 			startTime, | 
					
						
							|  |  |  | 			options, | 
					
						
							| 
									
										
										
										
											2019-10-21 21:35:38 +08:00
										 |  |  | 			(err, fileTimestamps, dirTimestamps, changedFiles, removedFiles) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (err) return callback(err); | 
					
						
							|  |  |  | 				for (const path of ignoredFiles) { | 
					
						
							| 
									
										
										
										
											2019-11-06 06:46:34 +08:00
										 |  |  | 					fileTimestamps.set(path, IGNORE_TIME_ENTRY); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-03-05 15:18:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				for (const path of ignoredDirs) { | 
					
						
							| 
									
										
										
										
											2019-11-06 06:46:34 +08:00
										 |  |  | 					dirTimestamps.set(path, IGNORE_TIME_ENTRY); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-03-05 15:18:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-21 21:35:38 +08:00
										 |  |  | 				callback( | 
					
						
							|  |  |  | 					err, | 
					
						
							|  |  |  | 					fileTimestamps, | 
					
						
							|  |  |  | 					dirTimestamps, | 
					
						
							|  |  |  | 					changedFiles, | 
					
						
							|  |  |  | 					removedFiles | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			callbackUndelayed | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-01-03 19:48:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return { | 
					
						
							|  |  |  | 			close: () => watcher.close(), | 
					
						
							|  |  |  | 			pause: () => watcher.pause(), | 
					
						
							| 
									
										
										
										
											2020-07-08 16:21:31 +08:00
										 |  |  | 			getContextTimeInfoEntries: () => { | 
					
						
							| 
									
										
										
										
											2021-01-11 06:04:13 +08:00
										 |  |  | 				const dirTimestamps = watcher.getContextTimeInfoEntries(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				for (const path of ignoredDirs) { | 
					
						
							| 
									
										
										
										
											2019-11-06 06:46:34 +08:00
										 |  |  | 					dirTimestamps.set(path, IGNORE_TIME_ENTRY); | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-01-03 19:48:46 +08:00
										 |  |  | 				return dirTimestamps; | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2019-01-05 21:58:06 +08:00
										 |  |  | 			getFileTimeInfoEntries: () => { | 
					
						
							|  |  |  | 				const fileTimestamps = watcher.getFileTimeInfoEntries(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				for (const path of ignoredFiles) { | 
					
						
							| 
									
										
										
										
											2019-11-06 06:46:34 +08:00
										 |  |  | 					fileTimestamps.set(path, IGNORE_TIME_ENTRY); | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-01-03 19:48:46 +08:00
										 |  |  | 				return fileTimestamps; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2017-01-04 14:20:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-31 14:17:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class WatchIgnorePlugin { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-08-08 00:15:41 +08:00
										 |  |  | 	 * @param {WatchIgnorePluginOptions} options options | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-08-08 00:15:41 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 		validate(options); | 
					
						
							| 
									
										
										
										
											2019-08-08 00:15:41 +08:00
										 |  |  | 		this.paths = options.paths; | 
					
						
							| 
									
										
										
										
											2018-07-31 14:17:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-31 14:17:44 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.afterEnvironment.tap("WatchIgnorePlugin", () => { | 
					
						
							|  |  |  | 			compiler.watchFileSystem = new IgnoringWatchFileSystem( | 
					
						
							|  |  |  | 				compiler.watchFileSystem, | 
					
						
							|  |  |  | 				this.paths | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = WatchIgnorePlugin; |