| 
									
										
										
										
											2013-11-06 01:19:04 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-31 07:17:33 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 08:01:04 +08:00
										 |  |  | const RawModule = require("./RawModule"); | 
					
						
							|  |  |  | const EntryDependency = require("./dependencies/EntryDependency"); | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const createSchemaValidation = require("./util/create-schema-validation"); | 
					
						
							| 
									
										
										
										
											2018-07-27 02:08:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 */ | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | /** @typedef {import("./NormalModuleFactory").ResolveData} ResolveData */ | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const validate = createSchemaValidation( | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 	require("../schemas/plugins/IgnorePlugin.check"), | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 	() => require("../schemas/plugins/IgnorePlugin.json"), | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		name: "Ignore Plugin", | 
					
						
							|  |  |  | 		baseDataPath: "options" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | const PLUGIN_NAME = "IgnorePlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 		validate(options); | 
					
						
							| 
									
										
										
										
											2018-07-27 02:08:09 +08:00
										 |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											2017-04-08 13:36:30 +08:00
										 |  |  | 		this.checkIgnore = this.checkIgnore.bind(this); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2024-07-30 21:48:58 +08:00
										 |  |  | 	 * Note that if "contextRegExp" is given, both the "resourceRegExp" and "contextRegExp" have to match. | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 	 * @param {ResolveData} resolveData resolve data | 
					
						
							|  |  |  | 	 * @returns {false|undefined} returns false when the request should be ignored, otherwise undefined | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 	checkIgnore(resolveData) { | 
					
						
							| 
									
										
										
										
											2018-12-18 01:40:51 +08:00
										 |  |  | 		if ( | 
					
						
							|  |  |  | 			"checkResource" in this.options && | 
					
						
							| 
									
										
										
										
											2018-12-20 23:52:06 +08:00
										 |  |  | 			this.options.checkResource && | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 			this.options.checkResource(resolveData.request, resolveData.context) | 
					
						
							| 
									
										
										
										
											2018-12-18 01:40:51 +08:00
										 |  |  | 		) { | 
					
						
							| 
									
										
										
										
											2019-06-14 16:45:56 +08:00
										 |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											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 && | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 			this.options.resourceRegExp.test(resolveData.request) | 
					
						
							| 
									
										
										
										
											2018-12-18 01:40:51 +08:00
										 |  |  | 		) { | 
					
						
							| 
									
										
										
										
											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.
 | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 				if (this.options.contextRegExp.test(resolveData.context)) { | 
					
						
							|  |  |  | 					return false; | 
					
						
							| 
									
										
										
										
											2018-12-18 01:40:51 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 				return false; | 
					
						
							| 
									
										
										
										
											2018-12-18 01:40:51 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-31 07:17:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2018-06-21 18:28:20 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2016-12-31 07:17:33 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (nmf) => { | 
					
						
							|  |  |  | 			nmf.hooks.beforeResolve.tap(PLUGIN_NAME, (resolveData) => { | 
					
						
							| 
									
										
										
										
											2024-10-23 08:01:04 +08:00
										 |  |  | 				const result = this.checkIgnore(resolveData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if ( | 
					
						
							|  |  |  | 					result === false && | 
					
						
							|  |  |  | 					resolveData.dependencies.length > 0 && | 
					
						
							|  |  |  | 					resolveData.dependencies[0] instanceof EntryDependency | 
					
						
							|  |  |  | 				) { | 
					
						
							| 
									
										
										
										
											2025-07-10 21:23:05 +08:00
										 |  |  | 					const module = new RawModule( | 
					
						
							| 
									
										
										
										
											2024-10-23 08:01:04 +08:00
										 |  |  | 						"", | 
					
						
							|  |  |  | 						"ignored-entry-module", | 
					
						
							|  |  |  | 						"(ignored-entry-module)" | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2025-07-10 21:23:05 +08:00
										 |  |  | 					module.factoryMeta = { sideEffectFree: true }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					resolveData.ignoredModule = module; | 
					
						
							| 
									
										
										
										
											2024-10-23 08:01:04 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				return result; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-11-06 01:19:04 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		compiler.hooks.contextModuleFactory.tap(PLUGIN_NAME, (cmf) => { | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 			cmf.hooks.beforeResolve.tap(PLUGIN_NAME, this.checkIgnore); | 
					
						
							| 
									
										
										
										
											2013-11-06 01:19:04 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-31 07:17:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = IgnorePlugin; |