ObjectMatcherRulePlugin.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /**
  7. * @import {
  8. * RuleSetConditionOrConditions,
  9. * RuleSetRule
  10. * } from "../../declarations/WebpackOptions"
  11. */
  12. /**
  13. * @import RuleSetCompiler, {
  14. * EffectData,
  15. * RuleConditionFunction
  16. * } from "./RuleSetCompiler"
  17. */
  18. /**
  19. * Defines the keys of types type used by this module.
  20. * @template T
  21. * @template {T[keyof T]} V
  22. * @typedef {import("./RuleSetCompiler").KeysOfTypes<T, V>} KeysOfTypes
  23. */
  24. /** @typedef {KeysOfTypes<RuleSetRule, { [k: string]: RuleSetConditionOrConditions }>} ObjectMatcherRuleKeys */
  25. /** @typedef {keyof EffectData} DataProperty */
  26. const PLUGIN_NAME = "ObjectMatcherRulePlugin";
  27. class ObjectMatcherRulePlugin {
  28. /**
  29. * Creates an instance of ObjectMatcherRulePlugin.
  30. * @param {ObjectMatcherRuleKeys} ruleProperty the rule property
  31. * @param {DataProperty=} dataProperty the data property
  32. * @param {RuleConditionFunction=} additionalConditionFunction need to check
  33. */
  34. constructor(ruleProperty, dataProperty, additionalConditionFunction) {
  35. /** @type {ObjectMatcherRuleKeys} */
  36. this.ruleProperty = ruleProperty;
  37. /** @type {DataProperty | ObjectMatcherRuleKeys} */
  38. this.dataProperty = dataProperty || ruleProperty;
  39. /** @type {RuleConditionFunction | undefined} */
  40. this.additionalConditionFunction = additionalConditionFunction;
  41. }
  42. /**
  43. * Applies the plugin by registering its hooks on the compiler.
  44. * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
  45. * @returns {void}
  46. */
  47. apply(ruleSetCompiler) {
  48. const { ruleProperty, dataProperty } = this;
  49. ruleSetCompiler.hooks.rule.tap(
  50. PLUGIN_NAME,
  51. (path, rule, unhandledProperties, result) => {
  52. if (unhandledProperties.has(ruleProperty)) {
  53. unhandledProperties.delete(ruleProperty);
  54. const value =
  55. /** @type {Record<string, RuleSetConditionOrConditions>} */
  56. (rule[ruleProperty]);
  57. for (const property of Object.keys(value)) {
  58. const nestedDataProperties = property.split(".");
  59. const condition = ruleSetCompiler.compileCondition(
  60. `${path}.${ruleProperty}.${property}`,
  61. value[property]
  62. );
  63. if (this.additionalConditionFunction) {
  64. result.conditions.push({
  65. property: [dataProperty],
  66. matchWhenEmpty: condition.matchWhenEmpty,
  67. fn: this.additionalConditionFunction
  68. });
  69. }
  70. result.conditions.push({
  71. property: [dataProperty, ...nestedDataProperties],
  72. matchWhenEmpty: condition.matchWhenEmpty,
  73. fn: condition.fn
  74. });
  75. }
  76. }
  77. }
  78. );
  79. }
  80. }
  81. module.exports = ObjectMatcherRulePlugin;