Skip to content

Commit d47ef5e

Browse files
authored
fix(gatsby-plugin-preact): Adjust framework chunk overwrite (#37658)
1 parent 6f19290 commit d47ef5e

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
const path = require(`path`)
21
const { onCreateWebpackConfig, onCreateBabelConfig } = require(`../gatsby-node`)
32
const PreactRefreshPlugin = require(`@prefresh/webpack`)
43
const ReactRefreshWebpackPlugin = require(`@pmmmwh/react-refresh-webpack-plugin`)
54

5+
const FRAMEWORK_BUNDLES_GATSBY = [
6+
`react`,
7+
`react-dom`,
8+
`scheduler`,
9+
`prop-types`,
10+
]
11+
12+
const FRAMEWORK_BUNDLES_REGEX_GATSBY = new RegExp(
13+
`(?<!node_modules.*)[\\\\/]node_modules[\\\\/](${FRAMEWORK_BUNDLES_GATSBY.join(
14+
`|`
15+
)})[\\\\/]`
16+
)
17+
618
describe(`gatsby-plugin-preact`, () => {
719
it(`sets the correct webpack config in development`, () => {
820
const getConfig = jest.fn(() => {
@@ -53,7 +65,6 @@ describe(`gatsby-plugin-preact`, () => {
5365
})
5466

5567
it(`sets the correct webpack config in production`, () => {
56-
const FRAMEWORK_BUNDLES = [`react`, `react-dom`, `scheduler`, `prop-types`]
5768
const getConfig = jest.fn(() => {
5869
return {
5970
optimization: {
@@ -65,14 +76,20 @@ describe(`gatsby-plugin-preact`, () => {
6576
framework: {
6677
chunks: `all`,
6778
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-
),
79+
// Mirrors what we have in gatsby/../webpack.config.js
80+
test: module => {
81+
if (
82+
module?.rawRequest === `react-dom/server` ||
83+
module?.rawRequest?.includes(`/react-dom-server`)
84+
) {
85+
return false
86+
}
87+
88+
return FRAMEWORK_BUNDLES_REGEX_GATSBY.test(
89+
module.nameForCondition()
90+
)
91+
},
7492
priority: 40,
75-
// Don't let webpack eliminate this chunk (prevents this chunk from becoming a part of the commons chunk)
7693
enforce: true,
7794
},
7895
},
@@ -121,7 +138,7 @@ describe(`gatsby-plugin-preact`, () => {
121138
"enforce": true,
122139
"name": "framework",
123140
"priority": 40,
124-
"test": [Function],
141+
"test": /\\(\\?<!node_modules\\.\\*\\)\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]\\(preact\\|react\\|react-dom\\|scheduler\\|prop-types\\)\\[\\\\\\\\/\\]/,
125142
},
126143
"vendors": false,
127144
},
@@ -141,7 +158,7 @@ describe(`gatsby-plugin-preact`, () => {
141158
"enforce": true,
142159
"name": "framework",
143160
"priority": 40,
144-
"test": /\\(\\?<!node_modules\\.\\*\\)\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]\\(react\\|react-dom\\|scheduler\\|prop-types\\)\\[\\\\\\\\/\\]/,
161+
"test": [Function],
145162
},
146163
"vendors": false,
147164
},

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
const PreactRefreshPlugin = require(`@prefresh/webpack`)
22

3+
const FRAMEWORK_BUNDLES_PREACT = [
4+
`preact`,
5+
`react`,
6+
`react-dom`,
7+
`scheduler`,
8+
`prop-types`,
9+
]
10+
11+
// This regex ignores nested copies of framework libraries so they're bundled with their issuer
12+
const FRAMEWORK_BUNDLES_REGEX_PREACT = new RegExp(
13+
`(?<!node_modules.*)[\\\\/]node_modules[\\\\/](${FRAMEWORK_BUNDLES_PREACT.join(
14+
`|`
15+
)})[\\\\/]`
16+
)
17+
318
export function onCreateBabelConfig({ actions, stage }) {
419
if (stage === `develop`) {
520
// enable react-refresh babel plugin to enable hooks
@@ -45,15 +60,9 @@ export function onCreateWebpackConfig({ stage, actions, getConfig }) {
4560
if (
4661
webpackConfig?.optimization?.splitChunks?.cacheGroups?.framework?.test
4762
) {
48-
const frameworkRegex =
49-
webpackConfig.optimization.splitChunks.cacheGroups.framework.test
50-
5163
// replace react libs with preact
5264
webpackConfig.optimization.splitChunks.cacheGroups.framework.test =
53-
module =>
54-
/(?<!node_modules.*)[\\/]node_modules[\\/](preact)[\\/]/.test(
55-
module.resource
56-
) || frameworkRegex.test(module.resource)
65+
FRAMEWORK_BUNDLES_REGEX_PREACT
5766
}
5867
}
5968

packages/gatsby/src/utils/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ module.exports = async (
635635
framework: {
636636
chunks: `all`,
637637
name: `framework`,
638+
// Important: If you change something here, also update "gatsby-plugin-preact"
638639
test: module => {
639640
// Packages like gatsby-plugin-image might import from "react-dom/server". We don't want to include react-dom-server in the framework bundle.
640641
// A rawRequest might look like these:

0 commit comments

Comments
 (0)