-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext.config.js
82 lines (78 loc) · 2.3 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
const path = require('path');
const withBundleAnalyzer = require('@next/bundle-analyzer');
const compose = require('./next.compose-plugin');
const REGEXP_ANT_STYLES = /antd\/.*?\/style.*?/i;
const REGEXP_LESS = /(styles|pages|components|assets|node_modules)\/.*\.less$/i;
/**
* Extrat built-in CSS loader rules and apply them to less files, too
*/
function extractCSSRules(allRules) {
const rules = allRules.find((x) => !!x.oneOf).oneOf;
const moduleCssRules = rules.filter((x) => String(x.test).includes('/\\.module\\.css$/'));
const cssRules = rules.filter((x) => String(x.test).includes('/(?<!\\.module)\\.css$/'));
return [
...moduleCssRules.map((x) => ({
...x,
test: /\.module\.less$/,
})),
// must manually import antd styles in global.css
{
test: REGEXP_ANT_STYLES,
use: 'null-loader',
},
...cssRules.map((x) => ({
...x,
test: REGEXP_LESS,
})),
];
}
module.exports = compose([
{
webpack: (config, { isServer, defaultLoaders }) => {
// eslint-disable-next-line no-param-reassign
defaultLoaders.less = {
loader: 'less-loader',
options: {
lessOptions: {
javascriptEnabled: true,
},
},
};
config.module.rules.push({
test: REGEXP_LESS,
oneOf: extractCSSRules(config.module.rules),
});
config.module.rules.push({
test: REGEXP_LESS,
use: defaultLoaders.less,
});
if (isServer) {
const origExternals = [...config.externals];
// eslint-disable-next-line no-param-reassign
config.externals = [
// make sure antd styles are collected to build bundles
(context, request, callback) => {
if (request.match(REGEXP_ANT_STYLES)) {
callback();
return;
}
if (typeof origExternals[0] === 'function') {
origExternals[0](context, request, callback);
} else {
callback();
}
},
...(typeof origExternals[0] === 'function' ? [] : origExternals),
];
}
config.resolve.modules = [path.resolve(__dirname, 'src'), "node_modules"]
return config;
},
},
[
withBundleAnalyzer,
{
enabled: process.env.ANALYZE === 'true',
},
],
]);