-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
124 lines (112 loc) · 3.45 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
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
const cssLoaderConfig = require('@zeit/next-css/css-loader-config')
const FilterWarningsPlugin = require('webpack-filter-warnings-plugin')
module.exports = (nextConfig = {}) => {
return Object.assign({}, nextConfig, {
webpack(config, options) {
config.plugins.push(new FilterWarningsPlugin({
exclude: /mini-css-extract-plugin[^]*Conflicting order between:/,
}))
const { dev, isServer } = options
const {
cssModules,
cssLoaderOptions,
postcssLoaderOptions,
lessLoaderOptions = {},
cssModulesExclude = [],
} = nextConfig
// default css loaders with cssModules
options.defaultLoaders.css = cssLoaderConfig(config, {
extensions: ['css'],
cssModules,
cssLoaderOptions,
postcssLoaderOptions,
dev,
isServer,
})
// default less loaders with cssModules
options.defaultLoaders.less = cssLoaderConfig(config, {
extensions: ['less'],
cssModules,
cssLoaderOptions,
postcssLoaderOptions,
dev,
isServer,
loaders: [
{
loader: 'less-loader',
options: lessLoaderOptions,
},
],
})
// if excluding cssModule is enabled
if (cssModulesExclude.length) {
// we need to disable assets/node_modules's cssModules for css loader
const globlaCssWithoutCssModuleLoader = cssLoaderConfig(config, {
extensions: ['css'],
cssModules: false,
cssLoaderOptions: {},
postcssLoaderOptions,
dev,
isServer,
})
// we need to disable assets/node_modules's cssModules for less loader
const globalLessWithoutCssModuleLoader = cssLoaderConfig(config, {
extensions: ['less'],
cssModules: false,
cssLoaderOptions: {},
postcssLoaderOptions,
dev,
isServer,
loaders: [
{
loader: 'less-loader',
options: lessLoaderOptions,
},
],
})
// we push that loader rule into css rule
config.module.rules.push({
test: /\.css$/,
issuer(issuer) {
if (issuer.match(/pages[\\/]_document\.js$/)) {
throw new Error(
'You can not import CSS files in pages/_document.js, use pages/_app.js instead.'
)
}
return true
},
include: cssModulesExclude,
use: globlaCssWithoutCssModuleLoader,
})
// we push that loader rule into less rule
config.module.rules.push({
test: /\.less$/,
include: cssModulesExclude,
use: globalLessWithoutCssModuleLoader,
})
}
config.module.rules.push({
test: /\.css$/,
issuer(issuer) {
if (issuer.match(/pages[\\/]_document\.js$/)) {
throw new Error(
'You can not import CSS files in pages/_document.js, use pages/_app.js instead.'
)
}
return true
},
exclude: cssModulesExclude,
use: options.defaultLoaders.css,
})
config.module.rules.push({
test: /\.less$/,
exclude: cssModulesExclude,
use: options.defaultLoaders.less,
})
if (typeof nextConfig.webpack === 'function') {
return nextConfig.webpack(config, options)
}
return config
},
})
}