-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss-loader-config.js
100 lines (89 loc) · 2.44 KB
/
css-loader-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
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const findUp = require('find-up')
const fileExtensions = new Set()
let extractCssInitialized = false
module.exports = (
config,
{
extensions = [],
cssModules = false,
cssLoaderOptions = {},
dev,
isServer,
postcssLoaderOptions = {},
loaders = []
}
) => {
// We have to keep a list of extensions for the splitchunk config
for (const extension of extensions) {
fileExtensions.add(extension)
}
if (!isServer) {
config.optimization.splitChunks.cacheGroups.styles = {
name: 'styles',
test: new RegExp(`\\.+(${[...fileExtensions].join('|')})$`),
chunks: 'all',
enforce: true
}
}
if (!isServer && !extractCssInitialized) {
config.plugins.push(
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: dev
? 'static/css/[name].css'
: 'static/css/[name].[contenthash:8].css',
chunkFilename: dev
? 'static/css/[name].chunk.css'
: 'static/css/[name].[contenthash:8].chunk.css'
})
)
extractCssInitialized = true
}
const postcssConfig = findUp.sync('postcss.config.js', {
cwd: config.context
})
let postcssLoader
if (postcssConfig) {
// Copy the postcss-loader config options first.
const postcssOptionsConfig = Object.assign(
{},
postcssLoaderOptions.config,
{ path: postcssConfig }
)
postcssLoader = {
loader: 'postcss-loader',
options: Object.assign({}, postcssLoaderOptions, {
config: postcssOptionsConfig
})
}
}
const cssLoader = {
loader: isServer ? 'css-loader/locals' : 'css-loader',
options: Object.assign(
{},
{
modules: cssModules,
sourceMap: dev,
importLoaders: loaders.length + (postcssLoader ? 1 : 0)
},
cssLoaderOptions
)
}
// When not using css modules we don't transpile on the server
if (isServer && !cssLoader.options.modules) {
return ['ignore-loader']
}
// When on the server and using css modules we transpile the css
if (isServer && cssLoader.options.modules) {
return [cssLoader, postcssLoader, ...loaders].filter(Boolean)
}
return [
!isServer && dev && 'extracted-loader',
!isServer && MiniCssExtractPlugin.loader,
cssLoader,
postcssLoader,
...loaders
].filter(Boolean)
}