UNPKG

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