BasicEffectRulePlugin.js 1.6 KB

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