-
Notifications
You must be signed in to change notification settings - Fork 2
/
css-loader-config.js
116 lines (103 loc) · 2.9 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const ExtractCssChunks = require('extract-css-chunks-webpack-plugin')
const findUp = require('find-up')
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
const fileExtensions = new Set()
let extractCssInitialized = false
module.exports = (
config,
{
extensions = [],
cssModules = false,
cssLoaderOptions = {},
dev,
isServer,
postcssLoaderOptions = {},
loaders = [],
tsCssModules
}
) => {
// 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 ExtractCssChunks({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: dev
? 'static/chunks/[name].css'
: 'static/chunks/[name].[contenthash:8].css',
chunkFilename: dev
? 'static/chunks/[name].chunk.css'
: 'static/chunks/[name].[contenthash:8].chunk.css',
hot: dev
})
)
extractCssInitialized = true
}
if (!dev) {
if (!Array.isArray(config.optimization.minimizer)) {
config.optimization.minimizer = []
}
config.optimization.minimizer.push(
new OptimizeCssAssetsWebpackPlugin({
cssProcessorOptions: {
discardComments: { removeAll: true }
}
})
)
}
const postcssConfigPath = findUp.sync('postcss.config.js', {
cwd: config.context
})
let postcssLoader
if (postcssConfigPath) {
// Copy the postcss-loader config options first.
const postcssOptionsConfig = Object.assign(
{},
postcssLoaderOptions.config,
{ path: postcssConfigPath }
)
postcssLoader = {
loader: 'postcss-loader',
options: Object.assign({}, postcssLoaderOptions, {
config: postcssOptionsConfig
})
}
}
const cssLoader = {
loader: isServer ? 'css-loader/locals' : (tsCssModules ? 'typings-for-css-modules-loader' : 'css-loader'),
options: Object.assign(
{},
{
modules: tsCssModules || 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 && ExtractCssChunks.loader,
cssLoader,
postcssLoader,
...loaders
].filter(Boolean)
}