-
Notifications
You must be signed in to change notification settings - Fork 10.3k
fix(gatsby-plugin-preact): Adjust framework chunk overwrite #37658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(() => { | ||
|
|
@@ -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: { | ||
|
|
@@ -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, | ||
| }, | ||
| }, | ||
|
|
@@ -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, | ||
| }, | ||
|
|
@@ -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], | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously this was the regex in the mock above |
||
| }, | ||
| "vendors": false, | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,20 @@ | ||
| const PreactRefreshPlugin = require(`@prefresh/webpack`) | ||
|
|
||
| const FRAMEWORK_BUNDLES_PREACT = [ | ||
| `preact`, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Main change: Adding |
||
| `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 | ||
|
|
@@ -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 | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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: