get-loader-options.js 897 B

12345678910111213141516171819202122232425262728293031323334
  1. const normalizeRule = require('./normalize-rule');
  2. const isWebpack1 = require('./is-webpack-1');
  3. /**
  4. * webpack 1 compat loader options finder. Returns normalized options.
  5. * @param {string} loaderPath
  6. * @param {Object|Rule} rule
  7. * @return {Object|null}
  8. */
  9. function getLoaderOptions(loaderPath, rule) {
  10. let multiRuleProp;
  11. if (isWebpack1) {
  12. multiRuleProp = 'loaders';
  13. } else if (rule.oneOf) {
  14. multiRuleProp = 'oneOf';
  15. } else {
  16. multiRuleProp = 'use';
  17. }
  18. const multiRule = typeof rule === 'object' && Array.isArray(rule[multiRuleProp]) ? rule[multiRuleProp] : null;
  19. let options;
  20. if (multiRule) {
  21. const rules = [].concat(...multiRule.map(r => (r.use || r)));
  22. options = rules.map(normalizeRule).find(r => loaderPath.includes(r.loader)).options;
  23. } else {
  24. options = normalizeRule(rule).options;
  25. }
  26. return options;
  27. }
  28. module.exports = getLoaderOptions;