BasicEffectRulePlugin.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 =
  34. rule[/** @type {keyof RuleSetRule} */ (this.ruleProperty)];
  35. result.effects.push({
  36. type: this.effectType,
  37. value
  38. });
  39. }
  40. }
  41. );
  42. }
  43. }
  44. module.exports = BasicEffectRulePlugin;