normalize-rule.js 764 B

123456789101112131415161718192021222324252627282930313233
  1. const { parseQuery } = require('loader-utils');
  2. const isWebpack1 = require('./is-webpack-1');
  3. /**
  4. * webpack 1 compat rule normalizer
  5. * @param {string|Rule} rule (string - webpack 1, Object - webpack 2)
  6. * @return {Object<loader: string, options: Object|null>}
  7. */
  8. function normalizeRule(rule) {
  9. if (!rule) {
  10. throw new Error('Rule should be string or object');
  11. }
  12. let data;
  13. if (typeof rule === 'string') {
  14. const parts = rule.split('?');
  15. data = {
  16. loader: parts[0],
  17. options: parts[1] ? parseQuery(`?${parts[1]}`) : null
  18. };
  19. } else {
  20. const options = isWebpack1 ? rule.query : rule.options;
  21. data = {
  22. loader: rule.loader,
  23. options: options || null
  24. };
  25. }
  26. return data;
  27. }
  28. module.exports = normalizeRule;