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

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

Fix frontend appearance

  • Property mode set to 100644
File size: 2.3 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").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
22const PLUGIN_NAME = "BasicMatcherRulePlugin";
23
24class 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
71module.exports = BasicMatcherRulePlugin;
Note: See TracBrowser for help on using the repository browser.