forked from gatsbyjs/gatsby
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-plugin-preact): enable prefresh (gatsbyjs#24153)
Enable prefresh (react-refresh) for gatsby-plugin-preact. It fixes the open issue of not having hot reloading with preact.
- Loading branch information
Showing
6 changed files
with
116 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 64 additions & 4 deletions
68
packages/gatsby-plugin-preact/src/__tests__/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,80 @@ | ||
const { onCreateWebpackConfig } = require(`../gatsby-node`) | ||
const PreactRefreshPlugin = require(`@prefresh/webpack`) | ||
const ReactRefreshWebpackPlugin = require(`@pmmmwh/react-refresh-webpack-plugin`) | ||
|
||
describe(`gatsby-plugin-preact`, () => { | ||
it(`sets the correct webpack config`, () => { | ||
const actions = { setWebpackConfig: jest.fn() } | ||
it(`sets the correct webpack config in development`, () => { | ||
const getConfig = jest.fn(() => { | ||
return { | ||
plugins: [new ReactRefreshWebpackPlugin()], | ||
} | ||
}) | ||
const actions = { | ||
setWebpackConfig: jest.fn(), | ||
setBabelPlugin: jest.fn(), | ||
replaceWebpackConfig: jest.fn(), | ||
} | ||
|
||
onCreateWebpackConfig({ stage: `build-javascript`, actions }) | ||
onCreateWebpackConfig({ stage: `develop`, actions, getConfig }) | ||
|
||
expect(actions.setWebpackConfig).toHaveBeenCalledTimes(1) | ||
expect(actions.setWebpackConfig).toHaveBeenCalledWith({ | ||
plugins: [new PreactRefreshPlugin()], | ||
resolve: { | ||
alias: { | ||
react: `preact/compat`, | ||
"react-dom": `preact/compat`, | ||
"react-dom/server": `preact/compat`, | ||
}, | ||
}, | ||
}) | ||
|
||
expect(getConfig).toHaveBeenCalledTimes(1) | ||
expect(actions.setBabelPlugin).toHaveBeenCalledTimes(1) | ||
expect(actions.setBabelPlugin).toHaveBeenCalledWith({ | ||
name: `react-refresh/babel`, | ||
}) | ||
expect(actions.replaceWebpackConfig).toMatchInlineSnapshot(` | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [ | ||
Object { | ||
"plugins": Array [], | ||
}, | ||
], | ||
], | ||
"results": Array [ | ||
Object { | ||
"type": "return", | ||
"value": undefined, | ||
}, | ||
], | ||
} | ||
`) | ||
}) | ||
|
||
it(`sets the correct webpack config in production`, () => { | ||
const getConfig = jest.fn() | ||
const actions = { | ||
setWebpackConfig: jest.fn(), | ||
setBabelPlugin: jest.fn(), | ||
replaceWebpackConfig: jest.fn(), | ||
} | ||
|
||
onCreateWebpackConfig({ stage: `build-javascript`, actions, getConfig }) | ||
|
||
expect(actions.setWebpackConfig).toHaveBeenCalledTimes(1) | ||
expect(actions.setWebpackConfig).toHaveBeenCalledWith({ | ||
plugins: [], | ||
resolve: { | ||
alias: { | ||
react: `preact/compat`, | ||
"react-dom": `preact/compat`, | ||
}, | ||
}, | ||
}) | ||
|
||
expect(getConfig).toHaveBeenCalledTimes(0) | ||
expect(actions.replaceWebpackConfig).toHaveBeenCalledTimes(0) | ||
expect(actions.setBabelPlugin).toHaveBeenCalledTimes(0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
exports.onClientEntry = () => { | ||
if (process.env.NODE_ENV !== `production`) { | ||
console.log(`[HMR] disabled: preact is not compatible with RHL`) | ||
require(`preact/debug`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,36 @@ | ||
const PreactRefreshPlugin = require(`@prefresh/webpack`) | ||
|
||
exports.onPreInit = () => { | ||
// Setting this variable replaces react-hot-loader with | ||
// [fast-refresh](https://reactnative.dev/docs/next/fast-refresh) | ||
// and resolves conflicts with running Preact in development. | ||
// force fast-refresh in gatsby | ||
process.env.GATSBY_HOT_LOADER = `fast-refresh` | ||
} | ||
|
||
exports.onCreateWebpackConfig = ({ actions }) => { | ||
// React-dom is hardcoded as part of react-hot-loader | ||
// in the development server. So we either avoid Preact | ||
// during development or switch to fast-refresh and loose | ||
// hot reloading capabilities. | ||
exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => { | ||
const webpackPlugins = [] | ||
if (stage === `develop`) { | ||
webpackPlugins.push(new PreactRefreshPlugin()) | ||
|
||
// remove React refresh plugin, we want to add preact refresh instead. | ||
const webpackConfig = getConfig() | ||
webpackConfig.plugins = webpackConfig.plugins.filter( | ||
plugin => plugin.constructor.name !== `ReactRefreshPlugin` | ||
) | ||
actions.replaceWebpackConfig(webpackConfig) | ||
|
||
// enable react-refresh babel plugin to enable hooks | ||
// @see https://github.com/JoviDeCroock/prefresh/tree/master/packages/webpack#using-hooks | ||
actions.setBabelPlugin({ | ||
name: `react-refresh/babel`, | ||
}) | ||
} | ||
|
||
actions.setWebpackConfig({ | ||
resolve: { | ||
alias: { | ||
react: `preact/compat`, | ||
"react-dom": `preact/compat`, | ||
"react-dom/server": `preact/compat`, | ||
}, | ||
}, | ||
plugins: webpackPlugins, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters