/
next.config.js
61 lines (52 loc) · 1.45 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
'use strict';
const assert = require('assert');
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: Boolean(process.env.ANALYZE),
});
const svgoConfig = {
floatPrecision: 2,
multipass: true,
plugins: [
{
convertPathData: {
makeArcs: false,
},
},
{ removeViewBox: false },
{ sortAttrs: true },
],
};
module.exports = withBundleAnalyzer({
webpack(config, { isServer }) {
addCodeMirrorAlias(config, isServer);
addReactSvgRule(config);
return config;
},
});
function addCodeMirrorAlias(config, isServer) {
if (isServer) {
const runmodeNodePath = require.resolve('codemirror/addon/runmode/runmode.node');
config.resolve.alias[require.resolve('codemirror/lib/codemirror')] = runmodeNodePath;
config.resolve.alias[require.resolve('codemirror/addon/runmode/runmode')] = runmodeNodePath;
}
}
const moduleTest = String.raw`/\.(tsx|ts|js|mjs|jsx)$/`;
function addReactSvgRule(config) {
const moduleRule = config.module.rules.find((rule) => String(rule.test) === moduleTest);
assert.notStrictEqual(
moduleRule,
undefined,
'Could not find the main module rule (check the regular expression)',
);
const moduleLoaders = Array.isArray(moduleRule.use) ? moduleRule.use : [moduleRule.use];
config.module.rules.push({
test: /\.react\.svg$/,
use: [
...moduleLoaders,
{
loader: '@svgr/webpack',
options: { svgoConfig },
},
],
});
}