Skip to content

Commit

Permalink
feat(gatsby-plugin-preact): enable prefresh (gatsbyjs#24153)
Browse files Browse the repository at this point in the history
Enable prefresh (react-refresh) for gatsby-plugin-preact. It fixes the open issue of not having hot reloading with preact.
  • Loading branch information
wardpeet committed May 20, 2020
1 parent 8f9acb1 commit 925610b
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-preact/README.md
Expand Up @@ -10,7 +10,7 @@ React.
## Install

`npm install --save gatsby-plugin-preact preact`
`npm install --save gatsby-plugin-preact preact preact-render-to-string`

## How to use

Expand Down
7 changes: 5 additions & 2 deletions packages/gatsby-plugin-preact/package.json
Expand Up @@ -7,11 +7,13 @@
"url": "https://github.com/gatsbyjs/gatsby/issues"
},
"dependencies": {
"@babel/runtime": "^7.9.6"
"@prefresh/webpack": "^0.6.0",
"react-refresh": "^0.8.2"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.6",
"@pmmmwh/react-refresh-webpack-plugin": "^0.3.1",
"babel-preset-gatsby-package": "^0.4.2",
"cross-env": "^5.2.1"
},
Expand All @@ -25,7 +27,8 @@
"main": "index.js",
"peerDependencies": {
"gatsby": "^2.0.0",
"preact": "^10.3.4"
"preact": "^10.3.4",
"preact-render-to-string": "^5.1.8"
},
"repository": {
"type": "git",
Expand Down
68 changes: 64 additions & 4 deletions packages/gatsby-plugin-preact/src/__tests__/gatsby-node.js
@@ -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)
})
})
1 change: 0 additions & 1 deletion packages/gatsby-plugin-preact/src/gatsby-browser.js
@@ -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`)
}
}
32 changes: 23 additions & 9 deletions packages/gatsby-plugin-preact/src/gatsby-node.js
@@ -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,
})
}
23 changes: 23 additions & 0 deletions yarn.lock
Expand Up @@ -3755,6 +3755,24 @@
native-url "^0.2.6"
schema-utils "^2.6.5"

"@prefresh/core@0.6.0":
version "0.6.0"
resolved "https://registry.yarnpkg.com/@prefresh/core/-/core-0.6.0.tgz#b7801ad3d16de9d21ed0824db6c85707f93cf93c"
integrity sha512-xhhBgQeMmyoSYEr8F27oypxRTWysIKPnc+Ed6U5OmEwPtLeKsccI3UCpSQyrcv5/HmPNPjAYHPqNC0uH57rIkw==

"@prefresh/utils@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@prefresh/utils/-/utils-0.1.0.tgz#9b69c9ecd6002e72e096e5e72f4bd4f88d15284c"
integrity sha512-xtUsQMJIHwBuiXu1E1iiXNVfn60N6zpVUo67ZvQVA6OMlslQzWtbdnTXRT+iplBBgfLmQvXR0bKsNz1+ZYSMgw==

"@prefresh/webpack@^0.6.0":
version "0.6.0"
resolved "https://registry.yarnpkg.com/@prefresh/webpack/-/webpack-0.6.0.tgz#fec281c8b94e5455646686eb4c123a7b22004dd3"
integrity sha512-z9S1jknJZsc9xmMsX/3nNae8CxKavlOH0YydDjdld41WYB4A2UkC7+rl4P+XH7www9HpGcXzmqgDfFKGGJnNEg==
dependencies:
"@prefresh/core" "0.6.0"
"@prefresh/utils" "0.1.0"

"@reach/router@^1.3.3":
version "1.3.3"
resolved "https://registry.yarnpkg.com/@reach/router/-/router-1.3.3.tgz#58162860dce6c9449d49be86b0561b5ef46d80db"
Expand Down Expand Up @@ -20050,6 +20068,11 @@ react-refresh@^0.7.0:
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.7.2.tgz#f30978d21eb8cac6e2f2fde056a7d04f6844dd50"
integrity sha512-u5l7fhAJXecWUJzVxzMRU2Zvw8m4QmDNHlTrT5uo3KBlYBhmChd7syAakBoay1yIiVhx/8Fi7a6v6kQZfsw81Q==

react-refresh@^0.8.2:
version "0.8.2"
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.8.2.tgz#24bb0858eac92b0d7b0dd561747f0c9fd6c60327"
integrity sha512-n8GXxo3DwM2KtFEL69DAVhGc4A1THn2qjmfvSo3nze0NLCoPbywazeJPqdp0RdSGLmyhQzeyA+XPXOobbYlkzg==

react-side-effect@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/react-side-effect/-/react-side-effect-1.2.0.tgz#0e940c78faba0c73b9b0eba9cd3dda8dfb7e7dae"
Expand Down

0 comments on commit 925610b

Please sign in to comment.