This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 173
/
css.ts
139 lines (117 loc) · 3.33 KB
/
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Webpack (CSS style loading)
// ----------------------------------------------------------------------------
// IMPORTS
/* NPM */
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import { Loader } from "webpack";
// ----------------------------------------------------------------------------
// Types
type ModuleSettings = [string, { modules: boolean }];
export interface IPostCSSConfig {
exec?: boolean;
parser?: string | object;
syntax?: string | object;
plugins?: any[] | ((loader: any) => any[]);
sourceMap?: string | boolean;
}
export interface IStylesConfig {
postcss?: boolean | IPostCSSConfig;
}
export interface IRule {
ext: string;
use: Loader[];
}
// Returns string RegEx and modules settings based on style file extension
function getExtensionSettings(ext: string): ModuleSettings[] {
return [
[`^(?!.*\\.global\\.${ext}$).*\\.${ext}$`, { modules: true }],
[`\\.global\\.${ext}$`, { modules: false }]
];
}
// Rules configuration for each style file extension
export const rules: IRule[] = [
{
ext: "css",
use: []
},
{
ext: "s(c|a)ss",
use: [
{
loader: "resolve-url-loader"
},
{
loader: "sass-loader",
options: {
sourceMap: true
}
}
]
},
{
ext: "less",
use: ["less-loader"]
}
];
const isProduction = process.env.NODE_ENV === "production";
// Create generator to get rules
export default function* css(isClient = true) {
// Source maps depend on us being in development
const sourceMap = !isProduction;
for (const loader of rules) {
// Iterate over CSS/SASS/LESS and yield local and global mod configs
for (const [test, modules] of getExtensionSettings(loader.ext)) {
// Build the use rules
const use = [
// CSS hot loading on the client, in development
isClient && !isProduction && "css-hot-loader",
// Add MiniCSS if we're on the client
isClient && MiniCssExtractPlugin.loader,
// Set-up `css-loader`
{
// If we're on the server, we only want to output the name
// loader: isClient ? "css-loader" : "css-loader/locals",
loader: "css-loader",
options: {
// Calculate how many loaders follow this one
importLoaders: loader.use.length + 1,
// Format for 'localised' CSS modules
localIdentName: "[local]-[hash:base64]",
// Add sourcemaps if we're in dev
sourceMap,
// Specify modules options
...modules
}
},
// Add PostCSS
{
loader: "postcss-loader",
options: {
ident: "postcss",
plugins() {
return [
// TODO - MAKE PLUGINS WORK WITH SASS!
require("postcss-preset-env")({
features: {
autoprefixer: false
}
}),
require("cssnano")()
];
},
// Enable sourcemaps in development
sourceMap
}
},
// Copy over the loader's specific rules
...loader.use
];
// Yield the full rule
yield {
test: new RegExp(test),
// Remove all falsy values
use: use.filter(l => l) as Loader[]
};
}
}
}