-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
82 lines (71 loc) · 2.24 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
const PreactRefreshPlugin = require(`@prefresh/webpack`)
const FRAMEWORK_BUNDLES_PREACT = [
`preact`,
`react`,
`react-dom`,
`scheduler`,
`prop-types`,
]
// This regex ignores nested copies of framework libraries so they're bundled with their issuer
const FRAMEWORK_BUNDLES_REGEX_PREACT = new RegExp(
`(?<!node_modules.*)[\\\\/]node_modules[\\\\/](${FRAMEWORK_BUNDLES_PREACT.join(
`|`
)})[\\\\/]`
)
export function onCreateBabelConfig({ actions, stage }) {
if (stage === `develop`) {
// enable react-refresh babel plugin to enable hooks
// @see https://github.com/JoviDeCroock/prefresh/tree/master/packages/webpack#using-hooks
actions.setBabelPlugin({
name: `@prefresh/babel-plugin`,
stage,
})
}
}
export function onCreateWebpackConfig({ stage, actions, getConfig }) {
const webpackPlugins = []
const webpackConfig = getConfig()
if (webpackConfig.resolve?.alias) {
delete webpackConfig.resolve.alias.react
delete webpackConfig.resolve.alias[`react-dom`]
}
if (stage === `develop`) {
webpackPlugins.push(
new PreactRefreshPlugin({
overlay: {
module: require.resolve(`gatsby/dist/utils/fast-refresh-module`),
},
})
)
// remove React refresh plugin, we want to add preact refresh instead.
webpackConfig.plugins = webpackConfig.plugins.filter(
plugin => plugin.constructor.name !== `ReactRefreshPlugin`
)
// add webpack-hot-middleware/client to the commons entry
webpackConfig.entry.commons.unshift(
`@gatsbyjs/webpack-hot-middleware/client`
)
}
// add preact to the framework bundle
if (stage === `build-javascript` || stage === `develop`) {
if (
webpackConfig?.optimization?.splitChunks?.cacheGroups?.framework?.test
) {
// replace react libs with preact
webpackConfig.optimization.splitChunks.cacheGroups.framework.test =
FRAMEWORK_BUNDLES_REGEX_PREACT
}
}
actions.replaceWebpackConfig(webpackConfig)
actions.setWebpackConfig({
resolve: {
alias: {
react: `preact/compat`,
"react-dom/test-utils": `preact/test-utils`,
"react-dom": `preact/compat`,
"react/jsx-runtime": `preact/jsx-runtime`,
},
},
plugins: webpackPlugins,
})
}