[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 | /** @typedef {import("./RuleSetCompiler").RuleConditionFunction} RuleConditionFunction */
|
---|
| 12 |
|
---|
| 13 | class ObjectMatcherRulePlugin {
|
---|
| 14 | /**
|
---|
| 15 | * @param {string} ruleProperty the rule property
|
---|
| 16 | * @param {string=} dataProperty the data property
|
---|
| 17 | * @param {RuleConditionFunction=} additionalConditionFunction need to check
|
---|
| 18 | */
|
---|
| 19 | constructor(ruleProperty, dataProperty, additionalConditionFunction) {
|
---|
| 20 | this.ruleProperty = ruleProperty;
|
---|
| 21 | this.dataProperty = dataProperty || ruleProperty;
|
---|
| 22 | this.additionalConditionFunction = additionalConditionFunction;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
|
---|
| 27 | * @returns {void}
|
---|
| 28 | */
|
---|
| 29 | apply(ruleSetCompiler) {
|
---|
| 30 | const { ruleProperty, dataProperty } = this;
|
---|
| 31 | ruleSetCompiler.hooks.rule.tap(
|
---|
| 32 | "ObjectMatcherRulePlugin",
|
---|
| 33 | (path, rule, unhandledProperties, result) => {
|
---|
| 34 | if (unhandledProperties.has(ruleProperty)) {
|
---|
| 35 | unhandledProperties.delete(ruleProperty);
|
---|
| 36 | const value =
|
---|
| 37 | /** @type {Record<string, any>} */
|
---|
| 38 | (rule[/** @type {keyof RuleSetRule} */ (ruleProperty)]);
|
---|
| 39 | for (const property of Object.keys(value)) {
|
---|
| 40 | const nestedDataProperties = property.split(".");
|
---|
| 41 | const condition = ruleSetCompiler.compileCondition(
|
---|
| 42 | `${path}.${ruleProperty}.${property}`,
|
---|
| 43 | value[property]
|
---|
| 44 | );
|
---|
| 45 | if (this.additionalConditionFunction) {
|
---|
| 46 | result.conditions.push({
|
---|
| 47 | property: [dataProperty],
|
---|
| 48 | matchWhenEmpty: condition.matchWhenEmpty,
|
---|
| 49 | fn: this.additionalConditionFunction
|
---|
| 50 | });
|
---|
| 51 | }
|
---|
| 52 | result.conditions.push({
|
---|
| 53 | property: [dataProperty, ...nestedDataProperties],
|
---|
| 54 | matchWhenEmpty: condition.matchWhenEmpty,
|
---|
| 55 | fn: condition.fn
|
---|
| 56 | });
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | );
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | module.exports = ObjectMatcherRulePlugin;
|
---|