BasicEffectRulePlugin.js 1.4 KB

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