Unverified Commit 6ea504a6 authored by Vitor Dino's avatar Vitor Dino Committed by GitHub

feat(examples): use next-plugin-preact on using-preact example (#18588)

hey there 👋 

stumbled on the [`using-preact` example](https://github.com/vercel/next.js/tree/canary/examples/using-preact) first when i tried to port my project, just saw that the [config wasn’t that trivial](https://github.com/vercel/next.js/issues/13969#issuecomment-641540051), and it would fit better on a [plugin](https://github.com/preactjs/next-plugin-preact)..

I did this migration first on my project (it worked super well) and realized that people also would dislike having to maintain that kind of code, and would stumble faster on the plugin if it was already on the official example ✨  
parent 6fbe5b43
...@@ -13,3 +13,11 @@ yarn create next-app --example using-preact using-preact-app ...@@ -13,3 +13,11 @@ yarn create next-app --example using-preact using-preact-app
``` ```
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
### Aliasing react -> preact
Using `preact` with `next.js` depends on aliasing react packages to preact ones (like `@preact/compat`). This is done directly in the [`package.json`](./package.json) dependencies.
### next-plugin-preact
This example was [updated](https://github.com/vercel/next.js/pull/18588) to use [`next-plugin-preact`](https://github.com/preactjs/next-plugin-preact) and now requires minimal configuration, if you want to add preact without a plugin, or see how it works, head to the previous [`next.config.js`](https://github.com/vercel/next.js/blob/629884af7d3ced97b8c2ec7aebdfb1a3a5d808f0/examples/using-preact/next.config.js).
const preact = require('preact') const withPreact = require('next-plugin-preact')
const withPrefresh = require('@prefresh/next')
module.exports = withPrefresh({ module.exports = withPreact({
webpack(config, { dev, isServer }) { /* regular next.js config options here */
// Move Preact into the framework chunk instead of duplicating in routes:
const splitChunks = config.optimization && config.optimization.splitChunks
if (splitChunks) {
const cacheGroups = splitChunks.cacheGroups
const test = /[\\/]node_modules[\\/](preact|preact-render-to-string|preact-context-provider)[\\/]/
if (cacheGroups.framework) {
cacheGroups.preact = Object.assign({}, cacheGroups.framework, { test })
// if you want to merge the 2 small commons+framework chunks:
// cacheGroups.commons.name = 'framework';
}
}
if (isServer) {
// mark `preact` stuffs as external for server bundle to prevent duplicate copies of preact
config.externals.push(
/^(preact|preact-render-to-string|preact-context-provider)([\\/]|$)/
)
}
// Install webpack aliases:
const aliases = config.resolve.alias || (config.resolve.alias = {})
aliases.react = aliases['react-dom'] = 'preact/compat'
if (dev) {
if (isServer) {
// Remove circular `__self` and `__source` props only meant for development
let oldVNodeHook = preact.options.vnode
preact.options.vnode = (vnode) => {
const props = vnode.props
if (props != null) {
if ('__self' in props) props.__self = null
if ('__source' in props) props.__source = null
}
if (oldVNodeHook) {
oldVNodeHook(vnode)
}
}
} else {
// Automatically inject Preact DevTools:
const entry = config.entry
config.entry = () =>
entry().then((entries) => {
entries['main.js'] = ['preact/debug'].concat(
entries['main.js'] || []
)
return entries
})
}
}
return config
},
}) })
...@@ -9,9 +9,10 @@ ...@@ -9,9 +9,10 @@
"devDependencies": {}, "devDependencies": {},
"dependencies": { "dependencies": {
"@prefresh/next": "^1.3.0", "@prefresh/next": "^1.3.0",
"next": "10.0.0", "next": "^10.0.0",
"preact": "^10.5.4", "next-plugin-preact": "^3.0.3",
"preact-render-to-string": "^5.1.10", "preact": "^10.5.5",
"preact-render-to-string": "^5.1.11",
"react": "npm:@preact/compat@^0.0.3", "react": "npm:@preact/compat@^0.0.3",
"react-dom": "npm:@preact/compat@^0.0.3", "react-dom": "npm:@preact/compat@^0.0.3",
"react-ssr-prepass": "npm:preact-ssr-prepass@^1.1.2" "react-ssr-prepass": "npm:preact-ssr-prepass@^1.1.2"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment