-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathwebpack-css.ts
106 lines (103 loc) · 2.28 KB
/
webpack-css.ts
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
import path from 'path'
import libsass from 'sass'
import cssnano from 'cssnano'
import autoprefixer from 'autoprefixer'
import hexrgba from 'postcss-hexrgba'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import RemoveEmptyScriptsPlugin from 'webpack-remove-empty-scripts'
import { glob } from 'glob'
import { RtlCssPlugin } from './RtlCssPlugin'
import type { Configuration, EntryObject } from 'webpack'
import type { Config as PostCssConfig } from 'postcss-load-config'
const postcssOptions: PostCssConfig = {
plugins: [
hexrgba(),
autoprefixer(),
cssnano({
preset: ['default', { discardComments: { removeAll: true } }]
})
]
}
const entriesFromFiles = (patterns: string | string[], entry: (filename: string) => string): EntryObject =>
Object.fromEntries(
glob.sync(patterns)
.map(filename => [entry(filename), `./${filename}`])
)
export const cssWebpackConfig: Configuration = {
entry: {
...entriesFromFiles(
['src/css/*.scss', '!src/css/**/_*.scss'],
filename => `${path.parse(filename).name}-css`
),
...entriesFromFiles(
'node_modules/codemirror/theme/*.css',
filename => `codemirror-theme-${path.parse(filename).name}`
)
},
module: {
rules: [
{
test: /\.scss$/,
exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 2
}
},
{
loader: 'postcss-loader',
options: {
postcssOptions
}
},
{
loader: 'sass-loader',
options: {
implementation: libsass,
sourceMap: true
}
}
]
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: false,
importLoaders: 1
}
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [cssnano()]
}
}
}
]
}
]
},
plugins: [
new RemoveEmptyScriptsPlugin(),
new MiniCssExtractPlugin({
filename: ({ chunk }) =>
chunk?.name
? `${chunk.name}.css`
.replace(/^codemirror-theme-/, 'editor-themes/')
.replace(/-css\.css$/, '.css')
: '[name].css'
}),
new RtlCssPlugin({
entries: new Set(['manage-css', 'edit-css'])
})
]
}