Skip to content

Commit

Permalink
feat(gatsby-plugin-preact): enable preact in development (#22441)
Browse files Browse the repository at this point in the history
Co-authored-by: Ward Peeters <ward@coding-tech.com>
  • Loading branch information
teodragovic and wardpeet authored Apr 16, 2020
1 parent 0558443 commit ca19076
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 35 deletions.
6 changes: 0 additions & 6 deletions packages/gatsby-plugin-preact/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,3 @@ React.
// In your gatsby-config.js
plugins: [`gatsby-plugin-preact`]
```

## Usage in a development environment

Gatsby development server currently has a hardcoded dependency on React-dom, therefore this plugin does not enable Preact in development.

While Preact is designed to be a drop-in replacement, you should check that your production build works as expected before putting it live.
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"main": "index.js",
"peerDependencies": {
"gatsby": "^2.0.0",
"preact": "^10.0.0"
"preact": "^10.3.4"
},
"repository": {
"type": "git",
Expand Down
8 changes: 0 additions & 8 deletions packages/gatsby-plugin-preact/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,4 @@ describe(`gatsby-plugin-preact`, () => {
},
})
})

it(`does not invoke setWebpackConfig when stage is develop`, () => {
const actions = { setWebpackConfig: jest.fn() }

onCreateWebpackConfig({ stage: `develop`, actions })

expect(actions.setWebpackConfig).not.toHaveBeenCalled()
})
})
12 changes: 6 additions & 6 deletions packages/gatsby-plugin-preact/src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO enable preact/debug again when preact devtools is fixed.
// exports.onClientEntry = () => {
// if (process.env.NODE_ENV !== `production`) {
// require(`preact/debug`)
// }
// }
exports.onClientEntry = () => {
if (process.env.NODE_ENV !== `production`) {
console.log(`[HMR] disabled: preact is not compatible with RHL`)
require(`preact/debug`)
}
}
34 changes: 20 additions & 14 deletions packages/gatsby-plugin-preact/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
exports.onCreateWebpackConfig = ({ stage, actions }) => {
// Requiring the server version of React-dom is hardcoded right now
// in the development server. So we'll just avoid loading Preact there
// for now.
if (stage !== `develop`) {
actions.setWebpackConfig({
resolve: {
alias: {
react: `preact/compat`,
"react-dom": `preact/compat`,
"react-dom/server": `preact/compat`,
},
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.
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.
actions.setWebpackConfig({
resolve: {
alias: {
react: `preact/compat`,
"react-dom": `preact/compat`,
"react-dom/server": `preact/compat`,
},
})
}
},
})
}

0 comments on commit ca19076

Please sign in to comment.