source: frontend/node_modules/webpack/lib/rules/ObjectMatcherRulePlugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.8 KB
Line 
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").RuleSetRule} RuleSetRule */
10/** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
11/** @typedef {import("./RuleSetCompiler").EffectData} EffectData */
12/** @typedef {import("./RuleSetCompiler").RuleConditionFunction} RuleConditionFunction */
13
14/**
15 * Defines the keys of types type used by this module.
16 * @template T
17 * @template {T[keyof T]} V
18 * @typedef {import("./RuleSetCompiler").KeysOfTypes<T, V>} KeysOfTypes
19 */
20
21/** @typedef {KeysOfTypes<RuleSetRule, { [k: string]: RuleSetConditionOrConditions }>} ObjectMatcherRuleKeys */
22/** @typedef {keyof EffectData} DataProperty */
23
24const PLUGIN_NAME = "ObjectMatcherRulePlugin";
25
26class ObjectMatcherRulePlugin {
27 /**
28 * Creates an instance of ObjectMatcherRulePlugin.
29 * @param {ObjectMatcherRuleKeys} ruleProperty the rule property
30 * @param {DataProperty=} dataProperty the data property
31 * @param {RuleConditionFunction=} additionalConditionFunction need to check
32 */
33 constructor(ruleProperty, dataProperty, additionalConditionFunction) {
34 /** @type {ObjectMatcherRuleKeys} */
35 this.ruleProperty = ruleProperty;
36 /** @type {DataProperty | ObjectMatcherRuleKeys} */
37 this.dataProperty = dataProperty || ruleProperty;
38 /** @type {RuleConditionFunction | undefined} */
39 this.additionalConditionFunction = additionalConditionFunction;
40 }
41
42 /**
43 * Applies the plugin by registering its hooks on the compiler.
44 * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
45 * @returns {void}
46 */
47 apply(ruleSetCompiler) {
48 const { ruleProperty, dataProperty } = this;
49 ruleSetCompiler.hooks.rule.tap(
50 PLUGIN_NAME,
51 (path, rule, unhandledProperties, result) => {
52 if (unhandledProperties.has(ruleProperty)) {
53 unhandledProperties.delete(ruleProperty);
54 const value =
55 /** @type {Record<string, RuleSetConditionOrConditions>} */
56 (rule[ruleProperty]);
57 for (const property of Object.keys(value)) {
58 const nestedDataProperties = property.split(".");
59 const condition = ruleSetCompiler.compileCondition(
60 `${path}.${ruleProperty}.${property}`,
61 value[property]
62 );
63 if (this.additionalConditionFunction) {
64 result.conditions.push({
65 property: [dataProperty],
66 matchWhenEmpty: condition.matchWhenEmpty,
67 fn: this.additionalConditionFunction
68 });
69 }
70 result.conditions.push({
71 property: [dataProperty, ...nestedDataProperties],
72 matchWhenEmpty: condition.matchWhenEmpty,
73 fn: condition.fn
74 });
75 }
76 }
77 }
78 );
79 }
80}
81
82module.exports = ObjectMatcherRulePlugin;
Note: See TracBrowser for help on using the repository browser.