| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /** @typedef {import("../../declarations/WebpackOptions").RuleSetConditionOrConditions} RuleSetConditionOrConditions */
|
|---|
| 9 | /** @typedef {import("../../declarations/WebpackOptions").RuleSetConditionOrConditionsAbsolute} RuleSetConditionOrConditionsAbsolute */
|
|---|
| 10 | /** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
|
|---|
| 11 | /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * Defines the keys of types type used by this module.
|
|---|
| 15 | * @template T
|
|---|
| 16 | * @template {T[keyof T]} V
|
|---|
| 17 | * @typedef {import("./RuleSetCompiler").KeysOfTypes<T, V>} KeysOfTypes
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | /** @typedef {KeysOfTypes<RuleSetRule, RuleSetConditionOrConditions | RuleSetConditionOrConditionsAbsolute>} BasicMatcherRuleKeys */
|
|---|
| 21 |
|
|---|
| 22 | const PLUGIN_NAME = "BasicMatcherRulePlugin";
|
|---|
| 23 |
|
|---|
| 24 | class BasicMatcherRulePlugin {
|
|---|
| 25 | /**
|
|---|
| 26 | * Creates an instance of BasicMatcherRulePlugin.
|
|---|
| 27 | * @param {BasicMatcherRuleKeys} ruleProperty the rule property
|
|---|
| 28 | * @param {string=} dataProperty the data property
|
|---|
| 29 | * @param {boolean=} invert if true, inverts the condition
|
|---|
| 30 | */
|
|---|
| 31 | constructor(ruleProperty, dataProperty, invert) {
|
|---|
| 32 | /** @type {BasicMatcherRuleKeys} */
|
|---|
| 33 | this.ruleProperty = ruleProperty;
|
|---|
| 34 | /** @type {string | BasicMatcherRuleKeys} */
|
|---|
| 35 | this.dataProperty = dataProperty || ruleProperty;
|
|---|
| 36 | /** @type {boolean} */
|
|---|
| 37 | this.invert = invert || false;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 42 | * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
|
|---|
| 43 | * @returns {void}
|
|---|
| 44 | */
|
|---|
| 45 | apply(ruleSetCompiler) {
|
|---|
| 46 | ruleSetCompiler.hooks.rule.tap(
|
|---|
| 47 | PLUGIN_NAME,
|
|---|
| 48 | (path, rule, unhandledProperties, result) => {
|
|---|
| 49 | if (unhandledProperties.has(this.ruleProperty)) {
|
|---|
| 50 | unhandledProperties.delete(this.ruleProperty);
|
|---|
| 51 | const value = rule[this.ruleProperty];
|
|---|
| 52 | const condition = ruleSetCompiler.compileCondition(
|
|---|
| 53 | `${path}.${this.ruleProperty}`,
|
|---|
| 54 | /** @type {RuleSetConditionOrConditions | RuleSetConditionOrConditionsAbsolute} */
|
|---|
| 55 | (value)
|
|---|
| 56 | );
|
|---|
| 57 | const fn = condition.fn;
|
|---|
| 58 | result.conditions.push({
|
|---|
| 59 | property: this.dataProperty,
|
|---|
| 60 | matchWhenEmpty: this.invert
|
|---|
| 61 | ? !condition.matchWhenEmpty
|
|---|
| 62 | : condition.matchWhenEmpty,
|
|---|
| 63 | fn: this.invert ? (v) => !fn(v) : fn
|
|---|
| 64 | });
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 | );
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | module.exports = BasicMatcherRulePlugin;
|
|---|