Skip to content

Commit 0772801

Browse files
wardpeetdevelopitgatsbybotpieh
authored
fix(gatsby-plugin-preact): add preact to framework bundle (#24154)
* fix(gatsby-plugin-preact): add preact to framework bundle * Update packages/gatsby-plugin-preact/src/gatsby-node.js Co-authored-by: Jason Miller <[email protected]> * chore: format * remove unused code * fix splitchunks * Update packages/gatsby-plugin-preact/src/gatsby-node.js Co-authored-by: Michal Piechowiak <[email protected]> Co-authored-by: Jason Miller <[email protected]> Co-authored-by: gatsbybot <[email protected]> Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 63bbd26 commit 0772801

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

packages/gatsby-plugin-preact/src/__tests__/gatsby-node.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,33 @@ describe(`gatsby-plugin-preact`, () => {
5353
})
5454

5555
it(`sets the correct webpack config in production`, () => {
56-
const getConfig = jest.fn()
56+
const FRAMEWORK_BUNDLES = [`react`, `react-dom`, `scheduler`, `prop-types`]
57+
const getConfig = jest.fn(() => {
58+
return {
59+
optimization: {
60+
splitChunks: {
61+
chunks: `all`,
62+
cacheGroups: {
63+
default: false,
64+
vendors: false,
65+
framework: {
66+
chunks: `all`,
67+
name: `framework`,
68+
// This regex ignores nested copies of framework libraries so they're bundled with their issuer.
69+
test: new RegExp(
70+
`(?<!node_modules.*)[\\\\/]node_modules[\\\\/](${FRAMEWORK_BUNDLES.join(
71+
`|`
72+
)})[\\\\/]`
73+
),
74+
priority: 40,
75+
// Don't let webpack eliminate this chunk (prevents this chunk from becoming a part of the commons chunk)
76+
enforce: true,
77+
},
78+
},
79+
},
80+
},
81+
}
82+
})
5783
const actions = {
5884
setWebpackConfig: jest.fn(),
5985
setBabelPlugin: jest.fn(),
@@ -73,8 +99,40 @@ describe(`gatsby-plugin-preact`, () => {
7399
},
74100
})
75101

76-
expect(getConfig).toHaveBeenCalledTimes(0)
77-
expect(actions.replaceWebpackConfig).toHaveBeenCalledTimes(0)
102+
expect(getConfig).toHaveBeenCalledTimes(1)
78103
expect(actions.setBabelPlugin).toHaveBeenCalledTimes(0)
104+
expect(actions.replaceWebpackConfig).toHaveBeenCalledTimes(1)
105+
expect(actions.replaceWebpackConfig).toMatchInlineSnapshot(`
106+
[MockFunction] {
107+
"calls": Array [
108+
Array [
109+
Object {
110+
"optimization": Object {
111+
"splitChunks": Object {
112+
"cacheGroups": Object {
113+
"default": false,
114+
"framework": Object {
115+
"chunks": "all",
116+
"enforce": true,
117+
"name": "framework",
118+
"priority": 40,
119+
"test": [Function],
120+
},
121+
"vendors": false,
122+
},
123+
"chunks": "all",
124+
},
125+
},
126+
},
127+
],
128+
],
129+
"results": Array [
130+
Object {
131+
"type": "return",
132+
"value": undefined,
133+
},
134+
],
135+
}
136+
`)
79137
})
80138
})

packages/gatsby-plugin-preact/src/gatsby-node.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
2424
})
2525
}
2626

27+
// add preact to the framework bundle
28+
if (stage === `build-javascript`) {
29+
const webpackConfig = getConfig()
30+
if (
31+
webpackConfig?.optimization?.splitChunks?.cacheGroups?.framework?.test
32+
) {
33+
const frameworkRegex =
34+
webpackConfig.optimization.splitChunks.cacheGroups.framework.test
35+
36+
// replace react libs with preact
37+
webpackConfig.optimization.splitChunks.cacheGroups.framework.test = module =>
38+
/(?<!node_modules.*)[\\/]node_modules[\\/](preact)[\\/]/.test(
39+
module.resource
40+
) || frameworkRegex.test(module.resource)
41+
42+
actions.replaceWebpackConfig(webpackConfig)
43+
}
44+
}
45+
2746
actions.setWebpackConfig({
2847
resolve: {
2948
alias: {

0 commit comments

Comments
 (0)