-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Examples] using-preact: add Prefresh, DevTools & chunking fix (#13976)
This adds [prefresh](https://github.com/JoviDeCroock/prefresh) and [preact/debug + DevTools](https://preactjs.com/guide/v10/debugging/) during development. It also fixes the chunking/splitting configuration for production builds by creating a `preact` chunk based on the existing framework chunk. I'm still considering publishing the config here as a plugin, since I worry about the copy-paste effect on this much regex-foo. /cc @JoviDeCroock
- Loading branch information
Showing
2 changed files
with
43 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const withPrefresh = require('@prefresh/next') | ||
|
||
module.exports = withPrefresh({ | ||
webpack(config, { dev, isServer }) { | ||
// 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 }) | ||
cacheGroups.commons.name = 'framework' | ||
} else { | ||
cacheGroups.preact = { name: 'commons', chunks: 'all', test } | ||
} | ||
} | ||
|
||
// Install webpack aliases: | ||
const aliases = config.resolve.alias || (config.resolve.alias = {}) | ||
aliases.react = aliases['react-dom'] = 'preact/compat' | ||
|
||
// Automatically inject Preact DevTools: | ||
if (dev && !isServer) { | ||
const entry = config.entry | ||
config.entry = () => | ||
entry().then((entries) => { | ||
entries['main.js'] = ['preact/debug'].concat(entries['main.js'] || []) | ||
return entries | ||
}) | ||
} | ||
|
||
return config | ||
}, | ||
}) |
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