-
Notifications
You must be signed in to change notification settings - Fork 20
/
gatsby-node.js
103 lines (89 loc) · 2.04 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const resolve = (module) => require.resolve(module)
exports.onCreateWebpackConfig = (
{ getConfig, actions, stage, loaders },
{ plugins, include, exclude, ...svgrOptions }
) => {
const { replaceWebpackConfig, setWebpackConfig } = actions
const existingConfig = getConfig()
const rules = existingConfig.module.rules.map((rule) => {
// Gatsby < 2.3.0 (no AVIF support)
if (
String(rule.test) === String(/\.(ico|svg|jpg|jpeg|png|gif|webp)(\?.*)?$/)
) {
return {
...rule,
test: /\.(ico|jpg|jpeg|png|gif|webp)(\?.*)?$/,
}
}
// Gatsby ≥ 2.3.0 (AVIF support)
if (
String(rule.test) ===
String(/\.(ico|svg|jpg|jpeg|png|gif|webp|avif)(\?.*)?$/)
) {
return {
...rule,
test: /\.(ico|jpg|jpeg|png|gif|webp|avif)(\?.*)?$/,
}
}
return rule
})
replaceWebpackConfig({
...existingConfig,
module: {
...existingConfig.module,
rules,
},
})
const urlLoader = loaders.url({
name: "static/[name].[hash:8].[ext]",
limit: 10000,
})
// for non-javascript issuers
const nonJs = {
test: /\.svg$/,
use: [urlLoader],
issuer: {
test: /\.(?!(js|jsx|ts|tsx)$)([^.]+$)/,
},
}
const svgrLoader = {
loader: resolve(`@svgr/webpack`),
options: svgrOptions,
}
// add new svg rule
const svgrRule = {
test: /\.svg$/,
use: [svgrLoader, urlLoader],
issuer: {
test: /\.(js|jsx|ts|tsx)$/,
},
include,
exclude,
}
// for excluded assets
const excludedRule = {
test: /\.svg$/,
use: urlLoader,
issuer: svgrRule.issuer,
include: exclude,
exclude: include,
}
let configRules = []
switch (stage) {
case `develop`:
case `build-javascript`:
case `build-html`:
case `develop-html`:
if (include || exclude) {
configRules = configRules.concat([excludedRule])
}
configRules = configRules.concat([nonJs, svgrRule])
break
default:
}
setWebpackConfig({
module: {
rules: configRules,
},
})
}