-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
42 lines (35 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const path = require("path");
const ruleChildren = (loader) =>
loader.use ||
loader.oneOf ||
(Array.isArray(loader.loader) && loader.loader) ||
[];
const findIndexAndRules = (rulesSource, ruleMatcher) => {
let result;
const rules = Array.isArray(rulesSource)
? rulesSource
: ruleChildren(rulesSource);
rules.some(
(rule, index) =>
(result = ruleMatcher(rule)
? { index, rules }
: findIndexAndRules(ruleChildren(rule), ruleMatcher))
);
return result;
};
const createLoaderMatcher = (loader) => (rule) =>
rule.loader && rule.loader.indexOf(`${path.sep}${loader}${path.sep}`) !== -1;
const fileLoaderMatcher = createLoaderMatcher("file-loader");
const addBeforeRule = (rulesSource, ruleMatcher, value) => {
const { index, rules } = findIndexAndRules(rulesSource, ruleMatcher);
rules.splice(index, 0, value);
};
module.exports = function (config, env, svgrLoaderOptions) {
const svgReactLoader = {
test: /\.svg$/,
loader: require.resolve("@svgr/webpack"),
options: svgrLoaderOptions,
};
addBeforeRule(config.module.rules, fileLoaderMatcher, svgReactLoader);
return config;
};