ObjectMatcherRulePlugin.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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").RuleSetConditionOrConditions} RuleSetConditionOrConditions */
  7. /** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */
  8. /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
  9. /** @typedef {import("./RuleSetCompiler").EffectData} EffectData */
  10. /** @typedef {import("./RuleSetCompiler").RuleConditionFunction} RuleConditionFunction */
  11. /**
  12. * @template T
  13. * @template {T[keyof T]} V
  14. * @typedef {import("./RuleSetCompiler").KeysOfTypes<T, V>} KeysOfTypes
  15. */
  16. /** @typedef {KeysOfTypes<RuleSetRule, { [k: string]: RuleSetConditionOrConditions }>} ObjectMatcherRuleKeys */
  17. /** @typedef {keyof EffectData} DataProperty */
  18. const PLUGIN_NAME = "ObjectMatcherRulePlugin";
  19. class ObjectMatcherRulePlugin {
  20. /**
  21. * @param {ObjectMatcherRuleKeys} ruleProperty the rule property
  22. * @param {DataProperty=} dataProperty the data property
  23. * @param {RuleConditionFunction=} additionalConditionFunction need to check
  24. */
  25. constructor(ruleProperty, dataProperty, additionalConditionFunction) {
  26. /** @type {ObjectMatcherRuleKeys} */
  27. this.ruleProperty = ruleProperty;
  28. /** @type {DataProperty | ObjectMatcherRuleKeys} */
  29. this.dataProperty = dataProperty || ruleProperty;
  30. /** @type {RuleConditionFunction | undefined} */
  31. this.additionalConditionFunction = additionalConditionFunction;
  32. }
  33. /**
  34. * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
  35. * @returns {void}
  36. */
  37. apply(ruleSetCompiler) {
  38. const { ruleProperty, dataProperty } = this;
  39. ruleSetCompiler.hooks.rule.tap(
  40. PLUGIN_NAME,
  41. (path, rule, unhandledProperties, result) => {
  42. if (unhandledProperties.has(ruleProperty)) {
  43. unhandledProperties.delete(ruleProperty);
  44. const value =
  45. /** @type {Record<string, RuleSetConditionOrConditions>} */
  46. (rule[ruleProperty]);
  47. for (const property of Object.keys(value)) {
  48. const nestedDataProperties = property.split(".");
  49. const condition = ruleSetCompiler.compileCondition(
  50. `${path}.${ruleProperty}.${property}`,
  51. value[property]
  52. );
  53. if (this.additionalConditionFunction) {
  54. result.conditions.push({
  55. property: [dataProperty],
  56. matchWhenEmpty: condition.matchWhenEmpty,
  57. fn: this.additionalConditionFunction
  58. });
  59. }
  60. result.conditions.push({
  61. property: [dataProperty, ...nestedDataProperties],
  62. matchWhenEmpty: condition.matchWhenEmpty,
  63. fn: condition.fn
  64. });
  65. }
  66. }
  67. }
  68. );
  69. }
  70. }
  71. module.exports = ObjectMatcherRulePlugin;