ObjectMatcherRulePlugin.js 2.8 KB

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