get-matched-rule.js 635 B

123456789101112131415161718192021
  1. // eslint-disable-next-line import/no-extraneous-dependencies
  2. const RuleSet = require('webpack/lib/RuleSet');
  3. const flattenAndExtractUse = rules => rules.reduce((pre, rule) => {
  4. if ('rules' in rule || 'oneOf' in rule) {
  5. return pre.concat(flattenAndExtractUse(rule.rules || rule.oneOf));
  6. }
  7. return pre.concat(rule.use || []);
  8. }, []);
  9. module.exports = (compiler) => {
  10. const rawRules = compiler.options.module.rules;
  11. const { rules } = new RuleSet(rawRules);
  12. const rule = flattenAndExtractUse(rules)
  13. .find((item) => {
  14. return /svg-sprite-loader/.test(item.loader);
  15. }) || {};
  16. return rule.options || {};
  17. };