[79a0317] | 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").RuleSetRule} RuleSetRule */
|
---|
| 9 | /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
|
---|
| 10 | /** @typedef {import("./RuleSetCompiler").RuleCondition} RuleCondition */
|
---|
| 11 |
|
---|
| 12 | class BasicMatcherRulePlugin {
|
---|
| 13 | /**
|
---|
| 14 | * @param {string} ruleProperty the rule property
|
---|
| 15 | * @param {string=} dataProperty the data property
|
---|
| 16 | * @param {boolean=} invert if true, inverts the condition
|
---|
| 17 | */
|
---|
| 18 | constructor(ruleProperty, dataProperty, invert) {
|
---|
| 19 | this.ruleProperty = ruleProperty;
|
---|
| 20 | this.dataProperty = dataProperty || ruleProperty;
|
---|
| 21 | this.invert = invert || false;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
|
---|
| 26 | * @returns {void}
|
---|
| 27 | */
|
---|
| 28 | apply(ruleSetCompiler) {
|
---|
| 29 | ruleSetCompiler.hooks.rule.tap(
|
---|
| 30 | "BasicMatcherRulePlugin",
|
---|
| 31 | (path, rule, unhandledProperties, result) => {
|
---|
| 32 | if (unhandledProperties.has(this.ruleProperty)) {
|
---|
| 33 | unhandledProperties.delete(this.ruleProperty);
|
---|
| 34 | const value =
|
---|
| 35 | rule[/** @type {keyof RuleSetRule} */ (this.ruleProperty)];
|
---|
| 36 | const condition = ruleSetCompiler.compileCondition(
|
---|
| 37 | `${path}.${this.ruleProperty}`,
|
---|
| 38 | value
|
---|
| 39 | );
|
---|
| 40 | const fn = condition.fn;
|
---|
| 41 | result.conditions.push({
|
---|
| 42 | property: this.dataProperty,
|
---|
| 43 | matchWhenEmpty: this.invert
|
---|
| 44 | ? !condition.matchWhenEmpty
|
---|
| 45 | : condition.matchWhenEmpty,
|
---|
| 46 | fn: this.invert ? v => !fn(v) : fn
|
---|
| 47 | });
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | );
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | module.exports = BasicMatcherRulePlugin;
|
---|