Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\\)\\[\\\\\\\\/\\]/,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this was the function:

module =>
          /(?<!node_modules.*)[\\/]node_modules[\\/](preact)[\\/]/.test(
            module.resource
          )

},
"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],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this was the regex in the mock above

},
"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`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main change: Adding preact to this regex and then just using this regex instead of trying to reuse what Gatsby framework itself uses

`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