| 
									
										
										
										
											2013-11-06 01:19:04 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2016-12-31 07:17:33 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-27 02:08:09 +08:00
										 |  |  | const validateOptions = require("schema-utils"); | 
					
						
							|  |  |  | const schema = require("../schemas/plugins/IgnorePlugin.json"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/IgnorePlugin").IgnorePluginOptions} IgnorePluginOptions */ | 
					
						
							| 
									
										
										
										
											2018-07-09 20:48:28 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-31 07:17:33 +08:00
										 |  |  | class IgnorePlugin { | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 	 * @param {IgnorePluginOptions} options IgnorePlugin options | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-07-27 02:08:09 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		// TODO webpack 5 remove this compat-layer
 | 
					
						
							|  |  |  | 		if (arguments.length > 1 || options instanceof RegExp) { | 
					
						
							|  |  |  | 			options = { | 
					
						
							|  |  |  | 				resourceRegExp: arguments[0], | 
					
						
							|  |  |  | 				contextRegExp: arguments[1] | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		validateOptions(schema, options, "IgnorePlugin"); | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 18:45:33 +08:00
										 |  |  | 		/** @private @type {Function} */ | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 		this.checkIgnore = this.checkIgnore.bind(this); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Note that if "contextRegExp" is given, both the "resourceRegExp" | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 	 * and "contextRegExp" have to match. | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	 * | 
					
						
							|  |  |  | 	 * @param {TODO} result result | 
					
						
							| 
									
										
										
										
											2018-06-21 18:47:15 +08:00
										 |  |  | 	 * @returns {TODO|null} returns result or null if result should be ignored | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-12-14 21:38:53 +08:00
										 |  |  | 	checkIgnore(result) { | 
					
						
							| 
									
										
										
										
											2018-12-22 20:39:26 +08:00
										 |  |  | 		if (!result) return result; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-18 01:40:51 +08:00
										 |  |  | 		if ( | 
					
						
							|  |  |  | 			"checkResource" in this.options && | 
					
						
							| 
									
										
										
										
											2018-12-20 23:52:06 +08:00
										 |  |  | 			this.options.checkResource && | 
					
						
							| 
									
										
										
										
											2018-12-18 01:40:51 +08:00
										 |  |  | 			this.options.checkResource(result.request, result.context) | 
					
						
							|  |  |  | 		) { | 
					
						
							|  |  |  | 			// TODO webpack 5 remove checkContext, as checkResource already gets context
 | 
					
						
							| 
									
										
										
										
											2018-12-20 23:52:06 +08:00
										 |  |  | 			if ("checkContext" in this.options && this.options.checkContext) { | 
					
						
							| 
									
										
										
										
											2018-12-18 01:40:51 +08:00
										 |  |  | 				if (this.options.checkContext(result.context)) { | 
					
						
							|  |  |  | 					return null; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				return null; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-12-18 01:40:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if ( | 
					
						
							|  |  |  | 			"resourceRegExp" in this.options && | 
					
						
							| 
									
										
										
										
											2018-12-20 23:52:06 +08:00
										 |  |  | 			this.options.resourceRegExp && | 
					
						
							| 
									
										
										
										
											2018-12-18 01:40:51 +08:00
										 |  |  | 			this.options.resourceRegExp.test(result.request) | 
					
						
							|  |  |  | 		) { | 
					
						
							| 
									
										
										
										
											2018-12-20 23:52:06 +08:00
										 |  |  | 			if ("contextRegExp" in this.options && this.options.contextRegExp) { | 
					
						
							| 
									
										
										
										
											2018-12-18 01:40:51 +08:00
										 |  |  | 				// if "contextRegExp" is given,
 | 
					
						
							|  |  |  | 				// both the "resourceRegExp" and "contextRegExp" have to match.
 | 
					
						
							|  |  |  | 				if (this.options.contextRegExp.test(result.context)) { | 
					
						
							|  |  |  | 					return null; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				return null; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 21:38:53 +08:00
										 |  |  | 		return result; | 
					
						
							| 
									
										
										
										
											2016-12-31 07:17:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler Webpack Compiler | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2016-12-31 07:17:33 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.normalModuleFactory.tap("IgnorePlugin", nmf => { | 
					
						
							| 
									
										
										
										
											2017-12-14 21:38:53 +08:00
										 |  |  | 			nmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore); | 
					
						
							| 
									
										
										
										
											2013-11-06 01:19:04 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.contextModuleFactory.tap("IgnorePlugin", cmf => { | 
					
						
							| 
									
										
										
										
											2017-12-14 21:38:53 +08:00
										 |  |  | 			cmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore); | 
					
						
							| 
									
										
										
										
											2013-11-06 01:19:04 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-31 07:17:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = IgnorePlugin; |