UNPKG

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