| 
									
										
										
										
											2019-05-16 17:31:41 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-27 08:07:25 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").RuleSetConditionOrConditions} RuleSetConditionOrConditions */ | 
					
						
							|  |  |  | /** @typedef {import("../../declarations/WebpackOptions").RuleSetConditionOrConditionsAbsolute} RuleSetConditionOrConditionsAbsolute */ | 
					
						
							|  |  |  | /** @typedef {import("../../declarations/WebpackOptions").RuleSetLoaderOptions} RuleSetLoaderOptions */ | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */ | 
					
						
							| 
									
										
										
										
											2019-05-16 17:31:41 +08:00
										 |  |  | /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */ | 
					
						
							|  |  |  | /** @typedef {import("./RuleSetCompiler").RuleCondition} RuleCondition */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-27 08:07:25 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  * @template {T[keyof T]} V | 
					
						
							|  |  |  |  * @typedef {import("./RuleSetCompiler").KeysOfTypes<T, V>} KeysOfTypes | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {KeysOfTypes<RuleSetRule, RuleSetConditionOrConditions | RuleSetConditionOrConditionsAbsolute>} BasicMatcherRuleKeys */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | const PLUGIN_NAME = "BasicMatcherRulePlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 17:31:41 +08:00
										 |  |  | class BasicMatcherRulePlugin { | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2025-03-27 08:07:25 +08:00
										 |  |  | 	 * @param {BasicMatcherRuleKeys} ruleProperty the rule property | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | 	 * @param {string=} dataProperty the data property | 
					
						
							|  |  |  | 	 * @param {boolean=} invert if true, inverts the condition | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-05-16 17:31:41 +08:00
										 |  |  | 	constructor(ruleProperty, dataProperty, invert) { | 
					
						
							|  |  |  | 		this.ruleProperty = ruleProperty; | 
					
						
							|  |  |  | 		this.dataProperty = dataProperty || ruleProperty; | 
					
						
							|  |  |  | 		this.invert = invert || false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(ruleSetCompiler) { | 
					
						
							|  |  |  | 		ruleSetCompiler.hooks.rule.tap( | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 			PLUGIN_NAME, | 
					
						
							| 
									
										
										
										
											2019-05-16 17:31:41 +08:00
										 |  |  | 			(path, rule, unhandledProperties, result) => { | 
					
						
							|  |  |  | 				if (unhandledProperties.has(this.ruleProperty)) { | 
					
						
							|  |  |  | 					unhandledProperties.delete(this.ruleProperty); | 
					
						
							| 
									
										
										
										
											2025-03-27 08:07:25 +08:00
										 |  |  | 					const value = rule[this.ruleProperty]; | 
					
						
							| 
									
										
										
										
											2019-05-16 17:31:41 +08:00
										 |  |  | 					const condition = ruleSetCompiler.compileCondition( | 
					
						
							|  |  |  | 						`${path}.${this.ruleProperty}`, | 
					
						
							| 
									
										
										
										
											2025-03-27 08:07:25 +08:00
										 |  |  | 						/** @type {RuleSetConditionOrConditions | RuleSetConditionOrConditionsAbsolute} */ | 
					
						
							|  |  |  | 						(value) | 
					
						
							| 
									
										
										
										
											2019-05-16 17:31:41 +08:00
										 |  |  | 					); | 
					
						
							|  |  |  | 					const fn = condition.fn; | 
					
						
							|  |  |  | 					result.conditions.push({ | 
					
						
							|  |  |  | 						property: this.dataProperty, | 
					
						
							|  |  |  | 						matchWhenEmpty: this.invert | 
					
						
							|  |  |  | 							? !condition.matchWhenEmpty | 
					
						
							|  |  |  | 							: condition.matchWhenEmpty, | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 						fn: this.invert ? (v) => !fn(v) : fn | 
					
						
							| 
									
										
										
										
											2019-05-16 17:31:41 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = BasicMatcherRulePlugin; |