-
Notifications
You must be signed in to change notification settings - Fork 21
/
gatsby-node.js
69 lines (64 loc) · 1.5 KB
/
gatsby-node.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
exports.onCreateWebpackConfig = ({
stage, actions, getConfig, rules
}, { rule: ruleProps = {} }) => {
const { include, exclude, omitKeys, options = {}, ...otherProps } = ruleProps
if([
'develop',
'develop-html',
'build-html',
'build-javascript'
].includes(stage)) {
if (omitKeys && Array.isArray(omitKeys) && omitKeys.length) {
const removals = new RegExp(omitKeys.join('|'), 'i')
if (!Array.isArray(options.filters)) {
options.filters = []
}
options.filters.push(function(value) {
Object.keys(value).forEach(function(key) {
if (removals.test(key)) {
delete value[key];
}
});
})
}
// Add the svg-react-loader rule
actions.setWebpackConfig({
module: {
rules: [
{
test: /\.svg$/,
include,
exclude,
...otherProps,
use: {
loader: 'svg-react-loader',
options
},
}
],
}
})
const cfg = getConfig()
const imgsRule = rules.images()
const newUrlLoaderRule = (include || exclude) ? {
...imgsRule,
include: exclude,
exclude: include
} : {
...imgsRule,
test: new RegExp(imgsRule.test.toString().replace('svg|', '').slice(1, -1))
}
cfg.module.rules = [
// Remove the base url-loader images rule entirely
...cfg.module.rules.filter(rule => {
if(rule.test) {
return rule.test.toString() !== imgsRule.test.toString()
}
return true
}),
// Put it back without SVG loading
newUrlLoaderRule
]
actions.replaceWebpackConfig(cfg)
}
}