get-matched-rules.js 925 B

123456789101112131415161718192021222324252627282930313233
  1. /* eslint-disable import/no-extraneous-dependencies */
  2. const ruleMatcher = require('webpack/lib/ModuleFilenameHelpers').matchObject;
  3. const isWebpack1 = require('./is-webpack-1');
  4. const RuleSet = !isWebpack1 ? require('webpack/lib/RuleSet') : null;
  5. /**
  6. * @param {string} request
  7. * @param {Rule[]} rules Webpack loaders config
  8. * @return {Rule[]}
  9. */
  10. function getMatchedRules(request, rules, issuer) {
  11. const matchedRules = rules.filter((rule) => {
  12. return typeof rule.test === 'function'
  13. ? rule.test(request)
  14. : ruleMatcher(rule, request);
  15. });
  16. if (issuer) {
  17. return matchedRules.filter((rule) => {
  18. // If rule doesn't have an issuer or RuleSet is not available
  19. if (!rule.issuer || !RuleSet) {
  20. return true;
  21. }
  22. const matcher = RuleSet.normalizeCondition(rule.issuer);
  23. return matcher(issuer);
  24. });
  25. }
  26. return matchedRules;
  27. }
  28. module.exports = getMatchedRules;