Skip to content

Commit

Permalink
fix(gatsby-plugin-preact): Adjust framework chunk overwrite (#37658) (#…
Browse files Browse the repository at this point in the history
…37666)

Co-authored-by: Lennart <lekoarts@gmail.com>
  • Loading branch information
ViCo0TeCH and LekoArts committed Feb 16, 2023
1 parent 62d2ef9 commit b5978a6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
39 changes: 28 additions & 11 deletions packages/gatsby-plugin-preact/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
const path = require(`path`)
const { onCreateWebpackConfig, onCreateBabelConfig } = require(`../gatsby-node`)
const PreactRefreshPlugin = require(`@prefresh/webpack`)
const ReactRefreshWebpackPlugin = require(`@pmmmwh/react-refresh-webpack-plugin`)

const FRAMEWORK_BUNDLES_GATSBY = [
`react`,
`react-dom`,
`scheduler`,
`prop-types`,
]

const FRAMEWORK_BUNDLES_REGEX_GATSBY = new RegExp(
`(?<!node_modules.*)[\\\\/]node_modules[\\\\/](${FRAMEWORK_BUNDLES_GATSBY.join(
`|`
)})[\\\\/]`
)

describe(`gatsby-plugin-preact`, () => {
it(`sets the correct webpack config in development`, () => {
const getConfig = jest.fn(() => {
Expand Down Expand Up @@ -53,7 +65,6 @@ describe(`gatsby-plugin-preact`, () => {
})

it(`sets the correct webpack config in production`, () => {
const FRAMEWORK_BUNDLES = [`react`, `react-dom`, `scheduler`, `prop-types`]
const getConfig = jest.fn(() => {
return {
optimization: {
Expand All @@ -65,14 +76,20 @@ describe(`gatsby-plugin-preact`, () => {
framework: {
chunks: `all`,
name: `framework`,
// This regex ignores nested copies of framework libraries so they're bundled with their issuer.
test: new RegExp(
`(?<!node_modules.*)[\\\\/]node_modules[\\\\/](${FRAMEWORK_BUNDLES.join(
`|`
)})[\\\\/]`
),
// Mirrors what we have in gatsby/../webpack.config.js
test: module => {
if (
module?.rawRequest === `react-dom/server` ||
module?.rawRequest?.includes(`/react-dom-server`)
) {
return false
}

return FRAMEWORK_BUNDLES_REGEX_GATSBY.test(
module.nameForCondition()
)
},
priority: 40,
// Don't let webpack eliminate this chunk (prevents this chunk from becoming a part of the commons chunk)
enforce: true,
},
},
Expand Down Expand Up @@ -121,7 +138,7 @@ describe(`gatsby-plugin-preact`, () => {
"enforce": true,
"name": "framework",
"priority": 40,
"test": [Function],
"test": /\\(\\?<!node_modules\\.\\*\\)\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]\\(preact\\|react\\|react-dom\\|scheduler\\|prop-types\\)\\[\\\\\\\\/\\]/,
},
"vendors": false,
},
Expand All @@ -141,7 +158,7 @@ describe(`gatsby-plugin-preact`, () => {
"enforce": true,
"name": "framework",
"priority": 40,
"test": /\\(\\?<!node_modules\\.\\*\\)\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]\\(react\\|react-dom\\|scheduler\\|prop-types\\)\\[\\\\\\\\/\\]/,
"test": [Function],
},
"vendors": false,
},
Expand Down
23 changes: 16 additions & 7 deletions packages/gatsby-plugin-preact/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
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
Expand Down Expand Up @@ -45,15 +60,9 @@ export function onCreateWebpackConfig({ stage, actions, getConfig }) {
if (
webpackConfig?.optimization?.splitChunks?.cacheGroups?.framework?.test
) {
const frameworkRegex =
webpackConfig.optimization.splitChunks.cacheGroups.framework.test

// replace react libs with preact
webpackConfig.optimization.splitChunks.cacheGroups.framework.test =
module =>
/(?<!node_modules.*)[\\/]node_modules[\\/](preact)[\\/]/.test(
module.resource
) || frameworkRegex.test(module.resource)
FRAMEWORK_BUNDLES_REGEX_PREACT
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ module.exports = async (
framework: {
chunks: `all`,
name: `framework`,
// Important: If you change something here, also update "gatsby-plugin-preact"
test: module => {
// Packages like gatsby-plugin-image might import from "react-dom/server". We don't want to include react-dom-server in the framework bundle.
// A rawRequest might look like these:
Expand Down

0 comments on commit b5978a6

Please sign in to comment.