BasicEffectRulePlugin.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
  7. /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
  8. /**
  9. * @template T
  10. * @template {T[keyof T]} V
  11. * @typedef {import("./RuleSetCompiler").KeysOfTypes<T, V>} KeysOfTypes
  12. */
  13. /** @typedef {KeysOfTypes<RuleSetRule, string | boolean | { [k: string]: EXPECTED_ANY }>} BasicEffectRuleKeys */
  14. class BasicEffectRulePlugin {
  15. /**
  16. * @param {BasicEffectRuleKeys} ruleProperty the rule property
  17. * @param {string=} effectType the effect type
  18. */
  19. constructor(ruleProperty, effectType) {
  20. this.ruleProperty = ruleProperty;
  21. this.effectType = effectType || ruleProperty;
  22. }
  23. /**
  24. * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
  25. * @returns {void}
  26. */
  27. apply(ruleSetCompiler) {
  28. ruleSetCompiler.hooks.rule.tap(
  29. "BasicEffectRulePlugin",
  30. (path, rule, unhandledProperties, result, references) => {
  31. if (unhandledProperties.has(this.ruleProperty)) {
  32. unhandledProperties.delete(this.ruleProperty);
  33. const value = rule[this.ruleProperty];
  34. result.effects.push({
  35. type: this.effectType,
  36. value
  37. });
  38. }
  39. }
  40. );
  41. }
  42. }
  43. module.exports = BasicEffectRulePlugin;