| 
									
										
										
										
											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
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-06-21 18:53:41 +08:00
										 |  |  | 	 * @param {string} resource resource | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	 * @returns {boolean} returns true if a "resourceRegExp" exists | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 	 * and the resource given matches the regexp. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-04-26 04:07:43 +08:00
										 |  |  | 	checkResource(resource) { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 		if ("checkResource" in this.options && this.options.checkResource) { | 
					
						
							| 
									
										
										
										
											2018-07-27 02:08:09 +08:00
										 |  |  | 			return this.options.checkResource(resource); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 		if ("resourceRegExp" in this.options && this.options.resourceRegExp) { | 
					
						
							|  |  |  | 			return this.options.resourceRegExp.test(resource); | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-06-21 18:53:41 +08:00
										 |  |  | 	 * @param {string} context context | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	 * @returns {boolean} returns true if "contextRegExp" does not exist | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 	 * or if context matches the given regexp. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	checkContext(context) { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 		if ("checkContext" in this.options && this.options.checkContext) { | 
					
						
							| 
									
										
										
										
											2018-07-27 02:08:09 +08:00
										 |  |  | 			return this.options.checkContext(context); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 		if ("contextRegExp" in this.options && this.options.contextRegExp) { | 
					
						
							|  |  |  | 			return this.options.contextRegExp.test(context); | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-09-20 16:13:55 +08:00
										 |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 	 * @returns {boolean} returns true if result should be ignored | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-04-08 21:42:10 +08:00
										 |  |  | 	checkResult(result) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (!result) { | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		return ( | 
					
						
							|  |  |  | 			this.checkResource(result.request) && this.checkContext(result.context) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 		// check if result is ignored
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.checkResult(result)) { | 
					
						
							| 
									
										
										
										
											2017-12-14 21:38:53 +08:00
										 |  |  | 			return null; | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											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; |