-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
59 lines (56 loc) · 1.73 KB
/
index.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
const LINARIA_EXTENSION = '.linaria.module.css';
function 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.mode = 'local';
rule.options.modules.auto = true;
rule.options.modules.exportGlobals = true;
rule.options.modules.exportOnlyLocals = false;
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);
}
}
}
module.exports = (nextConfig = {}) => {
return {
...nextConfig,
webpack(config, options) {
traverse(config.module.rules);
config.module.rules.push({
test: /\.(tsx|ts|js|mjs|jsx)$/,
exclude: /node_modules/,
use: [
{
loader: require.resolve('@linaria/webpack-loader'),
options: {
sourceMap: process.env.NODE_ENV !== 'production',
...(nextConfig.linaria || {}),
extension: LINARIA_EXTENSION,
},
},
],
});
if (typeof nextConfig.webpack === 'function') {
return nextConfig.webpack(config, options);
}
return config;
},
};
};