css-loader 1.0.1 → 7.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2180 -270
- package/dist/cjs.js +5 -0
- package/dist/index.js +178 -0
- package/dist/options.json +218 -0
- package/dist/plugins/index.js +27 -0
- package/dist/plugins/postcss-icss-parser.js +113 -0
- package/dist/plugins/postcss-import-parser.js +293 -0
- package/dist/plugins/postcss-url-parser.js +356 -0
- package/dist/runtime/api.js +85 -0
- package/dist/runtime/getUrl.js +26 -0
- package/dist/runtime/noSourceMaps.js +5 -0
- package/dist/runtime/sourceMaps.js +16 -0
- package/dist/utils.js +1068 -0
- package/package.json +100 -40
- package/CHANGELOG.md +0 -190
- package/index.js +0 -5
- package/lib/compile-exports.js +0 -51
- package/lib/css-base.js +0 -76
- package/lib/getImportPrefix.js +0 -14
- package/lib/getLocalIdent.js +0 -23
- package/lib/loader.js +0 -144
- package/lib/localsLoader.js +0 -44
- package/lib/processCss.js +0 -244
- package/lib/url/escape.js +0 -16
- package/locals.js +0 -5
package/README.md
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
[![npm][npm]][npm-url]
|
|
2
|
-
[![node][node]][node-url]
|
|
3
|
-
[![deps][deps]][deps-url]
|
|
4
|
-
[![tests][tests]][tests-url]
|
|
5
|
-
[![coverage][cover]][cover-url]
|
|
6
|
-
[![chat][chat]][chat-url]
|
|
7
|
-
|
|
8
1
|
<div align="center">
|
|
9
2
|
<img width="180" height="180" vspace="20"
|
|
10
3
|
src="https://cdn.worldvectorlogo.com/logos/css-3.svg">
|
|
@@ -12,170 +5,453 @@
|
|
|
12
5
|
<img width="200" height="200"
|
|
13
6
|
src="https://webpack.js.org/assets/icon-square-big.svg">
|
|
14
7
|
</a>
|
|
15
|
-
<h1>CSS Loader</h1>
|
|
16
8
|
</div>
|
|
17
9
|
|
|
18
|
-
|
|
10
|
+
[![npm][npm]][npm-url]
|
|
11
|
+
[![node][node]][node-url]
|
|
12
|
+
[![tests][tests]][tests-url]
|
|
13
|
+
[![coverage][cover]][cover-url]
|
|
14
|
+
[![discussion][discussion]][discussion-url]
|
|
15
|
+
[![size][size]][size-url]
|
|
16
|
+
|
|
17
|
+
# css-loader
|
|
18
|
+
|
|
19
|
+
The `css-loader` interprets `@import` and `url()` like `import/require()` and resolves them.
|
|
20
|
+
|
|
21
|
+
## Getting Started
|
|
19
22
|
|
|
20
|
-
|
|
23
|
+
> [!WARNING]
|
|
24
|
+
>
|
|
25
|
+
> To use the latest version of css-loader, webpack@5 is required
|
|
26
|
+
|
|
27
|
+
To begin, you'll need to install `css-loader`:
|
|
28
|
+
|
|
29
|
+
```console
|
|
21
30
|
npm install --save-dev css-loader
|
|
22
31
|
```
|
|
23
32
|
|
|
24
|
-
|
|
33
|
+
or
|
|
34
|
+
|
|
35
|
+
```console
|
|
36
|
+
yarn add -D css-loader
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
or
|
|
25
40
|
|
|
26
|
-
|
|
27
|
-
|
|
41
|
+
```console
|
|
42
|
+
pnpm add -D css-loader
|
|
43
|
+
```
|
|
28
44
|
|
|
29
|
-
|
|
30
|
-
and the [url-loader](https://github.com/webpack/url-loader) which you should specify in your config (see [below](https://github.com/webpack-contrib/css-loader#assets)).
|
|
45
|
+
Then, add the loader to your `webpack` configuration. For example:
|
|
31
46
|
|
|
32
47
|
**file.js**
|
|
48
|
+
|
|
33
49
|
```js
|
|
34
|
-
import css from
|
|
50
|
+
import * as css from "file.css";
|
|
35
51
|
```
|
|
36
52
|
|
|
37
53
|
**webpack.config.js**
|
|
54
|
+
|
|
38
55
|
```js
|
|
39
56
|
module.exports = {
|
|
40
57
|
module: {
|
|
41
58
|
rules: [
|
|
42
59
|
{
|
|
43
|
-
test: /\.css
|
|
44
|
-
use: [
|
|
45
|
-
}
|
|
46
|
-
]
|
|
47
|
-
}
|
|
48
|
-
}
|
|
60
|
+
test: /\.css$/i,
|
|
61
|
+
use: ["style-loader", "css-loader"],
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
};
|
|
49
66
|
```
|
|
50
67
|
|
|
51
|
-
|
|
68
|
+
Finally, run `webpack` using the method you normally use (e.g., via CLI or an npm script).
|
|
52
69
|
|
|
53
|
-
|
|
70
|
+
If you need to extract CSS into a separate file (i.e. do not store CSS in a JS module), consider using the [recommend example](https://github.com/webpack/css-loader#recommend).
|
|
71
|
+
|
|
72
|
+
## Options
|
|
73
|
+
|
|
74
|
+
- **[`url`](#url)**
|
|
75
|
+
- **[`import`](#import)**
|
|
76
|
+
- **[`modules`](#modules)**
|
|
77
|
+
- **[`sourceMap`](#sourcemap)**
|
|
78
|
+
- **[`importLoaders`](#importloaders)**
|
|
79
|
+
- **[`esModule`](#esmodule)**
|
|
80
|
+
- **[`exportType`](#exporttype)**
|
|
81
|
+
|
|
82
|
+
### `url`
|
|
83
|
+
|
|
84
|
+
Type:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
type url =
|
|
88
|
+
| boolean
|
|
89
|
+
| {
|
|
90
|
+
filter: (url: string, resourcePath: string) => boolean;
|
|
91
|
+
};
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Default: `true`
|
|
95
|
+
|
|
96
|
+
Enables or disables handling the CSS functions `url` and `image-set`.
|
|
97
|
+
|
|
98
|
+
- If set to `false`, `css-loader` will not parse any paths specified in `url` or `image-set`.
|
|
99
|
+
- You can also pass a function to control this behavior dynamically based on the asset path.
|
|
100
|
+
|
|
101
|
+
As of version [4.0.0](https://github.com/webpack/css-loader/main/CHANGELOG.md#400-2020-07-25), absolute paths are resolved based on the server root.
|
|
102
|
+
|
|
103
|
+
Examples resolutions:
|
|
54
104
|
|
|
55
|
-
**webpack.config.js**
|
|
56
105
|
```js
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
106
|
+
url(image.png) => require('./image.png')
|
|
107
|
+
url('image.png') => require('./image.png')
|
|
108
|
+
url(./image.png) => require('./image.png')
|
|
109
|
+
url('./image.png') => require('./image.png')
|
|
110
|
+
url('http://dontwritehorriblecode.com/2112.png') => require('http://dontwritehorriblecode.com/2112.png')
|
|
111
|
+
image-set(url('image2x.png') 1x, url('image1x.png') 2x) => require('./image1x.png') and require('./image2x.png')
|
|
64
112
|
```
|
|
65
113
|
|
|
66
|
-
or
|
|
114
|
+
To import assets from a `node_modules` path (including `resolve.modules`) or an `alias`, prefix it with a `~`:
|
|
67
115
|
|
|
68
116
|
```js
|
|
69
|
-
|
|
117
|
+
url(~module/image.png) => require('module/image.png')
|
|
118
|
+
url('~module/image.png') => require('module/image.png')
|
|
119
|
+
url(~aliasDirectory/image.png) => require('otherDirectory/image.png')
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
#### `boolean`
|
|
123
|
+
|
|
124
|
+
Enable/disable `url()` resolving.
|
|
125
|
+
|
|
126
|
+
**webpack.config.js**
|
|
70
127
|
|
|
71
|
-
|
|
128
|
+
```js
|
|
129
|
+
module.exports = {
|
|
130
|
+
module: {
|
|
131
|
+
rules: [
|
|
132
|
+
{
|
|
133
|
+
test: /\.css$/i,
|
|
134
|
+
loader: "css-loader",
|
|
135
|
+
options: {
|
|
136
|
+
url: true,
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
},
|
|
141
|
+
};
|
|
72
142
|
```
|
|
73
143
|
|
|
74
|
-
|
|
144
|
+
#### `object`
|
|
75
145
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
It's useful when you, for instance, need to post process the CSS as a string.
|
|
146
|
+
Allows filtering of `url()` values.
|
|
147
|
+
|
|
148
|
+
Any filtered `url()` will not be resolved (left in the code as they were written).
|
|
80
149
|
|
|
81
150
|
**webpack.config.js**
|
|
151
|
+
|
|
82
152
|
```js
|
|
83
|
-
{
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
153
|
+
module.exports = {
|
|
154
|
+
module: {
|
|
155
|
+
rules: [
|
|
156
|
+
{
|
|
157
|
+
test: /\.css$/i,
|
|
158
|
+
loader: "css-loader",
|
|
159
|
+
options: {
|
|
160
|
+
url: {
|
|
161
|
+
filter: (url, resourcePath) => {
|
|
162
|
+
// resourcePath - path to css file
|
|
163
|
+
|
|
164
|
+
// Don't handle `img.png` urls
|
|
165
|
+
if (url.includes("img.png")) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Don't handle images under root-relative /external_images/
|
|
170
|
+
if (/^\/external_images\//.test(url)) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return true;
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
],
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### `import`
|
|
185
|
+
|
|
186
|
+
Type:
|
|
187
|
+
|
|
188
|
+
<!-- use other name to prettify since import is reserved keyword -->
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
type importFn =
|
|
192
|
+
| boolean
|
|
193
|
+
| {
|
|
194
|
+
filter: (
|
|
195
|
+
url: string,
|
|
196
|
+
media: string,
|
|
197
|
+
resourcePath: string,
|
|
198
|
+
supports?: string,
|
|
199
|
+
layer?: string,
|
|
200
|
+
) => boolean;
|
|
201
|
+
};
|
|
91
202
|
```
|
|
92
203
|
|
|
93
|
-
|
|
204
|
+
Default: `true`
|
|
94
205
|
|
|
95
|
-
|
|
96
|
-
|:--:|:--:|:-----:|:----------|
|
|
97
|
-
|**[`url`](#url)**|`{Boolean}`|`true`| Enable/Disable `url()` handling|
|
|
98
|
-
|**[`import`](#import)** |`{Boolean}`|`true`| Enable/Disable @import handling|
|
|
99
|
-
|**[`modules`](#modules)**|`{Boolean}`|`false`|Enable/Disable CSS Modules|
|
|
100
|
-
|**[`localIdentName`](#localidentname)**|`{String}`|`[hash:base64]`|Configure the generated ident|
|
|
101
|
-
|**[`sourceMap`](#sourcemap)**|`{Boolean}`|`false`|Enable/Disable Sourcemaps|
|
|
102
|
-
|**[`camelCase`](#camelcase)**|`{Boolean\|String}`|`false`|Export Classnames in CamelCase|
|
|
103
|
-
|**[`importLoaders`](#importloaders)**|`{Number}`|`0`|Number of loaders applied before CSS loader|
|
|
206
|
+
Allows you to enable or disable handling of `@import` at-rules.
|
|
104
207
|
|
|
105
|
-
|
|
208
|
+
Controls how `@import` statements are resolved.
|
|
106
209
|
|
|
107
|
-
|
|
210
|
+
Absolute URLs in `@import` will be moved in runtime code.
|
|
108
211
|
|
|
109
|
-
|
|
212
|
+
Examples resolutions:
|
|
110
213
|
|
|
111
214
|
```
|
|
112
|
-
|
|
113
|
-
url(
|
|
215
|
+
@import 'style.css' => require('./style.css')
|
|
216
|
+
@import url(style.css) => require('./style.css')
|
|
217
|
+
@import url('style.css') => require('./style.css')
|
|
218
|
+
@import './style.css' => require('./style.css')
|
|
219
|
+
@import url(./style.css) => require('./style.css')
|
|
220
|
+
@import url('./style.css') => require('./style.css')
|
|
221
|
+
@import url('http://dontwritehorriblecode.com/style.css') => @import url('http://dontwritehorriblecode.com/style.css') in runtime
|
|
114
222
|
```
|
|
115
223
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
To disable `@import` resolving by `css-loader` set the option to `false`
|
|
224
|
+
To import styles from a `node_modules` path (include `resolve.modules`) or an `alias`, prefix it with a `~`:
|
|
119
225
|
|
|
120
|
-
```css
|
|
121
|
-
@import url('https://fonts.googleapis.com/css?family=Roboto');
|
|
122
226
|
```
|
|
227
|
+
@import url(~module/style.css) => require('module/style.css')
|
|
228
|
+
@import url('~module/style.css') => require('module/style.css')
|
|
229
|
+
@import url(~aliasDirectory/style.css) => require('otherDirectory/style.css')
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
#### `boolean`
|
|
233
|
+
|
|
234
|
+
Enable/disable `@import` resolving.
|
|
235
|
+
|
|
236
|
+
**webpack.config.js**
|
|
123
237
|
|
|
124
|
-
|
|
238
|
+
```js
|
|
239
|
+
module.exports = {
|
|
240
|
+
module: {
|
|
241
|
+
rules: [
|
|
242
|
+
{
|
|
243
|
+
test: /\.css$/i,
|
|
244
|
+
loader: "css-loader",
|
|
245
|
+
options: {
|
|
246
|
+
import: true,
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
],
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
```
|
|
125
253
|
|
|
126
|
-
|
|
254
|
+
#### `object`
|
|
127
255
|
|
|
128
|
-
|
|
256
|
+
##### `filter`
|
|
129
257
|
|
|
130
|
-
|
|
258
|
+
Type:
|
|
131
259
|
|
|
132
|
-
|
|
260
|
+
```ts
|
|
261
|
+
type filter = (url: string, media: string, resourcePath: string) => boolean;
|
|
262
|
+
```
|
|
133
263
|
|
|
134
|
-
|
|
264
|
+
Default: `undefined`
|
|
135
265
|
|
|
136
|
-
|
|
266
|
+
Allows filtering of `@import`.
|
|
137
267
|
|
|
138
|
-
|
|
268
|
+
Any filtered `@import` will not be resolved (left in the code as they were written).
|
|
139
269
|
|
|
140
|
-
|
|
270
|
+
**webpack.config.js**
|
|
141
271
|
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
:
|
|
145
|
-
:
|
|
146
|
-
|
|
272
|
+
```js
|
|
273
|
+
module.exports = {
|
|
274
|
+
module: {
|
|
275
|
+
rules: [
|
|
276
|
+
{
|
|
277
|
+
test: /\.css$/i,
|
|
278
|
+
loader: "css-loader",
|
|
279
|
+
options: {
|
|
280
|
+
import: {
|
|
281
|
+
filter: (url, media, resourcePath) => {
|
|
282
|
+
// resourcePath - path to css file
|
|
283
|
+
|
|
284
|
+
// Don't handle `style.css` import
|
|
285
|
+
if (url.includes("style.css")) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return true;
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
},
|
|
296
|
+
};
|
|
147
297
|
```
|
|
148
298
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
299
|
+
### `modules`
|
|
300
|
+
|
|
301
|
+
Type:
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
type modules =
|
|
305
|
+
| boolean
|
|
306
|
+
| "local"
|
|
307
|
+
| "global"
|
|
308
|
+
| "pure"
|
|
309
|
+
| "icss"
|
|
310
|
+
| {
|
|
311
|
+
auto: boolean | regExp | ((resourcePath: string) => boolean);
|
|
312
|
+
mode:
|
|
313
|
+
| "local"
|
|
314
|
+
| "global"
|
|
315
|
+
| "pure"
|
|
316
|
+
| "icss"
|
|
317
|
+
| ((resourcePath) => "local" | "global" | "pure" | "icss");
|
|
318
|
+
localIdentName: string;
|
|
319
|
+
localIdentContext: string;
|
|
320
|
+
localIdentHashSalt: string;
|
|
321
|
+
localIdentHashFunction: string;
|
|
322
|
+
localIdentHashDigest: string;
|
|
323
|
+
localIdentRegExp: string | regExp;
|
|
324
|
+
getLocalIdent: (
|
|
325
|
+
context: LoaderContext,
|
|
326
|
+
localIdentName: string,
|
|
327
|
+
localName: string,
|
|
328
|
+
) => string;
|
|
329
|
+
namedExport: boolean;
|
|
330
|
+
exportGlobals: boolean;
|
|
331
|
+
exportLocalsConvention:
|
|
332
|
+
| "as-is"
|
|
333
|
+
| "camel-case"
|
|
334
|
+
| "camel-case-only"
|
|
335
|
+
| "dashes"
|
|
336
|
+
| "dashes-only"
|
|
337
|
+
| ((name: string) => string);
|
|
338
|
+
exportOnlyLocals: boolean;
|
|
339
|
+
getJSON: ({
|
|
340
|
+
resourcePath,
|
|
341
|
+
imports,
|
|
342
|
+
exports,
|
|
343
|
+
replacements,
|
|
344
|
+
}: {
|
|
345
|
+
resourcePath: string;
|
|
346
|
+
imports: object[];
|
|
347
|
+
exports: object[];
|
|
348
|
+
replacements: object[];
|
|
349
|
+
}) => Promise<void> | void;
|
|
350
|
+
};
|
|
154
351
|
```
|
|
155
352
|
|
|
156
|
-
|
|
353
|
+
Default: `undefined`
|
|
354
|
+
|
|
355
|
+
Allows you to enable or disable CSS Modules or ICSS and configure them:
|
|
356
|
+
|
|
357
|
+
- `undefined`: Enables CSS modules for all files matching `/\.module\.\w+$/i.test(filename)` or `/\.icss\.\w+$/i.test(filename)` regexp.
|
|
358
|
+
- `true`: Enables CSS modules for all files.
|
|
359
|
+
- `false`: Disables CSS Modules for all files.
|
|
360
|
+
- `string`: Disables CSS Modules for all files and set the `mode` option (see [mode](https://github.com/webpack/css-loader#mode) for details).
|
|
361
|
+
- `object`: Enables CSS modules for all files unless the `modules.auto` option is provided. otherwise the `modules.auto` option will determine whether if it is CSS Modules or not (see [auto](https://github.com/webpack/css-loader#auto) for more details).
|
|
362
|
+
|
|
363
|
+
The `modules` option enables/disables the **[CSS Modules](https://github.com/css-modules/css-modules)** specification and configures its behavior.
|
|
364
|
+
|
|
365
|
+
Setting `modules: false` can improve performance because we avoid parsing **CSS Modules** features, this is useful for developers using use vanilla CSS or other technologies.
|
|
366
|
+
|
|
367
|
+
**webpack.config.js**
|
|
157
368
|
|
|
158
369
|
```js
|
|
159
|
-
exports
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
370
|
+
module.exports = {
|
|
371
|
+
module: {
|
|
372
|
+
rules: [
|
|
373
|
+
{
|
|
374
|
+
test: /\.css$/i,
|
|
375
|
+
loader: "css-loader",
|
|
376
|
+
options: {
|
|
377
|
+
modules: true,
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
],
|
|
381
|
+
},
|
|
382
|
+
};
|
|
163
383
|
```
|
|
164
384
|
|
|
165
|
-
|
|
385
|
+
#### `Features`
|
|
386
|
+
|
|
387
|
+
##### `Scope`
|
|
388
|
+
|
|
389
|
+
- Using `local` value requires you to specify `:global` classes.
|
|
390
|
+
- Using `global` value requires you to specify `:local` classes.
|
|
391
|
+
- Using `pure` value requires selectors must contain at least one local class or ID.
|
|
392
|
+
|
|
393
|
+
You can find more information on scoping module [here](https://github.com/css-modules/css-modules).
|
|
394
|
+
|
|
395
|
+
With CSS Modules, styles are scoped locally, preventing conflicts with global styles.
|
|
396
|
+
|
|
397
|
+
Use `:local(.className)` to declare a `className` in the local scope. The local identifiers are exported by the module.
|
|
398
|
+
|
|
399
|
+
- With `:local` (without parentheses) local mode can be switched `on` for this selector.
|
|
400
|
+
- The `:global(.className)` notation can be used to declare an explicit global selector.
|
|
401
|
+
- With `:global` (without parentheses) global mode can be switched `on` for this selector.
|
|
402
|
+
|
|
403
|
+
The loader replaces local selectors with unique, scoped identifiers. The chosen unique identifiers are exported by the module.
|
|
404
|
+
|
|
405
|
+
```css
|
|
406
|
+
:local(.className) {
|
|
407
|
+
background: red;
|
|
408
|
+
}
|
|
409
|
+
:local .className {
|
|
410
|
+
color: green;
|
|
411
|
+
}
|
|
412
|
+
:local(.className .subClass) {
|
|
413
|
+
color: green;
|
|
414
|
+
}
|
|
415
|
+
:local .className .subClass :global(.global-class-name) {
|
|
416
|
+
color: blue;
|
|
417
|
+
}
|
|
418
|
+
```
|
|
166
419
|
|
|
167
|
-
|
|
420
|
+
Output (example):
|
|
168
421
|
|
|
422
|
+
```css
|
|
423
|
+
._23_aKvs-b8bW2Vg3fwHozO {
|
|
424
|
+
background: red;
|
|
425
|
+
}
|
|
426
|
+
._23_aKvs-b8bW2Vg3fwHozO {
|
|
427
|
+
color: green;
|
|
428
|
+
}
|
|
429
|
+
._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 {
|
|
430
|
+
color: green;
|
|
431
|
+
}
|
|
432
|
+
._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 .global-class-name {
|
|
433
|
+
color: blue;
|
|
434
|
+
}
|
|
169
435
|
```
|
|
170
|
-
|
|
171
|
-
|
|
436
|
+
|
|
437
|
+
> [!NOTE]
|
|
438
|
+
>
|
|
439
|
+
> Identifiers are exported
|
|
440
|
+
|
|
441
|
+
```js
|
|
442
|
+
exports.locals = {
|
|
443
|
+
className: "_23_aKvs-b8bW2Vg3fwHozO",
|
|
444
|
+
subClass: "_13LGdX8RMStbBE9w-t0gZ1",
|
|
445
|
+
};
|
|
172
446
|
```
|
|
173
447
|
|
|
174
|
-
|
|
448
|
+
CamelCase naming is recommended for local selectors, as it simplifies usage in imported JS modules.
|
|
175
449
|
|
|
176
|
-
|
|
450
|
+
Although you can use `:local(#someId)`, but this is not recommended. Prefer classes instead of IDs for modular styling.
|
|
177
451
|
|
|
178
|
-
|
|
452
|
+
##### `Composing`
|
|
453
|
+
|
|
454
|
+
When declaring a local class name, you can compose it from one or more other local class names.
|
|
179
455
|
|
|
180
456
|
```css
|
|
181
457
|
:local(.className) {
|
|
@@ -189,16 +465,16 @@ When declaring a local classname you can compose a local class from another loca
|
|
|
189
465
|
}
|
|
190
466
|
```
|
|
191
467
|
|
|
192
|
-
This
|
|
468
|
+
This does not alter the final CSS output, but the generated `subClass` will include both class names in its export.
|
|
193
469
|
|
|
194
470
|
```js
|
|
195
471
|
exports.locals = {
|
|
196
|
-
className:
|
|
197
|
-
subClass:
|
|
198
|
-
}
|
|
472
|
+
className: "_23_aKvs-b8bW2Vg3fwHozO",
|
|
473
|
+
subClass: "_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO",
|
|
474
|
+
};
|
|
199
475
|
```
|
|
200
476
|
|
|
201
|
-
```
|
|
477
|
+
```css
|
|
202
478
|
._23_aKvs-b8bW2Vg3fwHozO {
|
|
203
479
|
background: red;
|
|
204
480
|
color: yellow;
|
|
@@ -209,20 +485,24 @@ exports.locals = {
|
|
|
209
485
|
}
|
|
210
486
|
```
|
|
211
487
|
|
|
212
|
-
|
|
488
|
+
##### `Importing`
|
|
489
|
+
|
|
490
|
+
To import a local class names from another module.
|
|
213
491
|
|
|
214
|
-
|
|
492
|
+
> [!NOTE]
|
|
493
|
+
>
|
|
494
|
+
> It is highly recommended to include file extensions when importing a file, since it is possible to import a file with any extension and it is not known in advance which file to use.
|
|
215
495
|
|
|
216
496
|
```css
|
|
217
497
|
:local(.continueButton) {
|
|
218
|
-
composes: button from
|
|
498
|
+
composes: button from "library/button.css";
|
|
219
499
|
background: red;
|
|
220
500
|
}
|
|
221
501
|
```
|
|
222
502
|
|
|
223
503
|
```css
|
|
224
504
|
:local(.nameEdit) {
|
|
225
|
-
composes: edit highlight from
|
|
505
|
+
composes: edit highlight from "./edit.css";
|
|
226
506
|
background: red;
|
|
227
507
|
}
|
|
228
508
|
```
|
|
@@ -231,224 +511,1854 @@ To import from multiple modules use multiple `composes:` rules.
|
|
|
231
511
|
|
|
232
512
|
```css
|
|
233
513
|
:local(.className) {
|
|
234
|
-
composes:
|
|
235
|
-
|
|
514
|
+
composes:
|
|
515
|
+
edit highlight from "./edit.css",
|
|
516
|
+
button from "module/button.css",
|
|
517
|
+
classFromThisModule;
|
|
518
|
+
background: red;
|
|
519
|
+
}
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
or
|
|
523
|
+
|
|
524
|
+
```css
|
|
525
|
+
:local(.className) {
|
|
526
|
+
composes: edit highlight from "./edit.css";
|
|
527
|
+
composes: button from "module/button.css";
|
|
236
528
|
composes: classFromThisModule;
|
|
237
529
|
background: red;
|
|
238
530
|
}
|
|
239
531
|
```
|
|
240
532
|
|
|
241
|
-
|
|
533
|
+
##### `Values`
|
|
242
534
|
|
|
243
|
-
You can
|
|
535
|
+
You can use `@value` to specific values to be reused throughout a document.
|
|
244
536
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
537
|
+
We recommend following a naming convention:
|
|
538
|
+
|
|
539
|
+
- `v-` prefix for values
|
|
540
|
+
- `s-` prefix for selectors
|
|
541
|
+
- `m-` prefix for media at-rules.
|
|
542
|
+
|
|
543
|
+
```css
|
|
544
|
+
@value v-primary: #BF4040;
|
|
545
|
+
@value s-black: black-selector;
|
|
546
|
+
@value m-large: (min-width: 960px);
|
|
547
|
+
|
|
548
|
+
.header {
|
|
549
|
+
color: v-primary;
|
|
550
|
+
padding: 0 10px;
|
|
258
551
|
}
|
|
259
|
-
```
|
|
260
552
|
|
|
261
|
-
|
|
553
|
+
.s-black {
|
|
554
|
+
color: black;
|
|
555
|
+
}
|
|
262
556
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
loader: 'css-loader',
|
|
267
|
-
options: {
|
|
268
|
-
modules: true,
|
|
269
|
-
localIdentName: '[path][name]__[local]--[hash:base64:5]',
|
|
270
|
-
getLocalIdent: (context, localIdentName, localName, options) => {
|
|
271
|
-
return 'whatever_random_class_name'
|
|
272
|
-
}
|
|
557
|
+
@media m-large {
|
|
558
|
+
.header {
|
|
559
|
+
padding: 0 20px;
|
|
273
560
|
}
|
|
274
561
|
}
|
|
275
562
|
```
|
|
276
563
|
|
|
277
|
-
|
|
564
|
+
#### `boolean`
|
|
278
565
|
|
|
279
|
-
|
|
566
|
+
Enable **CSS Modules** features.
|
|
567
|
+
|
|
568
|
+
**webpack.config.js**
|
|
280
569
|
|
|
281
|
-
|
|
570
|
+
```js
|
|
571
|
+
module.exports = {
|
|
572
|
+
module: {
|
|
573
|
+
rules: [
|
|
574
|
+
{
|
|
575
|
+
test: /\.css$/i,
|
|
576
|
+
loader: "css-loader",
|
|
577
|
+
options: {
|
|
578
|
+
modules: true,
|
|
579
|
+
},
|
|
580
|
+
},
|
|
581
|
+
],
|
|
582
|
+
},
|
|
583
|
+
};
|
|
584
|
+
```
|
|
282
585
|
|
|
283
|
-
|
|
586
|
+
#### `string`
|
|
284
587
|
|
|
285
|
-
|
|
588
|
+
Enable **CSS Modules** features and setup `mode`.
|
|
286
589
|
|
|
287
590
|
**webpack.config.js**
|
|
591
|
+
|
|
288
592
|
```js
|
|
289
|
-
{
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
593
|
+
module.exports = {
|
|
594
|
+
module: {
|
|
595
|
+
rules: [
|
|
596
|
+
{
|
|
597
|
+
test: /\.css$/i,
|
|
598
|
+
loader: "css-loader",
|
|
599
|
+
options: {
|
|
600
|
+
// Using `local` value has same effect like using `modules: true`
|
|
601
|
+
modules: "global",
|
|
602
|
+
},
|
|
603
|
+
},
|
|
604
|
+
],
|
|
605
|
+
},
|
|
606
|
+
};
|
|
295
607
|
```
|
|
296
608
|
|
|
297
|
-
|
|
609
|
+
#### `object`
|
|
298
610
|
|
|
299
|
-
|
|
611
|
+
Enable **CSS Modules** features and configure its behavior through `options`.
|
|
300
612
|
|
|
301
|
-
|
|
302
|
-
|:--:|:--:|:----------|
|
|
303
|
-
|**`true`**|`{Boolean}`|Class names will be camelized|
|
|
304
|
-
|**`'dashes'`**|`{String}`|Only dashes in class names will be camelized|
|
|
305
|
-
|**`'only'`** |`{String}`|Introduced in `0.27.1`. Class names will be camelized, the original class name will be removed from the locals|
|
|
306
|
-
|**`'dashesOnly'`**|`{String}`|Introduced in `0.27.1`. Dashes in class names will be camelized, the original class name will be removed from the locals|
|
|
613
|
+
**webpack.config.js**
|
|
307
614
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
615
|
+
```js
|
|
616
|
+
module.exports = {
|
|
617
|
+
module: {
|
|
618
|
+
rules: [
|
|
619
|
+
{
|
|
620
|
+
test: /\.css$/i,
|
|
621
|
+
loader: "css-loader",
|
|
622
|
+
options: {
|
|
623
|
+
modules: {
|
|
624
|
+
mode: "local",
|
|
625
|
+
auto: true,
|
|
626
|
+
exportGlobals: true,
|
|
627
|
+
localIdentName: "[path][name]__[local]--[hash:base64:5]",
|
|
628
|
+
localIdentContext: path.resolve(__dirname, "src"),
|
|
629
|
+
localIdentHashSalt: "my-custom-hash",
|
|
630
|
+
namedExport: true,
|
|
631
|
+
exportLocalsConvention: "as-is",
|
|
632
|
+
exportOnlyLocals: false,
|
|
633
|
+
getJSON: ({ resourcePath, imports, exports, replacements }) => {},
|
|
634
|
+
},
|
|
635
|
+
},
|
|
636
|
+
},
|
|
637
|
+
],
|
|
638
|
+
},
|
|
639
|
+
};
|
|
311
640
|
```
|
|
312
641
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
642
|
+
##### `auto`
|
|
643
|
+
|
|
644
|
+
Type:
|
|
645
|
+
|
|
646
|
+
```ts
|
|
647
|
+
type auto =
|
|
648
|
+
| boolean
|
|
649
|
+
| regExp
|
|
650
|
+
| ((
|
|
651
|
+
resourcePath: string,
|
|
652
|
+
resourceQuery: string,
|
|
653
|
+
resourceFragment: string,
|
|
654
|
+
) => boolean);
|
|
316
655
|
```
|
|
317
656
|
|
|
657
|
+
Default: `undefined`
|
|
658
|
+
|
|
659
|
+
Allows auto enable CSS modules or ICSS based on the file name, query or fragment when `modules` option is an object.
|
|
660
|
+
|
|
661
|
+
Possible values:
|
|
662
|
+
|
|
663
|
+
- `undefined`: Enables CSS modules for all files.
|
|
664
|
+
- `true`: Enables CSS modules for files matching `/\.module\.\w+$/i.test(filename)` and `/\.icss\.\w+$/i.test(filename)` regexp.
|
|
665
|
+
- `false`: Disables CSS Modules for all files.
|
|
666
|
+
- `RegExp`: Enables CSS modules for all files matching `/RegExp/i.test(filename)` regexp.
|
|
667
|
+
- `function`: Enables CSS Modules for files based on the file name satisfying your filter function check.
|
|
668
|
+
|
|
669
|
+
###### `boolean`
|
|
670
|
+
|
|
671
|
+
Possible values:
|
|
672
|
+
|
|
673
|
+
- `true`: Enables CSS modules or Interoperable CSS (ICSS) format, sets the [`modules.mode`](#mode) option to `local` value for all files which satisfy `/\.module(s)?\.\w+$/i.test(filename)` condition or sets the [`modules.mode`](#mode) option to `icss` value for all files which satisfy `/\.icss\.\w+$/i.test(filename)` condition.
|
|
674
|
+
- `false`: Disables CSS modules or ICSS format based on filename for all files.
|
|
675
|
+
|
|
318
676
|
**webpack.config.js**
|
|
677
|
+
|
|
319
678
|
```js
|
|
320
|
-
{
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
679
|
+
module.exports = {
|
|
680
|
+
module: {
|
|
681
|
+
rules: [
|
|
682
|
+
{
|
|
683
|
+
test: /\.css$/i,
|
|
684
|
+
loader: "css-loader",
|
|
685
|
+
options: {
|
|
686
|
+
modules: {
|
|
687
|
+
auto: true,
|
|
688
|
+
},
|
|
689
|
+
},
|
|
690
|
+
},
|
|
691
|
+
],
|
|
692
|
+
},
|
|
693
|
+
};
|
|
326
694
|
```
|
|
327
695
|
|
|
328
|
-
|
|
696
|
+
###### `RegExp`
|
|
329
697
|
|
|
330
|
-
|
|
698
|
+
Enables CSS modules for files based on the filename satisfying your regex check.
|
|
331
699
|
|
|
332
700
|
**webpack.config.js**
|
|
701
|
+
|
|
333
702
|
```js
|
|
334
|
-
{
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
}
|
|
703
|
+
module.exports = {
|
|
704
|
+
module: {
|
|
705
|
+
rules: [
|
|
706
|
+
{
|
|
707
|
+
test: /\.css$/i,
|
|
708
|
+
loader: "css-loader",
|
|
709
|
+
options: {
|
|
710
|
+
modules: {
|
|
711
|
+
auto: /\.custom-module\.\w+$/i,
|
|
712
|
+
},
|
|
713
|
+
},
|
|
714
|
+
},
|
|
715
|
+
],
|
|
716
|
+
},
|
|
717
|
+
};
|
|
348
718
|
```
|
|
349
719
|
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
<h2 align="center">Examples</h2>
|
|
353
|
-
|
|
354
|
-
### Assets
|
|
720
|
+
###### `function`
|
|
355
721
|
|
|
356
|
-
|
|
722
|
+
Enables CSS Modules for files based on the filename, query or fragment satisfying your filter function check.
|
|
357
723
|
|
|
358
724
|
**webpack.config.js**
|
|
725
|
+
|
|
359
726
|
```js
|
|
360
727
|
module.exports = {
|
|
361
728
|
module: {
|
|
362
729
|
rules: [
|
|
363
730
|
{
|
|
364
|
-
test: /\.css
|
|
365
|
-
|
|
366
|
-
},
|
|
367
|
-
{
|
|
368
|
-
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
|
|
369
|
-
loader: 'url-loader',
|
|
731
|
+
test: /\.css$/i,
|
|
732
|
+
loader: "css-loader",
|
|
370
733
|
options: {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
}
|
|
734
|
+
modules: {
|
|
735
|
+
auto: (resourcePath, resourceQuery, resourceFragment) => {
|
|
736
|
+
return resourcePath.endsWith(".custom-module.css");
|
|
737
|
+
},
|
|
738
|
+
},
|
|
739
|
+
},
|
|
740
|
+
},
|
|
741
|
+
],
|
|
742
|
+
},
|
|
743
|
+
};
|
|
377
744
|
```
|
|
378
745
|
|
|
379
|
-
|
|
746
|
+
##### `mode`
|
|
747
|
+
|
|
748
|
+
Type:
|
|
749
|
+
|
|
750
|
+
```ts
|
|
751
|
+
type mode =
|
|
752
|
+
| "local"
|
|
753
|
+
| "global"
|
|
754
|
+
| "pure"
|
|
755
|
+
| "icss"
|
|
756
|
+
| ((
|
|
757
|
+
resourcePath: string,
|
|
758
|
+
resourceQuery: string,
|
|
759
|
+
resourceFragment: string,
|
|
760
|
+
) => "local" | "global" | "pure" | "icss");
|
|
761
|
+
```
|
|
380
762
|
|
|
381
|
-
|
|
382
|
-
This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to extract the CSS when running in production mode.
|
|
383
|
-
|
|
384
|
-
<h2 align="center">Maintainers</h2>
|
|
385
|
-
|
|
386
|
-
<table>
|
|
387
|
-
<tbody>
|
|
388
|
-
<tr>
|
|
389
|
-
<td align="center">
|
|
390
|
-
<img width="150" height="150"
|
|
391
|
-
src="https://github.com/bebraw.png?v=3&s=150">
|
|
392
|
-
</br>
|
|
393
|
-
<a href="https://github.com/bebraw">Juho Vepsäläinen</a>
|
|
394
|
-
</td>
|
|
395
|
-
<td align="center">
|
|
396
|
-
<img width="150" height="150"
|
|
397
|
-
src="https://github.com/d3viant0ne.png?v=3&s=150">
|
|
398
|
-
</br>
|
|
399
|
-
<a href="https://github.com/d3viant0ne">Joshua Wiens</a>
|
|
400
|
-
</td>
|
|
401
|
-
<td align="center">
|
|
402
|
-
<img width="150" height="150"
|
|
403
|
-
src="https://github.com/SpaceK33z.png?v=3&s=150">
|
|
404
|
-
</br>
|
|
405
|
-
<a href="https://github.com/SpaceK33z">Kees Kluskens</a>
|
|
406
|
-
</td>
|
|
407
|
-
<td align="center">
|
|
408
|
-
<img width="150" height="150"
|
|
409
|
-
src="https://github.com/TheLarkInn.png?v=3&s=150">
|
|
410
|
-
</br>
|
|
411
|
-
<a href="https://github.com/TheLarkInn">Sean Larkin</a>
|
|
412
|
-
</td>
|
|
413
|
-
</tr>
|
|
414
|
-
<tr>
|
|
415
|
-
<td align="center">
|
|
416
|
-
<img width="150" height="150"
|
|
417
|
-
src="https://github.com/michael-ciniawsky.png?v=3&s=150">
|
|
418
|
-
</br>
|
|
419
|
-
<a href="https://github.com/michael-ciniawsky">Michael Ciniawsky</a>
|
|
420
|
-
</td>
|
|
421
|
-
<td align="center">
|
|
422
|
-
<img width="150" height="150"
|
|
423
|
-
src="https://github.com/evilebottnawi.png?v=3&s=150">
|
|
424
|
-
</br>
|
|
425
|
-
<a href="https://github.com/evilebottnawi">Evilebot Tnawi</a>
|
|
426
|
-
</td>
|
|
427
|
-
<td align="center">
|
|
428
|
-
<img width="150" height="150"
|
|
429
|
-
src="https://github.com/joscha.png?v=3&s=150">
|
|
430
|
-
</br>
|
|
431
|
-
<a href="https://github.com/joscha">Joscha Feth</a>
|
|
432
|
-
</td>
|
|
433
|
-
</tr>
|
|
434
|
-
<tbody>
|
|
435
|
-
</table>
|
|
763
|
+
Default: `'local'`
|
|
436
764
|
|
|
765
|
+
Setup `mode` option. You can omit the value when you want `local` mode.
|
|
437
766
|
|
|
438
|
-
|
|
439
|
-
[npm-url]: https://npmjs.com/package/css-loader
|
|
767
|
+
Controls the level of compilation applied to the input styles.
|
|
440
768
|
|
|
441
|
-
|
|
442
|
-
|
|
769
|
+
- The `local`, `global`, and `pure` handles `class` and `id` scoping and `@value` values.
|
|
770
|
+
- The `icss` will only compile the low level `Interoperable CSS (ICSS)` format for declaring `:import` and `:export` dependencies between CSS and other languages.
|
|
443
771
|
|
|
444
|
-
|
|
445
|
-
[deps-url]: https://david-dm.org/webpack-contrib/css-loader
|
|
772
|
+
ICSS underpins CSS Module support, and provides a low level syntax for other tools to implement CSS-module variations of their own.
|
|
446
773
|
|
|
447
|
-
|
|
448
|
-
[tests-url]: https://travis-ci.org/webpack-contrib/css-loader
|
|
774
|
+
###### `string`
|
|
449
775
|
|
|
450
|
-
|
|
451
|
-
|
|
776
|
+
Possible values - `local`, `global`, `pure`, and `icss`.
|
|
777
|
+
|
|
778
|
+
**webpack.config.js**
|
|
452
779
|
|
|
453
|
-
|
|
454
|
-
|
|
780
|
+
```js
|
|
781
|
+
module.exports = {
|
|
782
|
+
module: {
|
|
783
|
+
rules: [
|
|
784
|
+
{
|
|
785
|
+
test: /\.css$/i,
|
|
786
|
+
loader: "css-loader",
|
|
787
|
+
options: {
|
|
788
|
+
modules: {
|
|
789
|
+
mode: "global",
|
|
790
|
+
},
|
|
791
|
+
},
|
|
792
|
+
},
|
|
793
|
+
],
|
|
794
|
+
},
|
|
795
|
+
};
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
###### `function`
|
|
799
|
+
|
|
800
|
+
Allows setting different values for the `mode` option based on the filename, query or fragment.
|
|
801
|
+
Possible return values - `local`, `global`, `pure` and `icss`.
|
|
802
|
+
|
|
803
|
+
**webpack.config.js**
|
|
804
|
+
|
|
805
|
+
```js
|
|
806
|
+
module.exports = {
|
|
807
|
+
module: {
|
|
808
|
+
rules: [
|
|
809
|
+
{
|
|
810
|
+
test: /\.css$/i,
|
|
811
|
+
loader: "css-loader",
|
|
812
|
+
options: {
|
|
813
|
+
modules: {
|
|
814
|
+
// Callback must return "local", "global", or "pure" values
|
|
815
|
+
mode: (resourcePath, resourceQuery, resourceFragment) => {
|
|
816
|
+
if (/pure.css$/i.test(resourcePath)) {
|
|
817
|
+
return "pure";
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
if (/global.css$/i.test(resourcePath)) {
|
|
821
|
+
return "global";
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
return "local";
|
|
825
|
+
},
|
|
826
|
+
},
|
|
827
|
+
},
|
|
828
|
+
},
|
|
829
|
+
],
|
|
830
|
+
},
|
|
831
|
+
};
|
|
832
|
+
```
|
|
833
|
+
|
|
834
|
+
##### `localIdentName`
|
|
835
|
+
|
|
836
|
+
Type:
|
|
837
|
+
|
|
838
|
+
```ts
|
|
839
|
+
type localIdentName = string;
|
|
840
|
+
```
|
|
841
|
+
|
|
842
|
+
Default: `'[hash:base64]'`
|
|
843
|
+
|
|
844
|
+
Allows to configure the generated local ident name.
|
|
845
|
+
|
|
846
|
+
For more information on options see:
|
|
847
|
+
|
|
848
|
+
- [webpack template strings](https://webpack.js.org/configuration/output/#template-strings),
|
|
849
|
+
- [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest),
|
|
850
|
+
- [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength),
|
|
851
|
+
- [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction),
|
|
852
|
+
- [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).
|
|
853
|
+
|
|
854
|
+
Supported template strings:
|
|
855
|
+
|
|
856
|
+
- `[name]` the basename of the resource
|
|
857
|
+
- `[folder]` the folder the resource relative to the `compiler.context` option or `modules.localIdentContext` option.
|
|
858
|
+
- `[path]` the path of the resource relative to the `compiler.context` option or `modules.localIdentContext` option.
|
|
859
|
+
- `[file]` - filename and path.
|
|
860
|
+
- `[ext]` - extension with leading `.`.
|
|
861
|
+
- `[hash]` - the hash of the string, generated based on `localIdentHashSalt`, `localIdentHashFunction`, `localIdentHashDigest`, `localIdentHashDigestLength`, `localIdentContext`, `resourcePath` and `exportName`
|
|
862
|
+
- `[<hashFunction>:hash:<hashDigest>:<hashDigestLength>]` - hash with hash settings.
|
|
863
|
+
- `[local]` - original class.
|
|
864
|
+
|
|
865
|
+
Recommendations:
|
|
866
|
+
|
|
867
|
+
- Use `'[path][name]__[local]'` for development
|
|
868
|
+
- Use `'[hash:base64]'` for production
|
|
869
|
+
|
|
870
|
+
The `[local]` placeholder contains original class.
|
|
871
|
+
|
|
872
|
+
**Note:** all reserved characters (`<>:"/\|?*`) and control filesystem characters (excluding characters in the `[local]` placeholder) will be converted to `-`.
|
|
873
|
+
|
|
874
|
+
**webpack.config.js**
|
|
875
|
+
|
|
876
|
+
```js
|
|
877
|
+
module.exports = {
|
|
878
|
+
module: {
|
|
879
|
+
rules: [
|
|
880
|
+
{
|
|
881
|
+
test: /\.css$/i,
|
|
882
|
+
loader: "css-loader",
|
|
883
|
+
options: {
|
|
884
|
+
modules: {
|
|
885
|
+
localIdentName: "[path][name]__[local]--[hash:base64:5]",
|
|
886
|
+
},
|
|
887
|
+
},
|
|
888
|
+
},
|
|
889
|
+
],
|
|
890
|
+
},
|
|
891
|
+
};
|
|
892
|
+
```
|
|
893
|
+
|
|
894
|
+
##### `localIdentContext`
|
|
895
|
+
|
|
896
|
+
Type:
|
|
897
|
+
|
|
898
|
+
```ts
|
|
899
|
+
type localIdentContex = string;
|
|
900
|
+
```
|
|
901
|
+
|
|
902
|
+
Default: `compiler.context`
|
|
903
|
+
|
|
904
|
+
Allows redefining the basic loader context for local ident name.
|
|
905
|
+
|
|
906
|
+
**webpack.config.js**
|
|
907
|
+
|
|
908
|
+
```js
|
|
909
|
+
module.exports = {
|
|
910
|
+
module: {
|
|
911
|
+
rules: [
|
|
912
|
+
{
|
|
913
|
+
test: /\.css$/i,
|
|
914
|
+
loader: "css-loader",
|
|
915
|
+
options: {
|
|
916
|
+
modules: {
|
|
917
|
+
localIdentContext: path.resolve(__dirname, "src"),
|
|
918
|
+
},
|
|
919
|
+
},
|
|
920
|
+
},
|
|
921
|
+
],
|
|
922
|
+
},
|
|
923
|
+
};
|
|
924
|
+
```
|
|
925
|
+
|
|
926
|
+
##### `localIdentHashSalt`
|
|
927
|
+
|
|
928
|
+
Type:
|
|
929
|
+
|
|
930
|
+
```ts
|
|
931
|
+
type localIdentHashSalt = string;
|
|
932
|
+
```
|
|
933
|
+
|
|
934
|
+
Default: `undefined`
|
|
935
|
+
|
|
936
|
+
Allows to add custom hash to generate more unique classes.
|
|
937
|
+
|
|
938
|
+
For more information see [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).
|
|
939
|
+
|
|
940
|
+
**webpack.config.js**
|
|
941
|
+
|
|
942
|
+
```js
|
|
943
|
+
module.exports = {
|
|
944
|
+
module: {
|
|
945
|
+
rules: [
|
|
946
|
+
{
|
|
947
|
+
test: /\.css$/i,
|
|
948
|
+
loader: "css-loader",
|
|
949
|
+
options: {
|
|
950
|
+
modules: {
|
|
951
|
+
localIdentHashSalt: "hash",
|
|
952
|
+
},
|
|
953
|
+
},
|
|
954
|
+
},
|
|
955
|
+
],
|
|
956
|
+
},
|
|
957
|
+
};
|
|
958
|
+
```
|
|
959
|
+
|
|
960
|
+
##### `localIdentHashFunction`
|
|
961
|
+
|
|
962
|
+
Type:
|
|
963
|
+
|
|
964
|
+
```ts
|
|
965
|
+
type localIdentHashFunction = string;
|
|
966
|
+
```
|
|
967
|
+
|
|
968
|
+
Default: `md4`
|
|
969
|
+
|
|
970
|
+
Allows to specify hash function to generate classes .
|
|
971
|
+
|
|
972
|
+
For more information see [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction).
|
|
973
|
+
|
|
974
|
+
**webpack.config.js**
|
|
975
|
+
|
|
976
|
+
```js
|
|
977
|
+
module.exports = {
|
|
978
|
+
module: {
|
|
979
|
+
rules: [
|
|
980
|
+
{
|
|
981
|
+
test: /\.css$/i,
|
|
982
|
+
loader: "css-loader",
|
|
983
|
+
options: {
|
|
984
|
+
modules: {
|
|
985
|
+
localIdentHashFunction: "md4",
|
|
986
|
+
},
|
|
987
|
+
},
|
|
988
|
+
},
|
|
989
|
+
],
|
|
990
|
+
},
|
|
991
|
+
};
|
|
992
|
+
```
|
|
993
|
+
|
|
994
|
+
##### `localIdentHashDigest`
|
|
995
|
+
|
|
996
|
+
Type:
|
|
997
|
+
|
|
998
|
+
```ts
|
|
999
|
+
type localIdentHashDigest = string;
|
|
1000
|
+
```
|
|
1001
|
+
|
|
1002
|
+
Default: `hex`
|
|
1003
|
+
|
|
1004
|
+
Allows to specify hash digest to generate classes.
|
|
1005
|
+
|
|
1006
|
+
For more information see [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest).
|
|
1007
|
+
|
|
1008
|
+
**webpack.config.js**
|
|
1009
|
+
|
|
1010
|
+
```js
|
|
1011
|
+
module.exports = {
|
|
1012
|
+
module: {
|
|
1013
|
+
rules: [
|
|
1014
|
+
{
|
|
1015
|
+
test: /\.css$/i,
|
|
1016
|
+
loader: "css-loader",
|
|
1017
|
+
options: {
|
|
1018
|
+
modules: {
|
|
1019
|
+
localIdentHashDigest: "base64",
|
|
1020
|
+
},
|
|
1021
|
+
},
|
|
1022
|
+
},
|
|
1023
|
+
],
|
|
1024
|
+
},
|
|
1025
|
+
};
|
|
1026
|
+
```
|
|
1027
|
+
|
|
1028
|
+
##### `localIdentHashDigestLength`
|
|
1029
|
+
|
|
1030
|
+
Type:
|
|
1031
|
+
|
|
1032
|
+
```ts
|
|
1033
|
+
type localIdentHashDigestLength = number;
|
|
1034
|
+
```
|
|
1035
|
+
|
|
1036
|
+
Default: `20`
|
|
1037
|
+
|
|
1038
|
+
Allows to specify hash digest length to generate classes.
|
|
1039
|
+
|
|
1040
|
+
For more information, see [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength).
|
|
1041
|
+
|
|
1042
|
+
**webpack.config.js**
|
|
1043
|
+
|
|
1044
|
+
```js
|
|
1045
|
+
module.exports = {
|
|
1046
|
+
module: {
|
|
1047
|
+
rules: [
|
|
1048
|
+
{
|
|
1049
|
+
test: /\.css$/i,
|
|
1050
|
+
loader: "css-loader",
|
|
1051
|
+
options: {
|
|
1052
|
+
modules: {
|
|
1053
|
+
localIdentHashDigestLength: 5,
|
|
1054
|
+
},
|
|
1055
|
+
},
|
|
1056
|
+
},
|
|
1057
|
+
],
|
|
1058
|
+
},
|
|
1059
|
+
};
|
|
1060
|
+
```
|
|
1061
|
+
|
|
1062
|
+
##### `hashStrategy`
|
|
1063
|
+
|
|
1064
|
+
Type: `'resource-path-and-local-name' | 'minimal-subset'`
|
|
1065
|
+
Default: `'resource-path-and-local-name'`
|
|
1066
|
+
|
|
1067
|
+
Should local name be used when computing the hash.
|
|
1068
|
+
|
|
1069
|
+
- `'resource-path-and-local-name'` Both resource path and local name are used when hashing. Each identifier in a module gets its own hash digest, always.
|
|
1070
|
+
- `'minimal-subset'` Auto detect if identifier names can be omitted from hashing. Use this value to optimize the output for better GZIP or Brotli compression.
|
|
1071
|
+
|
|
1072
|
+
**webpack.config.js**
|
|
1073
|
+
|
|
1074
|
+
```js
|
|
1075
|
+
module.exports = {
|
|
1076
|
+
module: {
|
|
1077
|
+
rules: [
|
|
1078
|
+
{
|
|
1079
|
+
test: /\.css$/i,
|
|
1080
|
+
loader: "css-loader",
|
|
1081
|
+
options: {
|
|
1082
|
+
modules: {
|
|
1083
|
+
hashStrategy: "minimal-subset",
|
|
1084
|
+
},
|
|
1085
|
+
},
|
|
1086
|
+
},
|
|
1087
|
+
],
|
|
1088
|
+
},
|
|
1089
|
+
};
|
|
1090
|
+
```
|
|
1091
|
+
|
|
1092
|
+
##### `localIdentRegExp`
|
|
1093
|
+
|
|
1094
|
+
Type:
|
|
1095
|
+
|
|
1096
|
+
```ts
|
|
1097
|
+
type localIdentRegExp = string | RegExp;
|
|
1098
|
+
```
|
|
1099
|
+
|
|
1100
|
+
Default: `undefined`
|
|
1101
|
+
|
|
1102
|
+
**webpack.config.js**
|
|
1103
|
+
|
|
1104
|
+
```js
|
|
1105
|
+
module.exports = {
|
|
1106
|
+
module: {
|
|
1107
|
+
rules: [
|
|
1108
|
+
{
|
|
1109
|
+
test: /\.css$/i,
|
|
1110
|
+
loader: "css-loader",
|
|
1111
|
+
options: {
|
|
1112
|
+
modules: {
|
|
1113
|
+
localIdentRegExp: /page-(.*)\.css/i,
|
|
1114
|
+
},
|
|
1115
|
+
},
|
|
1116
|
+
},
|
|
1117
|
+
],
|
|
1118
|
+
},
|
|
1119
|
+
};
|
|
1120
|
+
```
|
|
1121
|
+
|
|
1122
|
+
##### `getLocalIdent`
|
|
1123
|
+
|
|
1124
|
+
Type:
|
|
1125
|
+
|
|
1126
|
+
```ts
|
|
1127
|
+
type getLocalIdent = (
|
|
1128
|
+
context: LoaderContext,
|
|
1129
|
+
localIdentName: string,
|
|
1130
|
+
localName: string,
|
|
1131
|
+
) => string;
|
|
1132
|
+
```
|
|
1133
|
+
|
|
1134
|
+
Default: `undefined`
|
|
1135
|
+
|
|
1136
|
+
Allows to specify a function to generate the classname.
|
|
1137
|
+
|
|
1138
|
+
By default we use built-in function to generate a classname.
|
|
1139
|
+
|
|
1140
|
+
If your custom function returns `null` or `undefined`, the built-in generator is used as a `fallback`.
|
|
1141
|
+
|
|
1142
|
+
**webpack.config.js**
|
|
1143
|
+
|
|
1144
|
+
```js
|
|
1145
|
+
module.exports = {
|
|
1146
|
+
module: {
|
|
1147
|
+
rules: [
|
|
1148
|
+
{
|
|
1149
|
+
test: /\.css$/i,
|
|
1150
|
+
loader: "css-loader",
|
|
1151
|
+
options: {
|
|
1152
|
+
modules: {
|
|
1153
|
+
getLocalIdent: (context, localIdentName, localName, options) => {
|
|
1154
|
+
return "whatever_random_class_name";
|
|
1155
|
+
},
|
|
1156
|
+
},
|
|
1157
|
+
},
|
|
1158
|
+
},
|
|
1159
|
+
],
|
|
1160
|
+
},
|
|
1161
|
+
};
|
|
1162
|
+
```
|
|
1163
|
+
|
|
1164
|
+
##### `namedExport`
|
|
1165
|
+
|
|
1166
|
+
Type:
|
|
1167
|
+
|
|
1168
|
+
```ts
|
|
1169
|
+
type namedExport = boolean;
|
|
1170
|
+
```
|
|
1171
|
+
|
|
1172
|
+
Default: Depends on the value of the `esModule` option. If the value of the `esModule` options is `true`, `namedExport` defaults to `true` ; otherwise, it defaults to `false`.
|
|
1173
|
+
|
|
1174
|
+
Enables or disables ES modules named export for locals.
|
|
1175
|
+
|
|
1176
|
+
> [!WARNING]
|
|
1177
|
+
>
|
|
1178
|
+
> The `default` class name cannot be used directly when `namedExport` is `true` because `default` is a reserved keyword in ECMAScript modules. It is automatically renamed to `_default`.
|
|
1179
|
+
|
|
1180
|
+
**styles.css**
|
|
1181
|
+
|
|
1182
|
+
```css
|
|
1183
|
+
.foo-baz {
|
|
1184
|
+
color: red;
|
|
1185
|
+
}
|
|
1186
|
+
.bar {
|
|
1187
|
+
color: blue;
|
|
1188
|
+
}
|
|
1189
|
+
.default {
|
|
1190
|
+
color: green;
|
|
1191
|
+
}
|
|
1192
|
+
```
|
|
1193
|
+
|
|
1194
|
+
**index.js**
|
|
1195
|
+
|
|
1196
|
+
```js
|
|
1197
|
+
import * as styles from "./styles.css";
|
|
1198
|
+
|
|
1199
|
+
// If using `exportLocalsConvention: "as-is"` (default value):
|
|
1200
|
+
console.log(styles["foo-baz"], styles.bar);
|
|
1201
|
+
|
|
1202
|
+
// If using `exportLocalsConvention: "camel-case-only"`:
|
|
1203
|
+
console.log(styles.fooBaz, styles.bar);
|
|
1204
|
+
|
|
1205
|
+
// For the `default` classname
|
|
1206
|
+
console.log(styles["_default"]);
|
|
1207
|
+
```
|
|
1208
|
+
|
|
1209
|
+
You can enable ES module named export using:
|
|
1210
|
+
|
|
1211
|
+
**webpack.config.js**
|
|
1212
|
+
|
|
1213
|
+
```js
|
|
1214
|
+
module.exports = {
|
|
1215
|
+
module: {
|
|
1216
|
+
rules: [
|
|
1217
|
+
{
|
|
1218
|
+
test: /\.css$/i,
|
|
1219
|
+
loader: "css-loader",
|
|
1220
|
+
options: {
|
|
1221
|
+
esModule: true,
|
|
1222
|
+
modules: {
|
|
1223
|
+
namedExport: true,
|
|
1224
|
+
},
|
|
1225
|
+
},
|
|
1226
|
+
},
|
|
1227
|
+
],
|
|
1228
|
+
},
|
|
1229
|
+
};
|
|
1230
|
+
```
|
|
1231
|
+
|
|
1232
|
+
To set a custom name for namedExport, can use [`exportLocalsConvention`](#exportLocalsConvention) option as a function.
|
|
1233
|
+
|
|
1234
|
+
See below in the [`examples`](#examples) section.
|
|
1235
|
+
|
|
1236
|
+
##### `exportGlobals`
|
|
1237
|
+
|
|
1238
|
+
Type:
|
|
1239
|
+
|
|
1240
|
+
```ts
|
|
1241
|
+
type exportsGLobals = boolean;
|
|
1242
|
+
```
|
|
1243
|
+
|
|
1244
|
+
Default: `false`
|
|
1245
|
+
|
|
1246
|
+
Allow `css-loader` to export names from global class or ID, so you can use that as local name.
|
|
1247
|
+
|
|
1248
|
+
**webpack.config.js**
|
|
1249
|
+
|
|
1250
|
+
```js
|
|
1251
|
+
module.exports = {
|
|
1252
|
+
module: {
|
|
1253
|
+
rules: [
|
|
1254
|
+
{
|
|
1255
|
+
test: /\.css$/i,
|
|
1256
|
+
loader: "css-loader",
|
|
1257
|
+
options: {
|
|
1258
|
+
modules: {
|
|
1259
|
+
exportGlobals: true,
|
|
1260
|
+
},
|
|
1261
|
+
},
|
|
1262
|
+
},
|
|
1263
|
+
],
|
|
1264
|
+
},
|
|
1265
|
+
};
|
|
1266
|
+
```
|
|
1267
|
+
|
|
1268
|
+
##### `exportLocalsConvention`
|
|
1269
|
+
|
|
1270
|
+
Type:
|
|
1271
|
+
|
|
1272
|
+
```ts
|
|
1273
|
+
type exportLocalsConvention =
|
|
1274
|
+
| "as-is"
|
|
1275
|
+
| "camel-case"
|
|
1276
|
+
| "camel-case-only"
|
|
1277
|
+
| "dashes"
|
|
1278
|
+
| "dashes-only"
|
|
1279
|
+
| ((name: string) => string);
|
|
1280
|
+
```
|
|
1281
|
+
|
|
1282
|
+
Default: Depends on the value of the `modules.namedExport` option:
|
|
1283
|
+
|
|
1284
|
+
- If `true` - `as-is`
|
|
1285
|
+
- Otherwise `camel-case-only` (class names converted to camelCase, original name removed).
|
|
1286
|
+
|
|
1287
|
+
> [!WARNING]
|
|
1288
|
+
>
|
|
1289
|
+
> Names of locals are converted to camelCase when the named export is `false`, i.e. the `exportLocalsConvention` option has`camelCaseOnly` value by default.
|
|
1290
|
+
> You can set this back to any other valid option but selectors which are not valid JavaScript identifiers may run into problems which do not implement the entire modules specification.
|
|
1291
|
+
|
|
1292
|
+
Style of exported class names.
|
|
1293
|
+
|
|
1294
|
+
###### `string`
|
|
1295
|
+
|
|
1296
|
+
By default, the exported JSON keys mirror the class names (i.e `as-is` value).
|
|
1297
|
+
|
|
1298
|
+
| Name | Type | Description |
|
|
1299
|
+
| :---------------------: | :------: | :-------------------------------------------------------------------------------------------------- |
|
|
1300
|
+
| **`'as-is'`** | `string` | Class names will be exported as is. |
|
|
1301
|
+
| **`'camel-case'`** | `string` | Class names will be camelCased, but the original class name will not to be removed from the locals. |
|
|
1302
|
+
| **`'camel-case-only'`** | `string` | Class names will be camelCased, and original class name will be removed from the locals. |
|
|
1303
|
+
| **`'dashes'`** | `string` | Only dashes in class names will be camelCased |
|
|
1304
|
+
| **`'dashes-only'`** | `string` | Dashes in class names will be camelCased, the original class name will be removed from the locals |
|
|
1305
|
+
|
|
1306
|
+
**file.css**
|
|
1307
|
+
|
|
1308
|
+
```css
|
|
1309
|
+
.class-name {
|
|
1310
|
+
}
|
|
1311
|
+
```
|
|
1312
|
+
|
|
1313
|
+
**file.js**
|
|
1314
|
+
|
|
1315
|
+
```js
|
|
1316
|
+
import { className } from "file.css";
|
|
1317
|
+
```
|
|
1318
|
+
|
|
1319
|
+
**webpack.config.js**
|
|
1320
|
+
|
|
1321
|
+
```js
|
|
1322
|
+
module.exports = {
|
|
1323
|
+
module: {
|
|
1324
|
+
rules: [
|
|
1325
|
+
{
|
|
1326
|
+
test: /\.css$/i,
|
|
1327
|
+
loader: "css-loader",
|
|
1328
|
+
options: {
|
|
1329
|
+
modules: {
|
|
1330
|
+
exportLocalsConvention: "camel-case-only",
|
|
1331
|
+
},
|
|
1332
|
+
},
|
|
1333
|
+
},
|
|
1334
|
+
],
|
|
1335
|
+
},
|
|
1336
|
+
};
|
|
1337
|
+
```
|
|
1338
|
+
|
|
1339
|
+
###### `function`
|
|
1340
|
+
|
|
1341
|
+
**webpack.config.js**
|
|
1342
|
+
|
|
1343
|
+
```js
|
|
1344
|
+
module.exports = {
|
|
1345
|
+
module: {
|
|
1346
|
+
rules: [
|
|
1347
|
+
{
|
|
1348
|
+
test: /\.css$/i,
|
|
1349
|
+
loader: "css-loader",
|
|
1350
|
+
options: {
|
|
1351
|
+
modules: {
|
|
1352
|
+
exportLocalsConvention: function (name) {
|
|
1353
|
+
return name.replace(/-/g, "_");
|
|
1354
|
+
},
|
|
1355
|
+
},
|
|
1356
|
+
},
|
|
1357
|
+
},
|
|
1358
|
+
],
|
|
1359
|
+
},
|
|
1360
|
+
};
|
|
1361
|
+
```
|
|
1362
|
+
|
|
1363
|
+
**webpack.config.js**
|
|
1364
|
+
|
|
1365
|
+
```js
|
|
1366
|
+
module.exports = {
|
|
1367
|
+
module: {
|
|
1368
|
+
rules: [
|
|
1369
|
+
{
|
|
1370
|
+
test: /\.css$/i,
|
|
1371
|
+
loader: "css-loader",
|
|
1372
|
+
options: {
|
|
1373
|
+
modules: {
|
|
1374
|
+
exportLocalsConvention: function (name) {
|
|
1375
|
+
return [
|
|
1376
|
+
name.replace(/-/g, "_"),
|
|
1377
|
+
// dashesCamelCase
|
|
1378
|
+
name.replace(/-+(\w)/g, (match, firstLetter) =>
|
|
1379
|
+
firstLetter.toUpperCase(),
|
|
1380
|
+
),
|
|
1381
|
+
];
|
|
1382
|
+
},
|
|
1383
|
+
},
|
|
1384
|
+
},
|
|
1385
|
+
},
|
|
1386
|
+
],
|
|
1387
|
+
},
|
|
1388
|
+
};
|
|
1389
|
+
```
|
|
1390
|
+
|
|
1391
|
+
##### `exportOnlyLocals`
|
|
1392
|
+
|
|
1393
|
+
Type:
|
|
1394
|
+
|
|
1395
|
+
```ts
|
|
1396
|
+
type exportOnlyLocals = boolean;
|
|
1397
|
+
```
|
|
1398
|
+
|
|
1399
|
+
Default: `false`
|
|
1400
|
+
|
|
1401
|
+
Export only locals.
|
|
1402
|
+
|
|
1403
|
+
**Useful** when you use **css modules** for pre-rendering (for example SSR).
|
|
1404
|
+
|
|
1405
|
+
For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
|
|
1406
|
+
|
|
1407
|
+
It doesn't embed CSS; it only exports the identifier mappings.
|
|
1408
|
+
|
|
1409
|
+
**webpack.config.js**
|
|
1410
|
+
|
|
1411
|
+
```js
|
|
1412
|
+
module.exports = {
|
|
1413
|
+
module: {
|
|
1414
|
+
rules: [
|
|
1415
|
+
{
|
|
1416
|
+
test: /\.css$/i,
|
|
1417
|
+
loader: "css-loader",
|
|
1418
|
+
options: {
|
|
1419
|
+
modules: {
|
|
1420
|
+
exportOnlyLocals: true,
|
|
1421
|
+
},
|
|
1422
|
+
},
|
|
1423
|
+
},
|
|
1424
|
+
],
|
|
1425
|
+
},
|
|
1426
|
+
};
|
|
1427
|
+
```
|
|
1428
|
+
|
|
1429
|
+
##### `getJSON`
|
|
1430
|
+
|
|
1431
|
+
Type:
|
|
1432
|
+
|
|
1433
|
+
```ts
|
|
1434
|
+
type getJSON = ({
|
|
1435
|
+
resourcePath,
|
|
1436
|
+
imports,
|
|
1437
|
+
exports,
|
|
1438
|
+
replacements,
|
|
1439
|
+
}: {
|
|
1440
|
+
resourcePath: string;
|
|
1441
|
+
imports: object[];
|
|
1442
|
+
exports: object[];
|
|
1443
|
+
replacements: object[];
|
|
1444
|
+
}) => Promise<void> | void;
|
|
1445
|
+
```
|
|
1446
|
+
|
|
1447
|
+
Default: `undefined`
|
|
1448
|
+
|
|
1449
|
+
Enables a callback to output the CSS modules mapping JSON.
|
|
1450
|
+
|
|
1451
|
+
The callback is invoked with an object containing the following:
|
|
1452
|
+
|
|
1453
|
+
- `resourcePath`: the absolute path of the original resource, e.g., `/foo/bar/baz.module.css`
|
|
1454
|
+
|
|
1455
|
+
- `imports`: an array of import objects with data about import types and file paths, e.g.,
|
|
1456
|
+
|
|
1457
|
+
```json
|
|
1458
|
+
[
|
|
1459
|
+
{
|
|
1460
|
+
"type": "icss_import",
|
|
1461
|
+
"importName": "___CSS_LOADER_ICSS_IMPORT_0___",
|
|
1462
|
+
"url": "\"-!../../../../../node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[4].use[1]!../../../../../node_modules/postcss-loader/dist/cjs.js!../../../../../node_modules/sass-loader/dist/cjs.js!../../../../baz.module.css\"",
|
|
1463
|
+
"icss": true,
|
|
1464
|
+
"index": 0
|
|
1465
|
+
}
|
|
1466
|
+
]
|
|
1467
|
+
```
|
|
1468
|
+
|
|
1469
|
+
(Note that this will include all imports, not just those relevant to CSS Modules.)
|
|
1470
|
+
|
|
1471
|
+
- `exports`: an array of export objects with exported names and values, e.g.,
|
|
1472
|
+
|
|
1473
|
+
```json
|
|
1474
|
+
[
|
|
1475
|
+
{
|
|
1476
|
+
"name": "main",
|
|
1477
|
+
"value": "D2Oy"
|
|
1478
|
+
}
|
|
1479
|
+
]
|
|
1480
|
+
```
|
|
1481
|
+
|
|
1482
|
+
- `replacements`: an array of import replacement objects used for linking `imports` and `exports`, e.g.,
|
|
1483
|
+
|
|
1484
|
+
```json
|
|
1485
|
+
{
|
|
1486
|
+
"replacementName": "___CSS_LOADER_ICSS_IMPORT_0_REPLACEMENT_0___",
|
|
1487
|
+
"importName": "___CSS_LOADER_ICSS_IMPORT_0___",
|
|
1488
|
+
"localName": "main"
|
|
1489
|
+
}
|
|
1490
|
+
```
|
|
1491
|
+
|
|
1492
|
+
Using `getJSON`, it's possible to output a file with all CSS module mappings.
|
|
1493
|
+
|
|
1494
|
+
In the following example, we use `getJSON` to cache canonical mappings and add stand-ins for any composed values (through `composes`), and we use a custom plugin to consolidate the values and output them to a file:
|
|
1495
|
+
|
|
1496
|
+
**webpack.config.js**
|
|
1497
|
+
|
|
1498
|
+
```js
|
|
1499
|
+
const path = require("path");
|
|
1500
|
+
const fs = require("fs");
|
|
1501
|
+
|
|
1502
|
+
const CSS_LOADER_REPLACEMENT_REGEX =
|
|
1503
|
+
/(___CSS_LOADER_ICSS_IMPORT_\d+_REPLACEMENT_\d+___)/g;
|
|
1504
|
+
const REPLACEMENT_REGEX = /___REPLACEMENT\[(.*?)]\[(.*?)]___/g;
|
|
1505
|
+
const IDENTIFIER_REGEX = /\[(.*?)]\[(.*?)]/;
|
|
1506
|
+
const replacementsMap = {};
|
|
1507
|
+
const canonicalValuesMap = {};
|
|
1508
|
+
const allExportsJson = {};
|
|
1509
|
+
|
|
1510
|
+
function generateIdentifier(resourcePath, localName) {
|
|
1511
|
+
return `[${resourcePath}][${localName}]`;
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
function addReplacements(resourcePath, imports, exportsJson, replacements) {
|
|
1515
|
+
const importReplacementsMap = {};
|
|
1516
|
+
|
|
1517
|
+
// create a dict to quickly identify imports and get their absolute stand-in strings in the currently loaded file
|
|
1518
|
+
// e.g., { '___CSS_LOADER_ICSS_IMPORT_0_REPLACEMENT_0___': '___REPLACEMENT[/foo/bar/baz.css][main]___' }
|
|
1519
|
+
importReplacementsMap[resourcePath] = replacements.reduce(
|
|
1520
|
+
(acc, { replacementName, importName, localName }) => {
|
|
1521
|
+
const replacementImportUrl = imports.find(
|
|
1522
|
+
(importData) => importData.importName === importName,
|
|
1523
|
+
).url;
|
|
1524
|
+
const relativePathRe = /.*!(.*)"/;
|
|
1525
|
+
const [, relativePath] = replacementImportUrl.match(relativePathRe);
|
|
1526
|
+
const importPath = path.resolve(path.dirname(resourcePath), relativePath);
|
|
1527
|
+
const identifier = generateIdentifier(importPath, localName);
|
|
1528
|
+
return { ...acc, [replacementName]: `___REPLACEMENT${identifier}___` };
|
|
1529
|
+
},
|
|
1530
|
+
{},
|
|
1531
|
+
);
|
|
1532
|
+
|
|
1533
|
+
// iterate through the raw exports and add stand-in variables
|
|
1534
|
+
// ('___REPLACEMENT[<absolute_path>][<class_name>]___')
|
|
1535
|
+
// to be replaced in the plugin below
|
|
1536
|
+
for (const [localName, classNames] of Object.entries(exportsJson)) {
|
|
1537
|
+
const identifier = generateIdentifier(resourcePath, localName);
|
|
1538
|
+
|
|
1539
|
+
if (CSS_LOADER_REPLACEMENT_REGEX.test(classNames)) {
|
|
1540
|
+
// if there are any replacements needed in the concatenated class names,
|
|
1541
|
+
// add them all to the replacements map to be replaced altogether later
|
|
1542
|
+
replacementsMap[identifier] = classNames.replaceAll(
|
|
1543
|
+
CSS_LOADER_REPLACEMENT_REGEX,
|
|
1544
|
+
(_, replacementName) =>
|
|
1545
|
+
importReplacementsMap[resourcePath][replacementName],
|
|
1546
|
+
);
|
|
1547
|
+
} else {
|
|
1548
|
+
// otherwise, no class names need replacements so we can add them to
|
|
1549
|
+
// canonical values map and all exports JSON verbatim
|
|
1550
|
+
canonicalValuesMap[identifier] = classNames;
|
|
1551
|
+
|
|
1552
|
+
allExportsJson[resourcePath] = allExportsJson[resourcePath] || {};
|
|
1553
|
+
allExportsJson[resourcePath][localName] = classNames;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
function replaceReplacements(classNames) {
|
|
1559
|
+
return classNames.replaceAll(
|
|
1560
|
+
REPLACEMENT_REGEX,
|
|
1561
|
+
(_, resourcePath, localName) => {
|
|
1562
|
+
const identifier = generateIdentifier(resourcePath, localName);
|
|
1563
|
+
|
|
1564
|
+
if (identifier in canonicalValuesMap) {
|
|
1565
|
+
return canonicalValuesMap[identifier];
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
// Recurse through other stand-in that may be imports
|
|
1569
|
+
const canonicalValue = replaceReplacements(replacementsMap[identifier]);
|
|
1570
|
+
|
|
1571
|
+
canonicalValuesMap[identifier] = canonicalValue;
|
|
1572
|
+
|
|
1573
|
+
return canonicalValue;
|
|
1574
|
+
},
|
|
1575
|
+
);
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
function getJSON({ resourcePath, imports, exports, replacements }) {
|
|
1579
|
+
const exportsJson = exports.reduce((acc, { name, value }) => {
|
|
1580
|
+
return { ...acc, [name]: value };
|
|
1581
|
+
}, {});
|
|
1582
|
+
|
|
1583
|
+
if (replacements.length > 0) {
|
|
1584
|
+
// replacements present --> add stand-in values for absolute paths and local names,
|
|
1585
|
+
// which will be resolved to their canonical values in the plugin below
|
|
1586
|
+
addReplacements(resourcePath, imports, exportsJson, replacements);
|
|
1587
|
+
} else {
|
|
1588
|
+
// no replacements present --> add to canonicalValuesMap verbatim
|
|
1589
|
+
// since all values here are canonical/don't need resolution
|
|
1590
|
+
for (const [key, value] of Object.entries(exportsJson)) {
|
|
1591
|
+
const id = `[${resourcePath}][${key}]`;
|
|
1592
|
+
|
|
1593
|
+
canonicalValuesMap[id] = value;
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
allExportsJson[resourcePath] = exportsJson;
|
|
1597
|
+
}
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
class CssModulesJsonPlugin {
|
|
1601
|
+
constructor(options) {
|
|
1602
|
+
this.options = options;
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
// eslint-disable-next-line class-methods-use-this
|
|
1606
|
+
apply(compiler) {
|
|
1607
|
+
compiler.hooks.emit.tap("CssModulesJsonPlugin", () => {
|
|
1608
|
+
for (const [identifier, classNames] of Object.entries(replacementsMap)) {
|
|
1609
|
+
const adjustedClassNames = replaceReplacements(classNames);
|
|
1610
|
+
|
|
1611
|
+
replacementsMap[identifier] = adjustedClassNames;
|
|
1612
|
+
|
|
1613
|
+
const [, resourcePath, localName] = identifier.match(IDENTIFIER_REGEX);
|
|
1614
|
+
|
|
1615
|
+
allExportsJson[resourcePath] = allExportsJson[resourcePath] || {};
|
|
1616
|
+
allExportsJson[resourcePath][localName] = adjustedClassNames;
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
fs.writeFileSync(
|
|
1620
|
+
this.options.filepath,
|
|
1621
|
+
JSON.stringify(
|
|
1622
|
+
// Make path to be relative to `context` (your project root)
|
|
1623
|
+
Object.fromEntries(
|
|
1624
|
+
Object.entries(allExportsJson).map((key) => {
|
|
1625
|
+
key[0] = path
|
|
1626
|
+
.relative(compiler.context, key[0])
|
|
1627
|
+
.replace(/\\/g, "/");
|
|
1628
|
+
|
|
1629
|
+
return key;
|
|
1630
|
+
}),
|
|
1631
|
+
),
|
|
1632
|
+
null,
|
|
1633
|
+
2,
|
|
1634
|
+
),
|
|
1635
|
+
"utf8",
|
|
1636
|
+
);
|
|
1637
|
+
});
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
module.exports = {
|
|
1642
|
+
module: {
|
|
1643
|
+
rules: [
|
|
1644
|
+
{
|
|
1645
|
+
test: /\.css$/i,
|
|
1646
|
+
loader: "css-loader",
|
|
1647
|
+
options: { modules: { getJSON } },
|
|
1648
|
+
},
|
|
1649
|
+
],
|
|
1650
|
+
},
|
|
1651
|
+
plugins: [
|
|
1652
|
+
new CssModulesJsonPlugin({
|
|
1653
|
+
filepath: path.resolve(__dirname, "./output.css.json"),
|
|
1654
|
+
}),
|
|
1655
|
+
],
|
|
1656
|
+
};
|
|
1657
|
+
```
|
|
1658
|
+
|
|
1659
|
+
In the above, all import aliases are replaced with `___REPLACEMENT[<resourcePath>][<localName>]___` in `getJSON`, and they're resolved in the custom plugin. All CSS mappings are contained in `allExportsJson`:
|
|
1660
|
+
|
|
1661
|
+
```json
|
|
1662
|
+
{
|
|
1663
|
+
"foo/bar/baz.module.css": {
|
|
1664
|
+
"main": "D2Oy",
|
|
1665
|
+
"header": "thNN"
|
|
1666
|
+
},
|
|
1667
|
+
"foot/bear/bath.module.css": {
|
|
1668
|
+
"logo": "sqiR",
|
|
1669
|
+
"info": "XMyI"
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
```
|
|
1673
|
+
|
|
1674
|
+
This is saved to a local file named `output.css.json`.
|
|
1675
|
+
|
|
1676
|
+
### `importLoaders`
|
|
1677
|
+
|
|
1678
|
+
Type:
|
|
1679
|
+
|
|
1680
|
+
```ts
|
|
1681
|
+
type importLoaders = number;
|
|
1682
|
+
```
|
|
1683
|
+
|
|
1684
|
+
Default: `0`
|
|
1685
|
+
|
|
1686
|
+
Allows to enables/disables or sets up the number of loaders applied before CSS loader for `@import` at-rules, CSS Modules and ICSS imports, i.e. `@import`/`composes`/`@value value from './values.css'`/etc.
|
|
1687
|
+
|
|
1688
|
+
The option `importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources and CSS Modules/ICSS imports.
|
|
1689
|
+
|
|
1690
|
+
**webpack.config.js**
|
|
1691
|
+
|
|
1692
|
+
```js
|
|
1693
|
+
module.exports = {
|
|
1694
|
+
module: {
|
|
1695
|
+
rules: [
|
|
1696
|
+
{
|
|
1697
|
+
test: /\.css$/i,
|
|
1698
|
+
use: [
|
|
1699
|
+
"style-loader",
|
|
1700
|
+
{
|
|
1701
|
+
loader: "css-loader",
|
|
1702
|
+
options: {
|
|
1703
|
+
importLoaders: 2,
|
|
1704
|
+
// 0 => no loaders (default);
|
|
1705
|
+
// 1 => postcss-loader;
|
|
1706
|
+
// 2 => postcss-loader, sass-loader
|
|
1707
|
+
},
|
|
1708
|
+
},
|
|
1709
|
+
"postcss-loader",
|
|
1710
|
+
"sass-loader",
|
|
1711
|
+
],
|
|
1712
|
+
},
|
|
1713
|
+
],
|
|
1714
|
+
},
|
|
1715
|
+
};
|
|
1716
|
+
```
|
|
1717
|
+
|
|
1718
|
+
This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
|
|
1719
|
+
|
|
1720
|
+
### `sourceMap`
|
|
1721
|
+
|
|
1722
|
+
Type:
|
|
1723
|
+
|
|
1724
|
+
```ts
|
|
1725
|
+
type sourceMap = boolean;
|
|
1726
|
+
```
|
|
1727
|
+
|
|
1728
|
+
Default: depends on the `compiler.devtool` value
|
|
1729
|
+
|
|
1730
|
+
By default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option. All values enable source map generation except `eval` and `false` values.
|
|
1731
|
+
|
|
1732
|
+
**webpack.config.js**
|
|
1733
|
+
|
|
1734
|
+
```js
|
|
1735
|
+
module.exports = {
|
|
1736
|
+
module: {
|
|
1737
|
+
rules: [
|
|
1738
|
+
{
|
|
1739
|
+
test: /\.css$/i,
|
|
1740
|
+
loader: "css-loader",
|
|
1741
|
+
options: {
|
|
1742
|
+
sourceMap: true,
|
|
1743
|
+
},
|
|
1744
|
+
},
|
|
1745
|
+
],
|
|
1746
|
+
},
|
|
1747
|
+
};
|
|
1748
|
+
```
|
|
1749
|
+
|
|
1750
|
+
### `esModule`
|
|
1751
|
+
|
|
1752
|
+
Type:
|
|
1753
|
+
|
|
1754
|
+
```ts
|
|
1755
|
+
type esModule = boolean;
|
|
1756
|
+
```
|
|
1757
|
+
|
|
1758
|
+
Default: `true`
|
|
1759
|
+
|
|
1760
|
+
By default, `css-loader` generates JS modules that use the ES modules syntax.
|
|
1761
|
+
|
|
1762
|
+
There are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
|
|
1763
|
+
|
|
1764
|
+
You can enable CommonJS module syntax using:
|
|
1765
|
+
|
|
1766
|
+
**webpack.config.js**
|
|
1767
|
+
|
|
1768
|
+
```js
|
|
1769
|
+
module.exports = {
|
|
1770
|
+
module: {
|
|
1771
|
+
rules: [
|
|
1772
|
+
{
|
|
1773
|
+
test: /\.css$/i,
|
|
1774
|
+
loader: "css-loader",
|
|
1775
|
+
options: {
|
|
1776
|
+
esModule: false,
|
|
1777
|
+
},
|
|
1778
|
+
},
|
|
1779
|
+
],
|
|
1780
|
+
},
|
|
1781
|
+
};
|
|
1782
|
+
```
|
|
1783
|
+
|
|
1784
|
+
### `exportType`
|
|
1785
|
+
|
|
1786
|
+
Type:
|
|
1787
|
+
|
|
1788
|
+
```ts
|
|
1789
|
+
type exportType = "array" | "string" | "css-style-sheet";
|
|
1790
|
+
```
|
|
1791
|
+
|
|
1792
|
+
Default: `'array'`
|
|
1793
|
+
|
|
1794
|
+
Allows exporting styles as array with modules, string or [constructable stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) (i.e. [`CSSStyleSheet`](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet)).
|
|
1795
|
+
|
|
1796
|
+
The default value is `'array'`, i.e. loader exports an array of modules with a specific API which is used in `style-loader` or other.
|
|
1797
|
+
|
|
1798
|
+
**webpack.config.js**
|
|
1799
|
+
|
|
1800
|
+
```js
|
|
1801
|
+
module.exports = {
|
|
1802
|
+
module: {
|
|
1803
|
+
rules: [
|
|
1804
|
+
{
|
|
1805
|
+
assert: { type: "css" },
|
|
1806
|
+
loader: "css-loader",
|
|
1807
|
+
options: {
|
|
1808
|
+
exportType: "css-style-sheet",
|
|
1809
|
+
},
|
|
1810
|
+
},
|
|
1811
|
+
],
|
|
1812
|
+
},
|
|
1813
|
+
};
|
|
1814
|
+
```
|
|
1815
|
+
|
|
1816
|
+
**src/index.js**
|
|
1817
|
+
|
|
1818
|
+
```js
|
|
1819
|
+
import sheet from "./styles.css" assert { type: "css" };
|
|
1820
|
+
|
|
1821
|
+
document.adoptedStyleSheets = [sheet];
|
|
1822
|
+
shadowRoot.adoptedStyleSheets = [sheet];
|
|
1823
|
+
```
|
|
1824
|
+
|
|
1825
|
+
#### `'array'`
|
|
1826
|
+
|
|
1827
|
+
The default export is array of modules with specific API which is used in `style-loader` or other.
|
|
1828
|
+
|
|
1829
|
+
**webpack.config.js**
|
|
1830
|
+
|
|
1831
|
+
```js
|
|
1832
|
+
module.exports = {
|
|
1833
|
+
module: {
|
|
1834
|
+
rules: [
|
|
1835
|
+
{
|
|
1836
|
+
test: /\.(sa|sc|c)ss$/i,
|
|
1837
|
+
use: ["style-loader", "css-loader", "postcss-loader", "sass-loader"],
|
|
1838
|
+
},
|
|
1839
|
+
],
|
|
1840
|
+
},
|
|
1841
|
+
};
|
|
1842
|
+
```
|
|
1843
|
+
|
|
1844
|
+
**src/index.js**
|
|
1845
|
+
|
|
1846
|
+
```js
|
|
1847
|
+
// `style-loader` applies styles to DOM
|
|
1848
|
+
import "./styles.css";
|
|
1849
|
+
```
|
|
1850
|
+
|
|
1851
|
+
#### `'string'`
|
|
1852
|
+
|
|
1853
|
+
> [!WARNING]
|
|
1854
|
+
>
|
|
1855
|
+
> You should not use [`style-loader`](https://github.com/webpack/style-loader) or [`mini-css-extract-plugin`](https://github.com/webpack/mini-css-extract-plugin) with this value.
|
|
1856
|
+
>
|
|
1857
|
+
> The `esModule` option should be enabled if you want to use it with [`CSS modules`](https://github.com/webpack/css-loader#modules). By default for locals [named export](https://github.com/webpack/css-loader#namedexport) will be used.
|
|
1858
|
+
|
|
1859
|
+
The default export is `string`.
|
|
1860
|
+
|
|
1861
|
+
**webpack.config.js**
|
|
1862
|
+
|
|
1863
|
+
```js
|
|
1864
|
+
module.exports = {
|
|
1865
|
+
module: {
|
|
1866
|
+
rules: [
|
|
1867
|
+
{
|
|
1868
|
+
test: /\.(sa|sc|c)ss$/i,
|
|
1869
|
+
use: ["css-loader", "postcss-loader", "sass-loader"],
|
|
1870
|
+
},
|
|
1871
|
+
],
|
|
1872
|
+
},
|
|
1873
|
+
};
|
|
1874
|
+
```
|
|
1875
|
+
|
|
1876
|
+
**src/index.js**
|
|
1877
|
+
|
|
1878
|
+
```js
|
|
1879
|
+
import sheet from "./styles.css";
|
|
1880
|
+
|
|
1881
|
+
console.log(sheet);
|
|
1882
|
+
```
|
|
1883
|
+
|
|
1884
|
+
#### `'css-style-sheet'`
|
|
1885
|
+
|
|
1886
|
+
> [!WARNING]
|
|
1887
|
+
>
|
|
1888
|
+
> `@import` rules not yet allowed, more [information](https://web.dev/css-module-scripts/#@import-rules-not-yet-allowed)
|
|
1889
|
+
|
|
1890
|
+
> [!WARNING]
|
|
1891
|
+
>
|
|
1892
|
+
> You don't need [`style-loader`](https://github.com/webpack/style-loader) anymore, please remove it.
|
|
1893
|
+
|
|
1894
|
+
> [!WARNING]
|
|
1895
|
+
>
|
|
1896
|
+
> The `esModule` option should be enabled if you want to use it with [`CSS modules`](https://github.com/webpack/css-loader#modules). By default for locals [named export](https://github.com/webpack/css-loader#namedexport) will be used.
|
|
1897
|
+
|
|
1898
|
+
> [!WARNING]
|
|
1899
|
+
>
|
|
1900
|
+
> Source maps are not currently supported in `Chrome` due to a [bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1174094&q=CSSStyleSheet%20source%20maps&can=2)
|
|
1901
|
+
|
|
1902
|
+
The default export is a [constructable stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) (i.e. [`CSSStyleSheet`](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet)).
|
|
1903
|
+
|
|
1904
|
+
Useful for [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) and shadow DOM.
|
|
1905
|
+
|
|
1906
|
+
More information:
|
|
1907
|
+
|
|
1908
|
+
- [Using CSS Module Scripts to import stylesheets](https://web.dev/css-module-scripts/)
|
|
1909
|
+
- [Constructable Stylesheets: seamless reusable styles](https://developers.google.com/web/updates/2019/02/constructable-stylesheets)
|
|
1910
|
+
|
|
1911
|
+
**webpack.config.js**
|
|
1912
|
+
|
|
1913
|
+
```js
|
|
1914
|
+
module.exports = {
|
|
1915
|
+
module: {
|
|
1916
|
+
rules: [
|
|
1917
|
+
{
|
|
1918
|
+
assert: { type: "css" },
|
|
1919
|
+
loader: "css-loader",
|
|
1920
|
+
options: {
|
|
1921
|
+
exportType: "css-style-sheet",
|
|
1922
|
+
},
|
|
1923
|
+
},
|
|
1924
|
+
|
|
1925
|
+
// For Sass/SCSS:
|
|
1926
|
+
//
|
|
1927
|
+
// {
|
|
1928
|
+
// assert: { type: "css" },
|
|
1929
|
+
// rules: [
|
|
1930
|
+
// {
|
|
1931
|
+
// loader: "css-loader",
|
|
1932
|
+
// options: {
|
|
1933
|
+
// exportType: "css-style-sheet",
|
|
1934
|
+
// // Other options
|
|
1935
|
+
// },
|
|
1936
|
+
// },
|
|
1937
|
+
// {
|
|
1938
|
+
// loader: "sass-loader",
|
|
1939
|
+
// options: {
|
|
1940
|
+
// // Other options
|
|
1941
|
+
// },
|
|
1942
|
+
// },
|
|
1943
|
+
// ],
|
|
1944
|
+
// },
|
|
1945
|
+
],
|
|
1946
|
+
},
|
|
1947
|
+
};
|
|
1948
|
+
```
|
|
1949
|
+
|
|
1950
|
+
**src/index.js**
|
|
1951
|
+
|
|
1952
|
+
```js
|
|
1953
|
+
// Example for Sass/SCSS:
|
|
1954
|
+
// import sheet from "./styles.scss" assert { type: "css" };
|
|
1955
|
+
|
|
1956
|
+
// Example for CSS modules:
|
|
1957
|
+
// import sheet, { myClass } from "./styles.scss" assert { type: "css" };
|
|
1958
|
+
|
|
1959
|
+
// Example for CSS:
|
|
1960
|
+
import sheet from "./styles.css" assert { type: "css" };
|
|
1961
|
+
|
|
1962
|
+
document.adoptedStyleSheets = [sheet];
|
|
1963
|
+
shadowRoot.adoptedStyleSheets = [sheet];
|
|
1964
|
+
```
|
|
1965
|
+
|
|
1966
|
+
For migration purposes, you can use the following configuration:
|
|
1967
|
+
|
|
1968
|
+
```js
|
|
1969
|
+
module.exports = {
|
|
1970
|
+
module: {
|
|
1971
|
+
rules: [
|
|
1972
|
+
{
|
|
1973
|
+
test: /\.css$/i,
|
|
1974
|
+
oneOf: [
|
|
1975
|
+
{
|
|
1976
|
+
assert: { type: "css" },
|
|
1977
|
+
loader: "css-loader",
|
|
1978
|
+
options: {
|
|
1979
|
+
exportType: "css-style-sheet",
|
|
1980
|
+
// Other options
|
|
1981
|
+
},
|
|
1982
|
+
},
|
|
1983
|
+
{
|
|
1984
|
+
use: [
|
|
1985
|
+
"style-loader",
|
|
1986
|
+
{
|
|
1987
|
+
loader: "css-loader",
|
|
1988
|
+
options: {
|
|
1989
|
+
// Other options
|
|
1990
|
+
},
|
|
1991
|
+
},
|
|
1992
|
+
],
|
|
1993
|
+
},
|
|
1994
|
+
],
|
|
1995
|
+
},
|
|
1996
|
+
],
|
|
1997
|
+
},
|
|
1998
|
+
};
|
|
1999
|
+
```
|
|
2000
|
+
|
|
2001
|
+
## Examples
|
|
2002
|
+
|
|
2003
|
+
### Recommend
|
|
2004
|
+
|
|
2005
|
+
For `production` builds, it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.
|
|
2006
|
+
|
|
2007
|
+
This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack/mini-css-extract-plugin), because it creates separate css files.
|
|
2008
|
+
|
|
2009
|
+
For `development` mode (including `webpack-dev-server`) you can use [style-loader](https://github.com/webpack/style-loader), because it injects CSS into the DOM using multiple `<style></style>` and works faster.
|
|
2010
|
+
|
|
2011
|
+
> [!NOTE]
|
|
2012
|
+
>
|
|
2013
|
+
> Do not use `style-loader` and `mini-css-extract-plugin` together.
|
|
2014
|
+
|
|
2015
|
+
**webpack.config.js**
|
|
2016
|
+
|
|
2017
|
+
```js
|
|
2018
|
+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
|
|
2019
|
+
const devMode = process.env.NODE_ENV !== "production";
|
|
2020
|
+
|
|
2021
|
+
module.exports = {
|
|
2022
|
+
module: {
|
|
2023
|
+
rules: [
|
|
2024
|
+
{
|
|
2025
|
+
// If you enable `experiments.css` or `experiments.futureDefaults`, please uncomment line below
|
|
2026
|
+
// type: "javascript/auto",
|
|
2027
|
+
test: /\.(sa|sc|c)ss$/i,
|
|
2028
|
+
use: [
|
|
2029
|
+
devMode ? "style-loader" : MiniCssExtractPlugin.loader,
|
|
2030
|
+
"css-loader",
|
|
2031
|
+
"postcss-loader",
|
|
2032
|
+
"sass-loader",
|
|
2033
|
+
],
|
|
2034
|
+
},
|
|
2035
|
+
],
|
|
2036
|
+
},
|
|
2037
|
+
plugins: [].concat(devMode ? [] : [new MiniCssExtractPlugin()]),
|
|
2038
|
+
};
|
|
2039
|
+
```
|
|
2040
|
+
|
|
2041
|
+
### Disable URL resolving using the `/* webpackIgnore: true */` comment
|
|
2042
|
+
|
|
2043
|
+
With the help of the `/* webpackIgnore: true */`comment, it is possible to disable sources handling for rules and for individual declarations.
|
|
2044
|
+
|
|
2045
|
+
```css
|
|
2046
|
+
/* webpackIgnore: true */
|
|
2047
|
+
@import url(./basic.css);
|
|
2048
|
+
@import /* webpackIgnore: true */ url(./imported.css);
|
|
2049
|
+
|
|
2050
|
+
.class {
|
|
2051
|
+
/* Disabled url handling for the all urls in the 'background' declaration */
|
|
2052
|
+
color: red;
|
|
2053
|
+
/* webpackIgnore: true */
|
|
2054
|
+
background: url("./url/img.png"), url("./url/img.png");
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2057
|
+
.class {
|
|
2058
|
+
/* Disabled url handling for the first url in the 'background' declaration */
|
|
2059
|
+
color: red;
|
|
2060
|
+
background:
|
|
2061
|
+
/* webpackIgnore: true */ url("./url/img.png"), url("./url/img.png");
|
|
2062
|
+
}
|
|
2063
|
+
|
|
2064
|
+
.class {
|
|
2065
|
+
/* Disabled url handling for the second url in the 'background' declaration */
|
|
2066
|
+
color: red;
|
|
2067
|
+
background:
|
|
2068
|
+
url("./url/img.png"),
|
|
2069
|
+
/* webpackIgnore: true */ url("./url/img.png");
|
|
2070
|
+
}
|
|
2071
|
+
|
|
2072
|
+
/* prettier-ignore */
|
|
2073
|
+
.class {
|
|
2074
|
+
/* Disabled url handling for the second url in the 'background' declaration */
|
|
2075
|
+
color: red;
|
|
2076
|
+
background: url("./url/img.png"),
|
|
2077
|
+
/* webpackIgnore: true */
|
|
2078
|
+
url("./url/img.png");
|
|
2079
|
+
}
|
|
2080
|
+
|
|
2081
|
+
/* prettier-ignore */
|
|
2082
|
+
.class {
|
|
2083
|
+
/* Disabled url handling for third and sixth urls in the 'background-image' declaration */
|
|
2084
|
+
background-image: image-set(
|
|
2085
|
+
url(./url/img.png) 2x,
|
|
2086
|
+
url(./url/img.png) 3x,
|
|
2087
|
+
/* webpackIgnore: true */ url(./url/img.png) 4x,
|
|
2088
|
+
url(./url/img.png) 5x,
|
|
2089
|
+
url(./url/img.png) 6x,
|
|
2090
|
+
/* webpackIgnore: true */
|
|
2091
|
+
url(./url/img.png) 7x
|
|
2092
|
+
);
|
|
2093
|
+
}
|
|
2094
|
+
```
|
|
2095
|
+
|
|
2096
|
+
### Assets
|
|
2097
|
+
|
|
2098
|
+
The following `webpack.config.js` can load CSS files, embed small PNG/JPG/GIF/SVG images as well as fonts as [Data URLs](https://tools.ietf.org/html/rfc2397) and copy larger files to the output directory.
|
|
2099
|
+
|
|
2100
|
+
**For webpack v5:**
|
|
2101
|
+
|
|
2102
|
+
**webpack.config.js**
|
|
2103
|
+
|
|
2104
|
+
```js
|
|
2105
|
+
module.exports = {
|
|
2106
|
+
module: {
|
|
2107
|
+
rules: [
|
|
2108
|
+
{
|
|
2109
|
+
test: /\.css$/i,
|
|
2110
|
+
use: ["style-loader", "css-loader"],
|
|
2111
|
+
},
|
|
2112
|
+
{
|
|
2113
|
+
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
|
|
2114
|
+
// More information here https://webpack.js.org/guides/asset-modules/
|
|
2115
|
+
type: "asset",
|
|
2116
|
+
},
|
|
2117
|
+
],
|
|
2118
|
+
},
|
|
2119
|
+
};
|
|
2120
|
+
```
|
|
2121
|
+
|
|
2122
|
+
### Extract
|
|
2123
|
+
|
|
2124
|
+
For production builds it's recommended to extract the CSS from your bundle to enable parallel loading of CSS/JS resources later on.
|
|
2125
|
+
|
|
2126
|
+
- This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack/mini-css-extract-plugin) to extract the CSS when running in production mode.
|
|
2127
|
+
|
|
2128
|
+
- As an alternative, if seeking better development performance and css outputs that mimic production. [extract-css-chunks-webpack-plugin](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin) offers a hot module reload friendly, extended version of mini-css-extract-plugin. HMR real CSS files in dev, works like mini-css in non-dev.
|
|
2129
|
+
|
|
2130
|
+
### Pure CSS, CSS Modules and PostCSS
|
|
2131
|
+
|
|
2132
|
+
When you have pure CSS (without CSS modules), CSS modules and PostCSS in your project, you can use this setup:
|
|
2133
|
+
|
|
2134
|
+
**webpack.config.js**
|
|
2135
|
+
|
|
2136
|
+
```js
|
|
2137
|
+
module.exports = {
|
|
2138
|
+
module: {
|
|
2139
|
+
rules: [
|
|
2140
|
+
{
|
|
2141
|
+
// For pure CSS - /\.css$/i,
|
|
2142
|
+
// For Sass/SCSS - /\.((c|sa|sc)ss)$/i,
|
|
2143
|
+
// For Less - /\.((c|le)ss)$/i,
|
|
2144
|
+
test: /\.((c|sa|sc)ss)$/i,
|
|
2145
|
+
use: [
|
|
2146
|
+
"style-loader",
|
|
2147
|
+
{
|
|
2148
|
+
loader: "css-loader",
|
|
2149
|
+
options: {
|
|
2150
|
+
// Run `postcss-loader` on each CSS `@import` and CSS modules/ICSS imports, do not forget that `sass-loader` compile non CSS `@import`'s into a single file
|
|
2151
|
+
// If you need run `sass-loader` and `postcss-loader` on each CSS `@import` please set it to `2`
|
|
2152
|
+
importLoaders: 1,
|
|
2153
|
+
},
|
|
2154
|
+
},
|
|
2155
|
+
{
|
|
2156
|
+
loader: "postcss-loader",
|
|
2157
|
+
options: { plugins: () => [postcssPresetEnv({ stage: 0 })] },
|
|
2158
|
+
},
|
|
2159
|
+
// Can be `less-loader`
|
|
2160
|
+
{
|
|
2161
|
+
loader: "sass-loader",
|
|
2162
|
+
},
|
|
2163
|
+
],
|
|
2164
|
+
},
|
|
2165
|
+
// For webpack v5
|
|
2166
|
+
{
|
|
2167
|
+
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
|
|
2168
|
+
// More information here https://webpack.js.org/guides/asset-modules/
|
|
2169
|
+
type: "asset",
|
|
2170
|
+
},
|
|
2171
|
+
],
|
|
2172
|
+
},
|
|
2173
|
+
};
|
|
2174
|
+
```
|
|
2175
|
+
|
|
2176
|
+
### Resolve unresolved URLs using an alias
|
|
2177
|
+
|
|
2178
|
+
**index.css**
|
|
2179
|
+
|
|
2180
|
+
```css
|
|
2181
|
+
.class {
|
|
2182
|
+
background: url(/assets/unresolved/img.png);
|
|
2183
|
+
}
|
|
2184
|
+
```
|
|
2185
|
+
|
|
2186
|
+
**webpack.config.js**
|
|
2187
|
+
|
|
2188
|
+
```js
|
|
2189
|
+
module.exports = {
|
|
2190
|
+
module: {
|
|
2191
|
+
rules: [
|
|
2192
|
+
{
|
|
2193
|
+
test: /\.css$/i,
|
|
2194
|
+
use: ["style-loader", "css-loader"],
|
|
2195
|
+
},
|
|
2196
|
+
],
|
|
2197
|
+
},
|
|
2198
|
+
resolve: {
|
|
2199
|
+
alias: {
|
|
2200
|
+
"/assets/unresolved/img.png": path.resolve(
|
|
2201
|
+
__dirname,
|
|
2202
|
+
"assets/real-path-to-img/img.png",
|
|
2203
|
+
),
|
|
2204
|
+
},
|
|
2205
|
+
},
|
|
2206
|
+
};
|
|
2207
|
+
```
|
|
2208
|
+
|
|
2209
|
+
### Named export with custom export names
|
|
2210
|
+
|
|
2211
|
+
**webpack.config.js**
|
|
2212
|
+
|
|
2213
|
+
```js
|
|
2214
|
+
module.exports = {
|
|
2215
|
+
module: {
|
|
2216
|
+
rules: [
|
|
2217
|
+
{
|
|
2218
|
+
test: /\.css$/i,
|
|
2219
|
+
loader: "css-loader",
|
|
2220
|
+
options: {
|
|
2221
|
+
modules: {
|
|
2222
|
+
namedExport: true,
|
|
2223
|
+
exportLocalsConvention: function (name) {
|
|
2224
|
+
return name.replace(/-/g, "_");
|
|
2225
|
+
},
|
|
2226
|
+
},
|
|
2227
|
+
},
|
|
2228
|
+
},
|
|
2229
|
+
],
|
|
2230
|
+
},
|
|
2231
|
+
};
|
|
2232
|
+
```
|
|
2233
|
+
|
|
2234
|
+
### Separating `Interoperable CSS`-only and `CSS Module` features
|
|
2235
|
+
|
|
2236
|
+
The following setup is an example of allowing `Interoperable CSS` features only (such as `:import` and `:export`) without using further `CSS Module` functionality by setting the `mode` option for all files that do not match the `*.module.scss` naming convention. This is for reference, as having `ICSS` features applied to all files was default `css-loader` behavior before v4.
|
|
2237
|
+
|
|
2238
|
+
Meanwhile, all files matching `*.module.scss` are treated as `CSS Modules` in this example.
|
|
2239
|
+
|
|
2240
|
+
An example case is assumed where a project requires canvas drawing variables to be synchronized with CSS - canvas drawing uses the same color (set by color name in JavaScript) as HTML background (set by class name in CSS).
|
|
2241
|
+
|
|
2242
|
+
**webpack.config.js**
|
|
2243
|
+
|
|
2244
|
+
```js
|
|
2245
|
+
module.exports = {
|
|
2246
|
+
module: {
|
|
2247
|
+
rules: [
|
|
2248
|
+
// ...
|
|
2249
|
+
// --------
|
|
2250
|
+
// SCSS ALL EXCEPT MODULES
|
|
2251
|
+
{
|
|
2252
|
+
test: /\.scss$/i,
|
|
2253
|
+
exclude: /\.module\.scss$/i,
|
|
2254
|
+
use: [
|
|
2255
|
+
{
|
|
2256
|
+
loader: "style-loader",
|
|
2257
|
+
},
|
|
2258
|
+
{
|
|
2259
|
+
loader: "css-loader",
|
|
2260
|
+
options: {
|
|
2261
|
+
importLoaders: 1,
|
|
2262
|
+
modules: {
|
|
2263
|
+
mode: "icss",
|
|
2264
|
+
},
|
|
2265
|
+
},
|
|
2266
|
+
},
|
|
2267
|
+
{
|
|
2268
|
+
loader: "sass-loader",
|
|
2269
|
+
},
|
|
2270
|
+
],
|
|
2271
|
+
},
|
|
2272
|
+
// --------
|
|
2273
|
+
// SCSS MODULES
|
|
2274
|
+
{
|
|
2275
|
+
test: /\.module\.scss$/i,
|
|
2276
|
+
use: [
|
|
2277
|
+
{
|
|
2278
|
+
loader: "style-loader",
|
|
2279
|
+
},
|
|
2280
|
+
{
|
|
2281
|
+
loader: "css-loader",
|
|
2282
|
+
options: {
|
|
2283
|
+
importLoaders: 1,
|
|
2284
|
+
modules: {
|
|
2285
|
+
mode: "local",
|
|
2286
|
+
},
|
|
2287
|
+
},
|
|
2288
|
+
},
|
|
2289
|
+
{
|
|
2290
|
+
loader: "sass-loader",
|
|
2291
|
+
},
|
|
2292
|
+
],
|
|
2293
|
+
},
|
|
2294
|
+
// --------
|
|
2295
|
+
// ...
|
|
2296
|
+
],
|
|
2297
|
+
},
|
|
2298
|
+
};
|
|
2299
|
+
```
|
|
2300
|
+
|
|
2301
|
+
**variables.scss**
|
|
2302
|
+
|
|
2303
|
+
File treated as `ICSS`-only.
|
|
2304
|
+
|
|
2305
|
+
```scss
|
|
2306
|
+
$colorBackground: red;
|
|
2307
|
+
:export {
|
|
2308
|
+
colorBackgroundCanvas: $colorBackground;
|
|
2309
|
+
}
|
|
2310
|
+
```
|
|
2311
|
+
|
|
2312
|
+
**Component.module.scss**
|
|
2313
|
+
|
|
2314
|
+
File treated as `CSS Module`.
|
|
2315
|
+
|
|
2316
|
+
```scss
|
|
2317
|
+
@import "variables.scss";
|
|
2318
|
+
.componentClass {
|
|
2319
|
+
background-color: $colorBackground;
|
|
2320
|
+
}
|
|
2321
|
+
```
|
|
2322
|
+
|
|
2323
|
+
**Component.jsx**
|
|
2324
|
+
|
|
2325
|
+
Using both `CSS Module` functionality as well as SCSS variables directly in JavaScript.
|
|
2326
|
+
|
|
2327
|
+
```jsx
|
|
2328
|
+
import * as svars from "variables.scss";
|
|
2329
|
+
import * as styles from "Component.module.scss";
|
|
2330
|
+
|
|
2331
|
+
// Render DOM with CSS modules class name
|
|
2332
|
+
// <div className={styles.componentClass}>
|
|
2333
|
+
// <canvas ref={mountsCanvas}/>
|
|
2334
|
+
// </div>
|
|
2335
|
+
|
|
2336
|
+
// Somewhere in JavaScript canvas drawing code use the variable directly
|
|
2337
|
+
// const ctx = mountsCanvas.current.getContext('2d',{alpha: false});
|
|
2338
|
+
ctx.fillStyle = `${svars.colorBackgroundCanvas}`;
|
|
2339
|
+
```
|
|
2340
|
+
|
|
2341
|
+
## Contributing
|
|
2342
|
+
|
|
2343
|
+
We welcome all contributions!
|
|
2344
|
+
|
|
2345
|
+
If you are new here, please take a moment to review our contributing guidelines before submitting issues or pull requests.
|
|
2346
|
+
|
|
2347
|
+
[CONTRIBUTING](https://github.com/webpack/css-loader?tab=contributing-ov-file#contributing)
|
|
2348
|
+
|
|
2349
|
+
## License
|
|
2350
|
+
|
|
2351
|
+
[MIT](./LICENSE)
|
|
2352
|
+
|
|
2353
|
+
[npm]: https://img.shields.io/npm/v/css-loader.svg
|
|
2354
|
+
[npm-url]: https://npmjs.com/package/css-loader
|
|
2355
|
+
[node]: https://img.shields.io/node/v/css-loader.svg
|
|
2356
|
+
[node-url]: https://nodejs.org
|
|
2357
|
+
[tests]: https://github.com/webpack/css-loader/workflows/css-loader/badge.svg
|
|
2358
|
+
[tests-url]: https://github.com/webpack/css-loader/actions
|
|
2359
|
+
[cover]: https://codecov.io/gh/webpack/css-loader/branch/main/graph/badge.svg
|
|
2360
|
+
[cover-url]: https://codecov.io/gh/webpack/css-loader
|
|
2361
|
+
[discussion]: https://img.shields.io/github/discussions/webpack/webpack
|
|
2362
|
+
[discussion-url]: https://github.com/webpack/webpack/discussions
|
|
2363
|
+
[size]: https://packagephobia.now.sh/badge?p=css-loader
|
|
2364
|
+
[size-url]: https://packagephobia.now.sh/result?p=css-loader
|