| [9af201e] | 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 |
|
|---|
| 11 | /**
|
|---|
| 12 | * Defines the keys of types type used by this module.
|
|---|
| 13 | * @template T
|
|---|
| 14 | * @template {T[keyof T]} V
|
|---|
| 15 | * @typedef {import("./RuleSetCompiler").KeysOfTypes<T, V>} KeysOfTypes
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | /** @typedef {KeysOfTypes<RuleSetRule, string | boolean | { [k: string]: EXPECTED_ANY }>} BasicEffectRuleKeys */
|
|---|
| 19 |
|
|---|
| 20 | const PLUGIN_NAME = "BasicEffectRulePlugin";
|
|---|
| 21 |
|
|---|
| 22 | class BasicEffectRulePlugin {
|
|---|
| 23 | /**
|
|---|
| 24 | * Creates an instance of BasicEffectRulePlugin.
|
|---|
| 25 | * @param {BasicEffectRuleKeys} ruleProperty the rule property
|
|---|
| 26 | * @param {string=} effectType the effect type
|
|---|
| 27 | */
|
|---|
| 28 | constructor(ruleProperty, effectType) {
|
|---|
| 29 | /** @type {BasicEffectRuleKeys} */
|
|---|
| 30 | this.ruleProperty = ruleProperty;
|
|---|
| 31 | /** @type {string | BasicEffectRuleKeys} */
|
|---|
| 32 | this.effectType = effectType || ruleProperty;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 37 | * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
|
|---|
| 38 | * @returns {void}
|
|---|
| 39 | */
|
|---|
| 40 | apply(ruleSetCompiler) {
|
|---|
| 41 | ruleSetCompiler.hooks.rule.tap(
|
|---|
| 42 | PLUGIN_NAME,
|
|---|
| 43 | (path, rule, unhandledProperties, result) => {
|
|---|
| 44 | if (unhandledProperties.has(this.ruleProperty)) {
|
|---|
| 45 | unhandledProperties.delete(this.ruleProperty);
|
|---|
| 46 |
|
|---|
| 47 | const value = rule[this.ruleProperty];
|
|---|
| 48 |
|
|---|
| 49 | result.effects.push({
|
|---|
| 50 | type: this.effectType,
|
|---|
| 51 | value
|
|---|
| 52 | });
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | );
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | module.exports = BasicEffectRulePlugin;
|
|---|