BasicEffectRulePlugin.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. const PLUGIN_NAME = "BasicEffectRulePlugin";
  15. class BasicEffectRulePlugin {
  16. /**
  17. * @param {BasicEffectRuleKeys} ruleProperty the rule property
  18. * @param {string=} effectType the effect type
  19. */
  20. constructor(ruleProperty, effectType) {
  21. this.ruleProperty = ruleProperty;
  22. this.effectType = effectType || ruleProperty;
  23. }
  24. /**
  25. * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
  26. * @returns {void}
  27. */
  28. apply(ruleSetCompiler) {
  29. ruleSetCompiler.hooks.rule.tap(
  30. PLUGIN_NAME,
  31. (path, rule, unhandledProperties, result) => {
  32. if (unhandledProperties.has(this.ruleProperty)) {
  33. unhandledProperties.delete(this.ruleProperty);
  34. const value = rule[this.ruleProperty];
  35. result.effects.push({
  36. type: this.effectType,
  37. value
  38. });
  39. }
  40. }
  41. );
  42. }
  43. }
  44. module.exports = BasicEffectRulePlugin;