-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.config.js
142 lines (130 loc) · 4.09 KB
/
next.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @type {import('next/dist/next-server/server/config-shared').NextConfig}
* */
const withPrefresh = require('@prefresh/next');
const preact = require('preact');
const path = require('path');
const LINARIA_EXTENSION = '.linaria.module.css';
const GLOBAL_CSS_EXTENSIOM = '.css';
const config = withPrefresh({
i18n: {
locales: ['en-US', 'ja-JP'],
defaultLocale: 'en-US',
},
webpack(config, options) {
const { dev, isServer, defaultLoaders } = options;
// For styles with Linaria
traverse(config.module.rules);
config.module.rules.push({
test: /(?!_app)\.(tsx|ts)$/,
exclude: /node_modules/,
use: [
defaultLoaders.babel,
{
loader: '@linaria/webpack-loader',
options: {
sourceMap: process.env.NODE_ENV !== 'production',
extension: LINARIA_EXTENSION,
},
},
],
});
config.module.rules.push({
test: /_app\.(tsx|ts)$/,
exclude: /node_modules/,
use: [
defaultLoaders.babel,
{
loader: '@linaria/webpack-loader',
options: {
sourceMap: process.env.NODE_ENV !== 'production',
extension: GLOBAL_CSS_EXTENSIOM,
},
},
],
});
config.module.rules.push({
test: /\.(svg)$/,
exclude: /node_modules/,
loader: 'svg-react-loader',
});
// 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';
}
}
// Install webpack aliases:
const aliases = config.resolve.alias || (config.resolve.alias = {});
aliases.react = aliases['react-dom'] = 'preact/compat';
aliases['react-ssr-prepass'] = 'preact-ssr-prepass';
aliases['@'] = path.join(__dirname, './');
// Automatically inject Preact DevTools
if (dev) {
// inject Preact DevTools
const entry = config.entry;
config.entry = () =>
entry().then((entries) => {
entries['main.js'] = ['preact/debug'].concat(
entries['main.js'] || []
);
return entries;
});
}
if (dev && isServer) {
// Remove circular `__self` and `__source` props only meant for
// development. See https://github.com/developit/nextjs-preact-demo/issues/25
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);
}
};
}
return config;
},
//ref: https://github.com/preactjs/next-plugin-preact/blob/master/packages/next-plugin-preact/index.js
});
module.exports = config;
const traverse = (rules) => {
for (let rule of rules) {
if (typeof rule.loader === 'string' && rule.loader.includes('css-loader')) {
if (
rule.options &&
rule.options.modules &&
typeof rule.options.modules.getLocalIdent === 'function'
) {
let nextGetLocalIdent = rule.options.modules.getLocalIdent;
rule.options.modules.getLocalIdent = (
context,
_,
exportName,
options
) => {
if (context.resourcePath.includes(LINARIA_EXTENSION)) {
return exportName;
}
return nextGetLocalIdent(context, _, exportName, options);
};
}
}
if (typeof rule.use === 'object') {
traverse(Array.isArray(rule.use) ? rule.use : [rule.use]);
}
if (Array.isArray(rule.oneOf)) {
traverse(rule.oneOf);
}
}
};