Skip to content

Commit eef812d

Browse files
committed
feat: set up "preact" config
1 parent 84c0e93 commit eef812d

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

next.config.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
const computePath = require('./utils').computePath
2+
const withPrefresh = require('@prefresh/next')
23

3-
module.exports = {
4-
webpack(config, options) {
4+
const config = {
5+
experimental: {
6+
modern: true,
7+
polyfillsOptimization: true,
8+
},
9+
10+
webpack(config, { dev, isServer }) {
511
const esLintRule = {
612
test: /\.tsx$/,
713
enforce: 'pre',
814
exclude: ['/node_modules/', '/.next/'],
9-
loader: 'eslint-loader'
15+
loader: 'eslint-loader',
1016
}
1117
config.module.rules.push(esLintRule)
1218

@@ -22,6 +28,41 @@ module.exports = {
2228
utils: computePath('./src/utils')
2329
}
2430

31+
// Preact stuff
32+
const splitChunks = config.optimization && config.optimization.splitChunks
33+
if (splitChunks) {
34+
const cacheGroups = splitChunks.cacheGroups
35+
const preactModules = /[\\/]node_modules[\\/](preact|preact-render-to-string|preact-context-provider)[\\/]/
36+
if (cacheGroups.framework) {
37+
cacheGroups.preact = Object.assign({}, cacheGroups.framework, {
38+
test: preactModules,
39+
})
40+
cacheGroups.commons.name = 'framework'
41+
} else {
42+
cacheGroups.preact = {
43+
name: 'commons',
44+
chunks: 'all',
45+
test: preactModules,
46+
}
47+
}
48+
}
49+
50+
// Install webpack aliases:
51+
const aliases = config.resolve.alias || (config.resolve.alias = {})
52+
aliases.react = aliases['react-dom'] = 'preact/compat'
53+
54+
// inject Preact DevTools
55+
if (dev && !isServer) {
56+
const entry = config.entry
57+
config.entry = () =>
58+
entry().then(entries => {
59+
entries['main.js'] = ['preact/debug'].concat(entries['main.js'] || [])
60+
return entries
61+
})
62+
}
63+
2564
return config
26-
}
65+
},
2766
}
67+
68+
module.exports = withPrefresh(config)

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@emotion/babel-preset-css-prop": "11.0.0",
3030
"@emotion/react": "11.1.4",
3131
"@emotion/styled": "11.0.0",
32+
"@prefresh/next": "1.4.4",
3233
"@testing-library/jest-dom": "5.11.9",
3334
"@testing-library/react": "11.2.3",
3435
"@types/jest": "26.0.20",
@@ -62,9 +63,12 @@
6263
"lint-staged": "10.5.3",
6364
"next": "10.0.5",
6465
"nprogress": "0.2.0",
66+
"preact": "10.5.11",
67+
"preact-render-to-string": "5.1.12",
6568
"prettier": "2.2.1",
66-
"react": "17.0.1",
67-
"react-dom": "17.0.1",
69+
"react": "npm:@preact/compat@^0.0.3",
70+
"react-dom": "npm:@preact/compat@^0.0.3",
71+
"react-ssr-prepass": "npm:preact-ssr-prepass@^1.1.1",
6872
"react-transition-group": "4.4.1",
6973
"ts-jest": "26.4.4",
7074
"ts-node": "9.1.1",

0 commit comments

Comments
 (0)