1 | <div align="center">
|
2 | <img width="180" height="180" vspace="20"
|
3 | src="https://cdn.worldvectorlogo.com/logos/css-3.svg">
|
4 | <a href="https://github.com/webpack/webpack">
|
5 | <img width="200" height="200"
|
6 | src="https://webpack.js.org/assets/icon-square-big.svg">
|
7 | </a>
|
8 | </div>
|
9 |
|
10 | [![npm][npm]][npm-url]
|
11 | [![node][node]][node-url]
|
12 | [![deps][deps]][deps-url]
|
13 | [![tests][tests]][tests-url]
|
14 | [![coverage][cover]][cover-url]
|
15 | [![chat][chat]][chat-url]
|
16 | [![size][size]][size-url]
|
17 |
|
18 | # css-loader
|
19 |
|
20 | The `css-loader` interprets `@import` and `url()` like `import/require()` and will resolve them.
|
21 |
|
22 | ## Getting Started
|
23 |
|
24 | > ⚠ To use css-loader, webpack@5 is required
|
25 |
|
26 | To begin, you'll need to install `css-loader`:
|
27 |
|
28 | ```console
|
29 | npm install --save-dev css-loader
|
30 | ```
|
31 |
|
32 | Then add the plugin to your `webpack` config. For example:
|
33 |
|
34 | **file.js**
|
35 |
|
36 | ```js
|
37 | import css from "file.css";
|
38 | ```
|
39 |
|
40 | **webpack.config.js**
|
41 |
|
42 | ```js
|
43 | module.exports = {
|
44 | module: {
|
45 | rules: [
|
46 | {
|
47 | test: /\.css$/i,
|
48 | use: ["style-loader", "css-loader"],
|
49 | },
|
50 | ],
|
51 | },
|
52 | };
|
53 | ```
|
54 |
|
55 | And run `webpack` via your preferred method.
|
56 |
|
57 | If, for one reason or another, you need to extract CSS as a file (i.e. do not store CSS in a JS module) you might want to check out the [recommend example](https://github.com/webpack-contrib/css-loader#recommend).
|
58 |
|
59 | ## Options
|
60 |
|
61 | | Name | Type | Default | Description |
|
62 | | :-----------------------------------: | :------------------------------------------: | :----------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
63 | | **[`url`](#url)** | `{Boolean\|Object}` | `true` | Allows to enables/disables `url()`/`image-set()` functions handling |
|
64 | | **[`import`](#import)** | `{Boolean\|Object}` | `true` | Allows to enables/disables `@import` at-rules handling |
|
65 | | **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `{auto: true}` | Allows to enables/disables or setup CSS Modules options |
|
66 | | **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
|
67 | | **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Allows enables/disables or setups number of loaders applied before CSS loader for `@import`/CSS Modules and ICSS imports |
|
68 | | **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax |
|
69 | | **[`exportType`](#exporttype)** | `{'array' \| 'string' \| 'css-style-sheet'}` | `array` | 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)) |
|
70 |
|
71 | ### `url`
|
72 |
|
73 | Type: `Boolean|Object`
|
74 | Default: `true`
|
75 |
|
76 | Allow to enable/disables handling the CSS functions `url` and `image-set`.
|
77 | If set to `false`, `css-loader` will not parse any paths specified in `url` or `image-set`.
|
78 | A function can also be passed to control this behavior dynamically based on the path to the asset.
|
79 | Starting with version [4.0.0](https://github.com/webpack-contrib/css-loader/blob/master/CHANGELOG.md#400-2020-07-25), absolute paths are parsed based on the server root.
|
80 |
|
81 | Examples resolutions:
|
82 |
|
83 | ```
|
84 | url(image.png) => require('./image.png')
|
85 | url('image.png') => require('./image.png')
|
86 | url(./image.png) => require('./image.png')
|
87 | url('./image.png') => require('./image.png')
|
88 | url('http://dontwritehorriblecode.com/2112.png') => require('http://dontwritehorriblecode.com/2112.png')
|
89 | image-set(url('image2x.png') 1x, url('image1x.png') 2x) => require('./image1x.png') and require('./image2x.png')
|
90 | ```
|
91 |
|
92 | To import assets from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:
|
93 |
|
94 | ```
|
95 | url(~module/image.png) => require('module/image.png')
|
96 | url('~module/image.png') => require('module/image.png')
|
97 | url(~aliasDirectory/image.png) => require('otherDirectory/image.png')
|
98 | ```
|
99 |
|
100 | #### `Boolean`
|
101 |
|
102 | Enable/disable `url()` resolving.
|
103 |
|
104 | **webpack.config.js**
|
105 |
|
106 | ```js
|
107 | module.exports = {
|
108 | module: {
|
109 | rules: [
|
110 | {
|
111 | test: /\.css$/i,
|
112 | loader: "css-loader",
|
113 | options: {
|
114 | url: true,
|
115 | },
|
116 | },
|
117 | ],
|
118 | },
|
119 | };
|
120 | ```
|
121 |
|
122 | #### `Object`
|
123 |
|
124 | Allow to filter `url()`. All filtered `url()` will not be resolved (left in the code as they were written).
|
125 |
|
126 | **webpack.config.js**
|
127 |
|
128 | ```js
|
129 | module.exports = {
|
130 | module: {
|
131 | rules: [
|
132 | {
|
133 | test: /\.css$/i,
|
134 | loader: "css-loader",
|
135 | options: {
|
136 | url: {
|
137 | filter: (url, resourcePath) => {
|
138 | // resourcePath - path to css file
|
139 |
|
140 | // Don't handle `img.png` urls
|
141 | if (url.includes("img.png")) {
|
142 | return false;
|
143 | }
|
144 |
|
145 | return true;
|
146 | },
|
147 | },
|
148 | },
|
149 | },
|
150 | ],
|
151 | },
|
152 | };
|
153 | ```
|
154 |
|
155 | ### `import`
|
156 |
|
157 | Type: `Boolean|Object`
|
158 | Default: `true`
|
159 |
|
160 | Allows to enables/disables `@import` at-rules handling.
|
161 | Control `@import` resolving. Absolute urls in `@import` will be moved in runtime code.
|
162 |
|
163 | Examples resolutions:
|
164 |
|
165 | ```
|
166 | @import 'style.css' => require('./style.css')
|
167 | @import url(style.css) => require('./style.css')
|
168 | @import url('style.css') => require('./style.css')
|
169 | @import './style.css' => require('./style.css')
|
170 | @import url(./style.css) => require('./style.css')
|
171 | @import url('./style.css') => require('./style.css')
|
172 | @import url('http://dontwritehorriblecode.com/style.css') => @import url('http://dontwritehorriblecode.com/style.css') in runtime
|
173 | ```
|
174 |
|
175 | To import styles from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:
|
176 |
|
177 | ```
|
178 | @import url(~module/style.css) => require('module/style.css')
|
179 | @import url('~module/style.css') => require('module/style.css')
|
180 | @import url(~aliasDirectory/style.css) => require('otherDirectory/style.css')
|
181 | ```
|
182 |
|
183 | #### `Boolean`
|
184 |
|
185 | Enable/disable `@import` resolving.
|
186 |
|
187 | **webpack.config.js**
|
188 |
|
189 | ```js
|
190 | module.exports = {
|
191 | module: {
|
192 | rules: [
|
193 | {
|
194 | test: /\.css$/i,
|
195 | loader: "css-loader",
|
196 | options: {
|
197 | import: true,
|
198 | },
|
199 | },
|
200 | ],
|
201 | },
|
202 | };
|
203 | ```
|
204 |
|
205 | #### `Object`
|
206 |
|
207 | | Name | Type | Default | Description |
|
208 | | :---------------------: | :----------: | :---------: | :------------------------ |
|
209 | | **[`filter`](#filter)** | `{Function}` | `undefined` | Allow to filter `@import` |
|
210 |
|
211 | ##### `filter`
|
212 |
|
213 | Type: `Function`
|
214 | Default: `undefined`
|
215 |
|
216 | Allow to filter `@import`. All filtered `@import` will not be resolved (left in the code as they were written).
|
217 |
|
218 | **webpack.config.js**
|
219 |
|
220 | ```js
|
221 | module.exports = {
|
222 | module: {
|
223 | rules: [
|
224 | {
|
225 | test: /\.css$/i,
|
226 | loader: "css-loader",
|
227 | options: {
|
228 | import: {
|
229 | filter: (url, media, resourcePath) => {
|
230 | // resourcePath - path to css file
|
231 |
|
232 | // Don't handle `style.css` import
|
233 | if (url.includes("style.css")) {
|
234 | return false;
|
235 | }
|
236 |
|
237 | return true;
|
238 | },
|
239 | },
|
240 | },
|
241 | },
|
242 | ],
|
243 | },
|
244 | };
|
245 | ```
|
246 |
|
247 | ### `modules`
|
248 |
|
249 | Type: `Boolean|String|Object`
|
250 | Default: `undefined`
|
251 |
|
252 | Allows to enable/disable CSS Modules or ICSS and setup configuration:
|
253 |
|
254 | - `undefined` - enable CSS modules for all files matching `/\.module\.\w+$/i.test(filename)` and `/\.icss\.\w+$/i.test(filename)` regexp.
|
255 | - `true` - enable CSS modules for all files.
|
256 | - `false` - disables CSS Modules for all files.
|
257 | - `string` - disables CSS Modules for all files and set the `mode` option, more information you can read [here](https://github.com/webpack-contrib/css-loader#mode)
|
258 | - `object` - enable CSS modules for all files, if `modules.auto` option is not specified, otherwise the `modules.auto` option will determine whether if it is CSS modules or not, more information you can read [here](https://github.com/webpack-contrib/css-loader#auto)
|
259 |
|
260 | The `modules` option enables/disables the **[CSS Modules](https://github.com/css-modules/css-modules)** specification and setup basic behaviour.
|
261 |
|
262 | Using `false` value increase performance because we avoid parsing **CSS Modules** features, it will be useful for developers who use vanilla css or use other technologies.
|
263 |
|
264 | **webpack.config.js**
|
265 |
|
266 | ```js
|
267 | module.exports = {
|
268 | module: {
|
269 | rules: [
|
270 | {
|
271 | test: /\.css$/i,
|
272 | loader: "css-loader",
|
273 | options: {
|
274 | modules: true,
|
275 | },
|
276 | },
|
277 | ],
|
278 | },
|
279 | };
|
280 | ```
|
281 |
|
282 | #### `Features`
|
283 |
|
284 | ##### `Scope`
|
285 |
|
286 | Using `local` value requires you to specify `:global` classes.
|
287 | Using `global` value requires you to specify `:local` classes.
|
288 | Using `pure` value requires selectors must contain at least one local class or id.
|
289 |
|
290 | You can find more information [here](https://github.com/css-modules/css-modules).
|
291 |
|
292 | Styles can be locally scoped to avoid globally scoping styles.
|
293 |
|
294 | The syntax `:local(.className)` can be used to declare `className` in the local scope. The local identifiers are exported by the module.
|
295 |
|
296 | With `:local` (without brackets) local mode can be switched on for this selector.
|
297 | The `:global(.className)` notation can be used to declare an explicit global selector.
|
298 | With `:global` (without brackets) global mode can be switched on for this selector.
|
299 |
|
300 | The loader replaces local selectors with unique identifiers. The chosen unique identifiers are exported by the module.
|
301 |
|
302 | ```css
|
303 | :local(.className) {
|
304 | background: red;
|
305 | }
|
306 | :local .className {
|
307 | color: green;
|
308 | }
|
309 | :local(.className .subClass) {
|
310 | color: green;
|
311 | }
|
312 | :local .className .subClass :global(.global-class-name) {
|
313 | color: blue;
|
314 | }
|
315 | ```
|
316 |
|
317 | ```css
|
318 | ._23_aKvs-b8bW2Vg3fwHozO {
|
319 | background: red;
|
320 | }
|
321 | ._23_aKvs-b8bW2Vg3fwHozO {
|
322 | color: green;
|
323 | }
|
324 | ._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 {
|
325 | color: green;
|
326 | }
|
327 | ._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 .global-class-name {
|
328 | color: blue;
|
329 | }
|
330 | ```
|
331 |
|
332 | > ℹ️ Identifiers are exported
|
333 |
|
334 | ```js
|
335 | exports.locals = {
|
336 | className: "_23_aKvs-b8bW2Vg3fwHozO",
|
337 | subClass: "_13LGdX8RMStbBE9w-t0gZ1",
|
338 | };
|
339 | ```
|
340 |
|
341 | CamelCase is recommended for local selectors. They are easier to use within the imported JS module.
|
342 |
|
343 | You can use `:local(#someId)`, but this is not recommended. Use classes instead of ids.
|
344 |
|
345 | ##### `Composing`
|
346 |
|
347 | When declaring a local classname you can compose a local class from another local classname.
|
348 |
|
349 | ```css
|
350 | :local(.className) {
|
351 | background: red;
|
352 | color: yellow;
|
353 | }
|
354 |
|
355 | :local(.subClass) {
|
356 | composes: className;
|
357 | background: blue;
|
358 | }
|
359 | ```
|
360 |
|
361 | This doesn't result in any change to the CSS itself but exports multiple classnames.
|
362 |
|
363 | ```js
|
364 | exports.locals = {
|
365 | className: "_23_aKvs-b8bW2Vg3fwHozO",
|
366 | subClass: "_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO",
|
367 | };
|
368 | ```
|
369 |
|
370 | ```css
|
371 | ._23_aKvs-b8bW2Vg3fwHozO {
|
372 | background: red;
|
373 | color: yellow;
|
374 | }
|
375 |
|
376 | ._13LGdX8RMStbBE9w-t0gZ1 {
|
377 | background: blue;
|
378 | }
|
379 | ```
|
380 |
|
381 | ##### `Importing`
|
382 |
|
383 | To import a local classname from another module.
|
384 |
|
385 | > i We strongly recommend that you specify the extension 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.
|
386 |
|
387 | ```css
|
388 | :local(.continueButton) {
|
389 | composes: button from "library/button.css";
|
390 | background: red;
|
391 | }
|
392 | ```
|
393 |
|
394 | ```css
|
395 | :local(.nameEdit) {
|
396 | composes: edit highlight from "./edit.css";
|
397 | background: red;
|
398 | }
|
399 | ```
|
400 |
|
401 | To import from multiple modules use multiple `composes:` rules.
|
402 |
|
403 | ```css
|
404 | :local(.className) {
|
405 | composes: edit hightlight from "./edit.css";
|
406 | composes: button from "module/button.css";
|
407 | composes: classFromThisModule;
|
408 | background: red;
|
409 | }
|
410 | ```
|
411 |
|
412 | ##### `Values`
|
413 |
|
414 | You can use `@value` to specific values to be reused throughout a document.
|
415 |
|
416 | We recommend use prefix `v-` for values, `s-` for selectors and `m-` for media at-rules.
|
417 |
|
418 | ```css
|
419 | @value v-primary: #BF4040;
|
420 | @value s-black: black-selector;
|
421 | @value m-large: (min-width: 960px);
|
422 |
|
423 | .header {
|
424 | color: v-primary;
|
425 | padding: 0 10px;
|
426 | }
|
427 |
|
428 | .s-black {
|
429 | color: black;
|
430 | }
|
431 |
|
432 | @media m-large {
|
433 | .header {
|
434 | padding: 0 20px;
|
435 | }
|
436 | }
|
437 | ```
|
438 |
|
439 | #### `Boolean`
|
440 |
|
441 | Enable **CSS Modules** features.
|
442 |
|
443 | **webpack.config.js**
|
444 |
|
445 | ```js
|
446 | module.exports = {
|
447 | module: {
|
448 | rules: [
|
449 | {
|
450 | test: /\.css$/i,
|
451 | loader: "css-loader",
|
452 | options: {
|
453 | modules: true,
|
454 | },
|
455 | },
|
456 | ],
|
457 | },
|
458 | };
|
459 | ```
|
460 |
|
461 | #### `String`
|
462 |
|
463 | Enable **CSS Modules** features and setup `mode`.
|
464 |
|
465 | **webpack.config.js**
|
466 |
|
467 | ```js
|
468 | module.exports = {
|
469 | module: {
|
470 | rules: [
|
471 | {
|
472 | test: /\.css$/i,
|
473 | loader: "css-loader",
|
474 | options: {
|
475 | // Using `local` value has same effect like using `modules: true`
|
476 | modules: "global",
|
477 | },
|
478 | },
|
479 | ],
|
480 | },
|
481 | };
|
482 | ```
|
483 |
|
484 | #### `Object`
|
485 |
|
486 | Enable **CSS Modules** features and setup options for them.
|
487 |
|
488 | **webpack.config.js**
|
489 |
|
490 | ```js
|
491 | module.exports = {
|
492 | module: {
|
493 | rules: [
|
494 | {
|
495 | test: /\.css$/i,
|
496 | loader: "css-loader",
|
497 | options: {
|
498 | modules: {
|
499 | mode: "local",
|
500 | auto: true,
|
501 | exportGlobals: true,
|
502 | localIdentName: "[path][name]__[local]--[hash:base64:5]",
|
503 | localIdentContext: path.resolve(__dirname, "src"),
|
504 | localIdentHashSalt: "my-custom-hash",
|
505 | namedExport: true,
|
506 | exportLocalsConvention: "camelCase",
|
507 | exportOnlyLocals: false,
|
508 | },
|
509 | },
|
510 | },
|
511 | ],
|
512 | },
|
513 | };
|
514 | ```
|
515 |
|
516 | ##### `auto`
|
517 |
|
518 | Type: `Boolean|RegExp|Function`
|
519 | Default: `undefined`
|
520 |
|
521 | Allows auto enable CSS modules/ICSS based on filename when `modules` option is object.
|
522 |
|
523 | Possible values:
|
524 |
|
525 | - `undefined` - enable CSS modules for all files.
|
526 | - `true` - enable CSS modules for all files matching `/\.module\.\w+$/i.test(filename)` and `/\.icss\.\w+$/i.test(filename)` regexp.
|
527 | - `false` - disables CSS Modules.
|
528 | - `RegExp` - enable CSS modules for all files matching `/RegExp/i.test(filename)` regexp.
|
529 | - `function` - enable CSS Modules for files based on the filename satisfying your filter function check.
|
530 |
|
531 | ###### `Boolean`
|
532 |
|
533 | Possible values:
|
534 |
|
535 | - `true` - enables CSS modules or interoperable CSS 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
|
536 | - `false` - disables CSS modules or interoperable CSS format based on filename
|
537 |
|
538 | **webpack.config.js**
|
539 |
|
540 | ```js
|
541 | module.exports = {
|
542 | module: {
|
543 | rules: [
|
544 | {
|
545 | test: /\.css$/i,
|
546 | loader: "css-loader",
|
547 | options: {
|
548 | modules: {
|
549 | auto: true,
|
550 | },
|
551 | },
|
552 | },
|
553 | ],
|
554 | },
|
555 | };
|
556 | ```
|
557 |
|
558 | ###### `RegExp`
|
559 |
|
560 | Enable css modules for files based on the filename satisfying your regex check.
|
561 |
|
562 | **webpack.config.js**
|
563 |
|
564 | ```js
|
565 | module.exports = {
|
566 | module: {
|
567 | rules: [
|
568 | {
|
569 | test: /\.css$/i,
|
570 | loader: "css-loader",
|
571 | options: {
|
572 | modules: {
|
573 | auto: /\.custom-module\.\w+$/i,
|
574 | },
|
575 | },
|
576 | },
|
577 | ],
|
578 | },
|
579 | };
|
580 | ```
|
581 |
|
582 | ###### `Function`
|
583 |
|
584 | Enable css modules for files based on the filename satisfying your filter function check.
|
585 |
|
586 | **webpack.config.js**
|
587 |
|
588 | ```js
|
589 | module.exports = {
|
590 | module: {
|
591 | rules: [
|
592 | {
|
593 | test: /\.css$/i,
|
594 | loader: "css-loader",
|
595 | options: {
|
596 | modules: {
|
597 | auto: (resourcePath) => resourcePath.endsWith(".custom-module.css"),
|
598 | },
|
599 | },
|
600 | },
|
601 | ],
|
602 | },
|
603 | };
|
604 | ```
|
605 |
|
606 | ##### `mode`
|
607 |
|
608 | Type: `String|Function`
|
609 | Default: `'local'`
|
610 |
|
611 | Setup `mode` option. You can omit the value when you want `local` mode.
|
612 |
|
613 | Controls the level of compilation applied to the input styles.
|
614 |
|
615 | The `local`, `global`, and `pure` handles `class` and `id` scoping and `@value` values.
|
616 | The `icss` will only compile the low level `Interoperable CSS` format for declaring `:import` and `:export` dependencies between CSS and other languages.
|
617 |
|
618 | ICSS underpins CSS Module support, and provides a low level syntax for other tools to implement CSS-module variations of their own.
|
619 |
|
620 | ###### `String`
|
621 |
|
622 | Possible values - `local`, `global`, `pure`, and `icss`.
|
623 |
|
624 | **webpack.config.js**
|
625 |
|
626 | ```js
|
627 | module.exports = {
|
628 | module: {
|
629 | rules: [
|
630 | {
|
631 | test: /\.css$/i,
|
632 | loader: "css-loader",
|
633 | options: {
|
634 | modules: {
|
635 | mode: "global",
|
636 | },
|
637 | },
|
638 | },
|
639 | ],
|
640 | },
|
641 | };
|
642 | ```
|
643 |
|
644 | ###### `Function`
|
645 |
|
646 | Allows set different values for the `mode` option based on a filename
|
647 |
|
648 | Possible return values - `local`, `global`, `pure` and `icss`.
|
649 |
|
650 | **webpack.config.js**
|
651 |
|
652 | ```js
|
653 | module.exports = {
|
654 | module: {
|
655 | rules: [
|
656 | {
|
657 | test: /\.css$/i,
|
658 | loader: "css-loader",
|
659 | options: {
|
660 | modules: {
|
661 | // Callback must return "local", "global", or "pure" values
|
662 | mode: (resourcePath) => {
|
663 | if (/pure.css$/i.test(resourcePath)) {
|
664 | return "pure";
|
665 | }
|
666 |
|
667 | if (/global.css$/i.test(resourcePath)) {
|
668 | return "global";
|
669 | }
|
670 |
|
671 | return "local";
|
672 | },
|
673 | },
|
674 | },
|
675 | },
|
676 | ],
|
677 | },
|
678 | };
|
679 | ```
|
680 |
|
681 | ##### `localIdentName`
|
682 |
|
683 | Type: `String`
|
684 | Default: `'[hash:base64]'`
|
685 |
|
686 | Allows to configure the generated local ident name.
|
687 |
|
688 | For more information on options see:
|
689 |
|
690 | - [webpack template strings](https://webpack.js.org/configuration/output/#template-strings),
|
691 | - [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest),
|
692 | - [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength),
|
693 | - [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction),
|
694 | - [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).
|
695 |
|
696 | Supported template strings:
|
697 |
|
698 | - `[name]` the basename of the resource
|
699 | - `[folder]` the folder the resource relative to the `compiler.context` option or `modules.localIdentContext` option.
|
700 | - `[path]` the path of the resource relative to the `compiler.context` option or `modules.localIdentContext` option.
|
701 | - `[file]` - filename and path.
|
702 | - `[ext]` - extension with leading `.`.
|
703 | - `[hash]` - the hash of the string, generated based on `localIdentHashSalt`, `localIdentHashFunction`, `localIdentHashDigest`, `localIdentHashDigestLength`, `localIdentContext`, `resourcePath` and `exportName`
|
704 | - `[<hashFunction>:hash:<hashDigest>:<hashDigestLength>]` - hash with hash settings.
|
705 | - `[local]` - original class.
|
706 |
|
707 | Recommendations:
|
708 |
|
709 | - use `'[path][name]__[local]'` for development
|
710 | - use `'[hash:base64]'` for production
|
711 |
|
712 | The `[local]` placeholder contains original class.
|
713 |
|
714 | **Note:** all reserved (`<>:"/\|?*`) and control filesystem characters (excluding characters in the `[local]` placeholder) will be converted to `-`.
|
715 |
|
716 | **webpack.config.js**
|
717 |
|
718 | ```js
|
719 | module.exports = {
|
720 | module: {
|
721 | rules: [
|
722 | {
|
723 | test: /\.css$/i,
|
724 | loader: "css-loader",
|
725 | options: {
|
726 | modules: {
|
727 | localIdentName: "[path][name]__[local]--[hash:base64:5]",
|
728 | },
|
729 | },
|
730 | },
|
731 | ],
|
732 | },
|
733 | };
|
734 | ```
|
735 |
|
736 | ##### `localIdentContext`
|
737 |
|
738 | Type: `String`
|
739 | Default: `compiler.context`
|
740 |
|
741 | Allows to redefine basic loader context for local ident name.
|
742 |
|
743 | **webpack.config.js**
|
744 |
|
745 | ```js
|
746 | module.exports = {
|
747 | module: {
|
748 | rules: [
|
749 | {
|
750 | test: /\.css$/i,
|
751 | loader: "css-loader",
|
752 | options: {
|
753 | modules: {
|
754 | localIdentContext: path.resolve(__dirname, "src"),
|
755 | },
|
756 | },
|
757 | },
|
758 | ],
|
759 | },
|
760 | };
|
761 | ```
|
762 |
|
763 | ##### `localIdentHashSalt`
|
764 |
|
765 | Type: `String`
|
766 | Default: `undefined`
|
767 |
|
768 | Allows to add custom hash to generate more unique classes.
|
769 | For more information see [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).
|
770 |
|
771 | **webpack.config.js**
|
772 |
|
773 | ```js
|
774 | module.exports = {
|
775 | module: {
|
776 | rules: [
|
777 | {
|
778 | test: /\.css$/i,
|
779 | loader: "css-loader",
|
780 | options: {
|
781 | modules: {
|
782 | localIdentHashSalt: "hash",
|
783 | },
|
784 | },
|
785 | },
|
786 | ],
|
787 | },
|
788 | };
|
789 | ```
|
790 |
|
791 | ##### `localIdentHashFunction`
|
792 |
|
793 | Type: `String`
|
794 | Default: `md4`
|
795 |
|
796 | Allows to specify hash function to generate classes .
|
797 | For more information see [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction).
|
798 |
|
799 | **webpack.config.js**
|
800 |
|
801 | ```js
|
802 | module.exports = {
|
803 | module: {
|
804 | rules: [
|
805 | {
|
806 | test: /\.css$/i,
|
807 | loader: "css-loader",
|
808 | options: {
|
809 | modules: {
|
810 | localIdentHashFunction: "md4",
|
811 | },
|
812 | },
|
813 | },
|
814 | ],
|
815 | },
|
816 | };
|
817 | ```
|
818 |
|
819 | ##### `localIdentHashDigest`
|
820 |
|
821 | Type: `String`
|
822 | Default: `hex`
|
823 |
|
824 | Allows to specify hash digest to generate classes.
|
825 | For more information see [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest).
|
826 |
|
827 | **webpack.config.js**
|
828 |
|
829 | ```js
|
830 | module.exports = {
|
831 | module: {
|
832 | rules: [
|
833 | {
|
834 | test: /\.css$/i,
|
835 | loader: "css-loader",
|
836 | options: {
|
837 | modules: {
|
838 | localIdentHashDigest: "base64",
|
839 | },
|
840 | },
|
841 | },
|
842 | ],
|
843 | },
|
844 | };
|
845 | ```
|
846 |
|
847 | ##### `localIdentHashDigestLength`
|
848 |
|
849 | Type: `Number`
|
850 | Default: `20`
|
851 |
|
852 | Allows to specify hash digest length to generate classes.
|
853 | For more information see [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength).
|
854 |
|
855 | **webpack.config.js**
|
856 |
|
857 | ```js
|
858 | module.exports = {
|
859 | module: {
|
860 | rules: [
|
861 | {
|
862 | test: /\.css$/i,
|
863 | loader: "css-loader",
|
864 | options: {
|
865 | modules: {
|
866 | localIdentHashDigestLength: 5,
|
867 | },
|
868 | },
|
869 | },
|
870 | ],
|
871 | },
|
872 | };
|
873 | ```
|
874 |
|
875 | ##### `localIdentRegExp`
|
876 |
|
877 | Type: `String|RegExp`
|
878 | Default: `undefined`
|
879 |
|
880 | **webpack.config.js**
|
881 |
|
882 | ```js
|
883 | module.exports = {
|
884 | module: {
|
885 | rules: [
|
886 | {
|
887 | test: /\.css$/i,
|
888 | loader: "css-loader",
|
889 | options: {
|
890 | modules: {
|
891 | localIdentRegExp: /page-(.*)\.css/i,
|
892 | },
|
893 | },
|
894 | },
|
895 | ],
|
896 | },
|
897 | };
|
898 | ```
|
899 |
|
900 | ##### `getLocalIdent`
|
901 |
|
902 | Type: `Function`
|
903 | Default: `undefined`
|
904 |
|
905 | Allows to specify a function to generate the classname.
|
906 | By default we use built-in function to generate a classname.
|
907 | If the custom function returns `null` or `undefined`, we fallback to the
|
908 | built-in function to generate the classname.
|
909 |
|
910 | **webpack.config.js**
|
911 |
|
912 | ```js
|
913 | module.exports = {
|
914 | module: {
|
915 | rules: [
|
916 | {
|
917 | test: /\.css$/i,
|
918 | loader: "css-loader",
|
919 | options: {
|
920 | modules: {
|
921 | getLocalIdent: (context, localIdentName, localName, options) => {
|
922 | return "whatever_random_class_name";
|
923 | },
|
924 | },
|
925 | },
|
926 | },
|
927 | ],
|
928 | },
|
929 | };
|
930 | ```
|
931 |
|
932 | ##### `namedExport`
|
933 |
|
934 | Type: `Boolean`
|
935 | Default: `false`
|
936 |
|
937 | Enables/disables ES modules named export for locals.
|
938 |
|
939 | > ⚠ Names of locals are converted to camelcase, i.e. the `exportLocalsConvention` option has `camelCaseOnly` value by default.
|
940 |
|
941 | > ⚠ It is not allowed to use JavaScript reserved words in css class names.
|
942 |
|
943 | **styles.css**
|
944 |
|
945 | ```css
|
946 | .foo-baz {
|
947 | color: red;
|
948 | }
|
949 | .bar {
|
950 | color: blue;
|
951 | }
|
952 | ```
|
953 |
|
954 | **index.js**
|
955 |
|
956 | ```js
|
957 | import { fooBaz, bar } from "./styles.css";
|
958 |
|
959 | console.log(fooBaz, bar);
|
960 | ```
|
961 |
|
962 | You can enable a ES module named export using:
|
963 |
|
964 | **webpack.config.js**
|
965 |
|
966 | ```js
|
967 | module.exports = {
|
968 | module: {
|
969 | rules: [
|
970 | {
|
971 | test: /\.css$/i,
|
972 | loader: "css-loader",
|
973 | options: {
|
974 | esModule: true,
|
975 | modules: {
|
976 | namedExport: true,
|
977 | },
|
978 | },
|
979 | },
|
980 | ],
|
981 | },
|
982 | };
|
983 | ```
|
984 |
|
985 | To set a custom name for namedExport, can use [`exportLocalsConvention`](#exportLocalsConvention) option as a function.
|
986 | Example below in the [`examples`](#examples) section.
|
987 |
|
988 | ##### `exportGlobals`
|
989 |
|
990 | Type: `Boolean`
|
991 | Default: `false`
|
992 |
|
993 | Allow `css-loader` to export names from global class or id, so you can use that as local name.
|
994 |
|
995 | **webpack.config.js**
|
996 |
|
997 | ```js
|
998 | module.exports = {
|
999 | module: {
|
1000 | rules: [
|
1001 | {
|
1002 | test: /\.css$/i,
|
1003 | loader: "css-loader",
|
1004 | options: {
|
1005 | modules: {
|
1006 | exportGlobals: true,
|
1007 | },
|
1008 | },
|
1009 | },
|
1010 | ],
|
1011 | },
|
1012 | };
|
1013 | ```
|
1014 |
|
1015 | ##### `exportLocalsConvention`
|
1016 |
|
1017 | Type: `String|Function`
|
1018 | Default: based on the `modules.namedExport` option value, if `true` - `camelCaseOnly`, otherwise `asIs`
|
1019 |
|
1020 | Style of exported class names.
|
1021 |
|
1022 | ###### `String`
|
1023 |
|
1024 | By default, the exported JSON keys mirror the class names (i.e `asIs` value).
|
1025 |
|
1026 | > ⚠ Only `camelCaseOnly` value allowed if you set the `namedExport` value to `true`.
|
1027 |
|
1028 | | Name | Type | Description |
|
1029 | | :-------------------: | :--------: | :----------------------------------------------------------------------------------------------- |
|
1030 | | **`'asIs'`** | `{String}` | Class names will be exported as is. |
|
1031 | | **`'camelCase'`** | `{String}` | Class names will be camelized, the original class name will not to be removed from the locals |
|
1032 | | **`'camelCaseOnly'`** | `{String}` | Class names will be camelized, the original class name will be removed from the locals |
|
1033 | | **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
|
1034 | | **`'dashesOnly'`** | `{String}` | Dashes in class names will be camelized, the original class name will be removed from the locals |
|
1035 |
|
1036 | **file.css**
|
1037 |
|
1038 | ```css
|
1039 | .class-name {
|
1040 | }
|
1041 | ```
|
1042 |
|
1043 | **file.js**
|
1044 |
|
1045 | ```js
|
1046 | import { className } from "file.css";
|
1047 | ```
|
1048 |
|
1049 | **webpack.config.js**
|
1050 |
|
1051 | ```js
|
1052 | module.exports = {
|
1053 | module: {
|
1054 | rules: [
|
1055 | {
|
1056 | test: /\.css$/i,
|
1057 | loader: "css-loader",
|
1058 | options: {
|
1059 | modules: {
|
1060 | exportLocalsConvention: "camelCase",
|
1061 | },
|
1062 | },
|
1063 | },
|
1064 | ],
|
1065 | },
|
1066 | };
|
1067 | ```
|
1068 |
|
1069 | ###### `Function`
|
1070 |
|
1071 | **webpack.config.js**
|
1072 |
|
1073 | ```js
|
1074 | module.exports = {
|
1075 | module: {
|
1076 | rules: [
|
1077 | {
|
1078 | test: /\.css$/i,
|
1079 | loader: "css-loader",
|
1080 | options: {
|
1081 | modules: {
|
1082 | exportLocalsConvention: function (name) {
|
1083 | return name.replace(/-/g, "_");
|
1084 | },
|
1085 | },
|
1086 | },
|
1087 | },
|
1088 | ],
|
1089 | },
|
1090 | };
|
1091 | ```
|
1092 |
|
1093 | **webpack.config.js**
|
1094 |
|
1095 | ```js
|
1096 | module.exports = {
|
1097 | module: {
|
1098 | rules: [
|
1099 | {
|
1100 | test: /\.css$/i,
|
1101 | loader: "css-loader",
|
1102 | options: {
|
1103 | modules: {
|
1104 | exportLocalsConvention: function (name) {
|
1105 | return [
|
1106 | name.replace(/-/g, "_"),
|
1107 | // dashesCamelCase
|
1108 | name.replace(/-+(\w)/g, (match, firstLetter) =>
|
1109 | firstLetter.toUpperCase()
|
1110 | ),
|
1111 | ];
|
1112 | },
|
1113 | },
|
1114 | },
|
1115 | },
|
1116 | ],
|
1117 | },
|
1118 | };
|
1119 | ```
|
1120 |
|
1121 | ##### `exportOnlyLocals`
|
1122 |
|
1123 | Type: `Boolean`
|
1124 | Default: `false`
|
1125 |
|
1126 | Export only locals.
|
1127 |
|
1128 | **Useful** when you use **css modules** for pre-rendering (for example SSR).
|
1129 | For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
|
1130 | It doesn't embed CSS but only exports the identifier mappings.
|
1131 |
|
1132 | **webpack.config.js**
|
1133 |
|
1134 | ```js
|
1135 | module.exports = {
|
1136 | module: {
|
1137 | rules: [
|
1138 | {
|
1139 | test: /\.css$/i,
|
1140 | loader: "css-loader",
|
1141 | options: {
|
1142 | modules: {
|
1143 | exportOnlyLocals: true,
|
1144 | },
|
1145 | },
|
1146 | },
|
1147 | ],
|
1148 | },
|
1149 | };
|
1150 | ```
|
1151 |
|
1152 | ### `importLoaders`
|
1153 |
|
1154 | Type: `Number`
|
1155 | Default: `0`
|
1156 |
|
1157 | Allows to enables/disables or setups 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.
|
1158 |
|
1159 | 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.
|
1160 |
|
1161 | **webpack.config.js**
|
1162 |
|
1163 | ```js
|
1164 | module.exports = {
|
1165 | module: {
|
1166 | rules: [
|
1167 | {
|
1168 | test: /\.css$/i,
|
1169 | use: [
|
1170 | "style-loader",
|
1171 | {
|
1172 | loader: "css-loader",
|
1173 | options: {
|
1174 | importLoaders: 2,
|
1175 | // 0 => no loaders (default);
|
1176 | // 1 => postcss-loader;
|
1177 | // 2 => postcss-loader, sass-loader
|
1178 | },
|
1179 | },
|
1180 | "postcss-loader",
|
1181 | "sass-loader",
|
1182 | ],
|
1183 | },
|
1184 | ],
|
1185 | },
|
1186 | };
|
1187 | ```
|
1188 |
|
1189 | This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
|
1190 |
|
1191 | ### `sourceMap`
|
1192 |
|
1193 | Type: `Boolean`
|
1194 | Default: depends on the `compiler.devtool` value
|
1195 |
|
1196 | 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` value.
|
1197 |
|
1198 | **webpack.config.js**
|
1199 |
|
1200 | ```js
|
1201 | module.exports = {
|
1202 | module: {
|
1203 | rules: [
|
1204 | {
|
1205 | test: /\.css$/i,
|
1206 | loader: "css-loader",
|
1207 | options: {
|
1208 | sourceMap: true,
|
1209 | },
|
1210 | },
|
1211 | ],
|
1212 | },
|
1213 | };
|
1214 | ```
|
1215 |
|
1216 | ### `esModule`
|
1217 |
|
1218 | Type: `Boolean`
|
1219 | Default: `true`
|
1220 |
|
1221 | By default, `css-loader` generates JS modules that use the ES modules syntax.
|
1222 | 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/).
|
1223 |
|
1224 | You can enable a CommonJS modules syntax using:
|
1225 |
|
1226 | **webpack.config.js**
|
1227 |
|
1228 | ```js
|
1229 | module.exports = {
|
1230 | module: {
|
1231 | rules: [
|
1232 | {
|
1233 | test: /\.css$/i,
|
1234 | loader: "css-loader",
|
1235 | options: {
|
1236 | esModule: false,
|
1237 | },
|
1238 | },
|
1239 | ],
|
1240 | },
|
1241 | };
|
1242 | ```
|
1243 |
|
1244 | ### `exportType`
|
1245 |
|
1246 | Type: `'array' | 'string' | 'css-style-sheet'`
|
1247 | Default: `'array'`
|
1248 |
|
1249 | 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)).
|
1250 | Default value is `'array'`, i.e. loader exports array of modules with specific API which is used in `style-loader` or other.
|
1251 |
|
1252 | **webpack.config.js**
|
1253 |
|
1254 | ```js
|
1255 | module.exports = {
|
1256 | module: {
|
1257 | rules: [
|
1258 | {
|
1259 | assert: { type: "css" },
|
1260 | loader: "css-loader",
|
1261 | options: {
|
1262 | exportType: "css-style-sheet",
|
1263 | },
|
1264 | },
|
1265 | ],
|
1266 | },
|
1267 | };
|
1268 | ```
|
1269 |
|
1270 | **src/index.js**
|
1271 |
|
1272 | ```js
|
1273 | import sheet from "./styles.css" assert { type: "css" };
|
1274 |
|
1275 | document.adoptedStyleSheets = [sheet];
|
1276 | shadowRoot.adoptedStyleSheets = [sheet];
|
1277 | ```
|
1278 |
|
1279 | #### `'array'`
|
1280 |
|
1281 | The default export is array of modules with specific API which is used in `style-loader` or other.
|
1282 |
|
1283 | **webpack.config.js**
|
1284 |
|
1285 | ```js
|
1286 | module.exports = {
|
1287 | module: {
|
1288 | rules: [
|
1289 | {
|
1290 | test: /\.(sa|sc|c)ss$/i,
|
1291 | use: ["style-loader", "css-loader", "postcss-loader", "sass-loader"],
|
1292 | },
|
1293 | ],
|
1294 | },
|
1295 | };
|
1296 | ```
|
1297 |
|
1298 | **src/index.js**
|
1299 |
|
1300 | ```js
|
1301 | // `style-loader` applies styles to DOM
|
1302 | import "./styles.css";
|
1303 | ```
|
1304 |
|
1305 | #### `'string'`
|
1306 |
|
1307 | > ⚠ You don't need [`style-loader`](https://github.com/webpack-contrib/style-loader) anymore, please remove it.
|
1308 | > ⚠ The `esModules` option should be enabled if you want to use it with [`CSS modules`](https://github.com/webpack-contrib/css-loader#modules), by default for locals will be used [named export](https://github.com/webpack-contrib/css-loader#namedexport).
|
1309 |
|
1310 | The default export is `string`.
|
1311 |
|
1312 | **webpack.config.js**
|
1313 |
|
1314 | ```js
|
1315 | module.exports = {
|
1316 | module: {
|
1317 | rules: [
|
1318 | {
|
1319 | test: /\.(sa|sc|c)ss$/i,
|
1320 | use: ["css-loader", "postcss-loader", "sass-loader"],
|
1321 | },
|
1322 | ],
|
1323 | },
|
1324 | };
|
1325 | ```
|
1326 |
|
1327 | **src/index.js**
|
1328 |
|
1329 | ```js
|
1330 | import sheet from "./styles.css";
|
1331 |
|
1332 | console.log(sheet);
|
1333 | ```
|
1334 |
|
1335 | #### `'css-style-sheet'`
|
1336 |
|
1337 | > ⚠ `@import` rules not yet allowed, more [information](https://web.dev/css-module-scripts/#@import-rules-not-yet-allowed)
|
1338 | > ⚠ You don't need [`style-loader`](https://github.com/webpack-contrib/style-loader) anymore, please remove it.
|
1339 | > ⚠ The `esModules` option should be enabled if you want to use it with [`CSS modules`](https://github.com/webpack-contrib/css-loader#modules), by default for locals will be used [named export](https://github.com/webpack-contrib/css-loader#namedexport).
|
1340 | > ⚠ Source maps are not currently supported in `Chrome` due [bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1174094&q=CSSStyleSheet%20source%20maps&can=2)
|
1341 |
|
1342 | 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)).
|
1343 |
|
1344 | Useful for [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) and shadow DOM.
|
1345 |
|
1346 | More information:
|
1347 |
|
1348 | - [Using CSS Module Scripts to import stylesheets](https://web.dev/css-module-scripts/)
|
1349 | - [Constructable Stylesheets: seamless reusable styles](https://developers.google.com/web/updates/2019/02/constructable-stylesheets)
|
1350 |
|
1351 | **webpack.config.js**
|
1352 |
|
1353 | ```js
|
1354 | module.exports = {
|
1355 | module: {
|
1356 | rules: [
|
1357 | {
|
1358 | assert: { type: "css" },
|
1359 | loader: "css-loader",
|
1360 | options: {
|
1361 | exportType: "css-style-sheet",
|
1362 | },
|
1363 | },
|
1364 |
|
1365 | // For Sass/SCSS:
|
1366 | //
|
1367 | // {
|
1368 | // assert: { type: "css" },
|
1369 | // rules: [
|
1370 | // {
|
1371 | // loader: "css-loader",
|
1372 | // options: {
|
1373 | // exportType: "css-style-sheet",
|
1374 | // // Other options
|
1375 | // },
|
1376 | // },
|
1377 | // {
|
1378 | // loader: "sass-loader",
|
1379 | // options: {
|
1380 | // // Other options
|
1381 | // },
|
1382 | // },
|
1383 | // ],
|
1384 | // },
|
1385 | ],
|
1386 | },
|
1387 | };
|
1388 | ```
|
1389 |
|
1390 | **src/index.js**
|
1391 |
|
1392 | ```js
|
1393 | // Example for Sass/SCSS:
|
1394 | // import sheet from "./styles.scss" assert { type: "css" };
|
1395 |
|
1396 | // Example for CSS modules:
|
1397 | // import sheet, { myClass } from "./styles.scss" assert { type: "css" };
|
1398 |
|
1399 | // Example for CSS:
|
1400 | import sheet from "./styles.css" assert { type: "css" };
|
1401 |
|
1402 | document.adoptedStyleSheets = [sheet];
|
1403 | shadowRoot.adoptedStyleSheets = [sheet];
|
1404 | ```
|
1405 |
|
1406 | For migration purposes, you can use the following configuration:
|
1407 |
|
1408 | ```js
|
1409 | module.exports = {
|
1410 | module: {
|
1411 | rules: [
|
1412 | {
|
1413 | test: /\.css$/i,
|
1414 | oneOf: [
|
1415 | {
|
1416 | assert: { type: "css" },
|
1417 | loader: "css-loader",
|
1418 | options: {
|
1419 | exportType: "css-style-sheet",
|
1420 | // Other options
|
1421 | },
|
1422 | },
|
1423 | {
|
1424 | use: [
|
1425 | "style-loader",
|
1426 | {
|
1427 | loader: "css-loader",
|
1428 | options: {
|
1429 | // Other options
|
1430 | },
|
1431 | },
|
1432 | ],
|
1433 | },
|
1434 | ],
|
1435 | },
|
1436 | ],
|
1437 | },
|
1438 | };
|
1439 | ```
|
1440 |
|
1441 | ## Examples
|
1442 |
|
1443 | ### Recommend
|
1444 |
|
1445 | 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.
|
1446 | This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin), because it creates separate css files.
|
1447 | For `development` mode (including `webpack-dev-server`) you can use [style-loader](https://github.com/webpack-contrib/style-loader), because it injects CSS into the DOM using multiple <style></style> and works faster.
|
1448 |
|
1449 | > i Do not use together `style-loader` and `mini-css-extract-plugin`.
|
1450 |
|
1451 | **webpack.config.js**
|
1452 |
|
1453 | ```js
|
1454 | const MiniCssExtractPlugin = require("mini-css-extract-plugin");
|
1455 | const devMode = process.env.NODE_ENV !== "production";
|
1456 |
|
1457 | module.exports = {
|
1458 | module: {
|
1459 | rules: [
|
1460 | {
|
1461 | test: /\.(sa|sc|c)ss$/i,
|
1462 | use: [
|
1463 | devMode ? "style-loader" : MiniCssExtractPlugin.loader,
|
1464 | "css-loader",
|
1465 | "postcss-loader",
|
1466 | "sass-loader",
|
1467 | ],
|
1468 | },
|
1469 | ],
|
1470 | },
|
1471 | plugins: [].concat(devMode ? [] : [new MiniCssExtractPlugin()]),
|
1472 | };
|
1473 | ```
|
1474 |
|
1475 | ### Disable url resolving using the `/* webpackIgnore: true */` comment
|
1476 |
|
1477 | With the help of the `/* webpackIgnore: true */`comment, it is possible to disable sources handling for rules and for individual declarations.
|
1478 |
|
1479 | ```css
|
1480 | /* webpackIgnore: true */
|
1481 | @import url(./basic.css);
|
1482 | @import /* webpackIgnore: true */ url(./imported.css);
|
1483 |
|
1484 | .class {
|
1485 | /* Disabled url handling for the all urls in the 'background' declaration */
|
1486 | color: red;
|
1487 | /* webpackIgnore: true */
|
1488 | background: url("./url/img.png"), url("./url/img.png");
|
1489 | }
|
1490 |
|
1491 | .class {
|
1492 | /* Disabled url handling for the first url in the 'background' declaration */
|
1493 | color: red;
|
1494 | background:
|
1495 | /* webpackIgnore: true */ url("./url/img.png"), url("./url/img.png");
|
1496 | }
|
1497 |
|
1498 | .class {
|
1499 | /* Disabled url handling for the second url in the 'background' declaration */
|
1500 | color: red;
|
1501 | background: url("./url/img.png"),
|
1502 | /* webpackIgnore: true */ url("./url/img.png");
|
1503 | }
|
1504 |
|
1505 | /* prettier-ignore */
|
1506 | .class {
|
1507 | /* Disabled url handling for the second url in the 'background' declaration */
|
1508 | color: red;
|
1509 | background: url("./url/img.png"),
|
1510 | /* webpackIgnore: true */
|
1511 | url("./url/img.png");
|
1512 | }
|
1513 |
|
1514 | /* prettier-ignore */
|
1515 | .class {
|
1516 | /* Disabled url handling for third and sixth urls in the 'background-image' declaration */
|
1517 | background-image: image-set(
|
1518 | url(./url/img.png) 2x,
|
1519 | url(./url/img.png) 3x,
|
1520 | /* webpackIgnore: true */ url(./url/img.png) 4x,
|
1521 | url(./url/img.png) 5x,
|
1522 | url(./url/img.png) 6x,
|
1523 | /* webpackIgnore: true */
|
1524 | url(./url/img.png) 7x
|
1525 | );
|
1526 | }
|
1527 | ```
|
1528 |
|
1529 | ### Assets
|
1530 |
|
1531 | 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.
|
1532 |
|
1533 | **For webpack v5:**
|
1534 |
|
1535 | **webpack.config.js**
|
1536 |
|
1537 | ```js
|
1538 | module.exports = {
|
1539 | module: {
|
1540 | rules: [
|
1541 | {
|
1542 | test: /\.css$/i,
|
1543 | use: ["style-loader", "css-loader"],
|
1544 | },
|
1545 | {
|
1546 | test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
|
1547 | // More information here https://webpack.js.org/guides/asset-modules/
|
1548 | type: "asset",
|
1549 | },
|
1550 | ],
|
1551 | },
|
1552 | };
|
1553 | ```
|
1554 |
|
1555 | ### Extract
|
1556 |
|
1557 | 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.
|
1558 |
|
1559 | - 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.
|
1560 |
|
1561 | - 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
|
1562 |
|
1563 | ### Pure CSS, CSS modules and PostCSS
|
1564 |
|
1565 | When you have pure CSS (without CSS modules), CSS modules and PostCSS in your project you can use this setup:
|
1566 |
|
1567 | **webpack.config.js**
|
1568 |
|
1569 | ```js
|
1570 | module.exports = {
|
1571 | module: {
|
1572 | rules: [
|
1573 | {
|
1574 | // For pure CSS - /\.css$/i,
|
1575 | // For Sass/SCSS - /\.((c|sa|sc)ss)$/i,
|
1576 | // For Less - /\.((c|le)ss)$/i,
|
1577 | test: /\.((c|sa|sc)ss)$/i,
|
1578 | use: [
|
1579 | "style-loader",
|
1580 | {
|
1581 | loader: "css-loader",
|
1582 | options: {
|
1583 | // 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
|
1584 | // If you need run `sass-loader` and `postcss-loader` on each CSS `@import` please set it to `2`
|
1585 | importLoaders: 1,
|
1586 | },
|
1587 | },
|
1588 | {
|
1589 | loader: "postcss-loader",
|
1590 | options: { plugins: () => [postcssPresetEnv({ stage: 0 })] },
|
1591 | },
|
1592 | // Can be `less-loader`
|
1593 | {
|
1594 | loader: "sass-loader",
|
1595 | },
|
1596 | ],
|
1597 | },
|
1598 | // For webpack v5
|
1599 | {
|
1600 | test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
|
1601 | // More information here https://webpack.js.org/guides/asset-modules/
|
1602 | type: "asset",
|
1603 | },
|
1604 | ],
|
1605 | },
|
1606 | };
|
1607 | ```
|
1608 |
|
1609 | ### Resolve unresolved URLs using an alias
|
1610 |
|
1611 | **index.css**
|
1612 |
|
1613 | ```css
|
1614 | .class {
|
1615 | background: url(/assets/unresolved/img.png);
|
1616 | }
|
1617 | ```
|
1618 |
|
1619 | **webpack.config.js**
|
1620 |
|
1621 | ```js
|
1622 | module.exports = {
|
1623 | module: {
|
1624 | rules: [
|
1625 | {
|
1626 | test: /\.css$/i,
|
1627 | use: ["style-loader", "css-loader"],
|
1628 | },
|
1629 | ],
|
1630 | },
|
1631 | resolve: {
|
1632 | alias: {
|
1633 | "/assets/unresolved/img.png": path.resolve(
|
1634 | __dirname,
|
1635 | "assets/real-path-to-img/img.png"
|
1636 | ),
|
1637 | },
|
1638 | },
|
1639 | };
|
1640 | ```
|
1641 |
|
1642 | ### Named export with custom export names
|
1643 |
|
1644 | **webpack.config.js**
|
1645 |
|
1646 | ```js
|
1647 | module.exports = {
|
1648 | module: {
|
1649 | rules: [
|
1650 | {
|
1651 | test: /\.css$/i,
|
1652 | loader: "css-loader",
|
1653 | options: {
|
1654 | modules: {
|
1655 | namedExport: true,
|
1656 | exportLocalsConvention: function (name) {
|
1657 | return name.replace(/-/g, "_");
|
1658 | },
|
1659 | },
|
1660 | },
|
1661 | },
|
1662 | ],
|
1663 | },
|
1664 | };
|
1665 | ```
|
1666 |
|
1667 | ### Separating `Interoperable CSS`-only and `CSS Module` features
|
1668 |
|
1669 | 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 `mode` option for all files that do not match `*.module.scss` naming convention. This is for reference as having `ICSS` features applied to all files was default `css-loader` behavior before v4.
|
1670 | Meanwhile all files matching `*.module.scss` are treated as `CSS Modules` in this example.
|
1671 |
|
1672 | 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).
|
1673 |
|
1674 | **webpack.config.js**
|
1675 |
|
1676 | ```js
|
1677 | module.exports = {
|
1678 | module: {
|
1679 | rules: [
|
1680 | // ...
|
1681 | // --------
|
1682 | // SCSS ALL EXCEPT MODULES
|
1683 | {
|
1684 | test: /\.scss$/i,
|
1685 | exclude: /\.module\.scss$/i,
|
1686 | use: [
|
1687 | {
|
1688 | loader: "style-loader",
|
1689 | },
|
1690 | {
|
1691 | loader: "css-loader",
|
1692 | options: {
|
1693 | importLoaders: 1,
|
1694 | modules: {
|
1695 | mode: "icss",
|
1696 | },
|
1697 | },
|
1698 | },
|
1699 | {
|
1700 | loader: "sass-loader",
|
1701 | },
|
1702 | ],
|
1703 | },
|
1704 | // --------
|
1705 | // SCSS MODULES
|
1706 | {
|
1707 | test: /\.module\.scss$/i,
|
1708 | use: [
|
1709 | {
|
1710 | loader: "style-loader",
|
1711 | },
|
1712 | {
|
1713 | loader: "css-loader",
|
1714 | options: {
|
1715 | importLoaders: 1,
|
1716 | modules: {
|
1717 | mode: "local",
|
1718 | },
|
1719 | },
|
1720 | },
|
1721 | {
|
1722 | loader: "sass-loader",
|
1723 | },
|
1724 | ],
|
1725 | },
|
1726 | // --------
|
1727 | // ...
|
1728 | ],
|
1729 | },
|
1730 | };
|
1731 | ```
|
1732 |
|
1733 | **variables.scss**
|
1734 |
|
1735 | File treated as `ICSS`-only.
|
1736 |
|
1737 | ```scss
|
1738 | $colorBackground: red;
|
1739 | :export {
|
1740 | colorBackgroundCanvas: $colorBackground;
|
1741 | }
|
1742 | ```
|
1743 |
|
1744 | **Component.module.scss**
|
1745 |
|
1746 | File treated as `CSS Module`.
|
1747 |
|
1748 | ```scss
|
1749 | @import "variables.scss";
|
1750 | .componentClass {
|
1751 | background-color: $colorBackground;
|
1752 | }
|
1753 | ```
|
1754 |
|
1755 | **Component.jsx**
|
1756 |
|
1757 | Using both `CSS Module` functionality as well as SCSS variables directly in JavaScript.
|
1758 |
|
1759 | ```jsx
|
1760 | import svars from "variables.scss";
|
1761 | import styles from "Component.module.scss";
|
1762 |
|
1763 | // Render DOM with CSS modules class name
|
1764 | // <div className={styles.componentClass}>
|
1765 | // <canvas ref={mountsCanvas}/>
|
1766 | // </div>
|
1767 |
|
1768 | // Somewhere in JavaScript canvas drawing code use the variable directly
|
1769 | // const ctx = mountsCanvas.current.getContext('2d',{alpha: false});
|
1770 | ctx.fillStyle = `${svars.colorBackgroundCanvas}`;
|
1771 | ```
|
1772 |
|
1773 | ## Contributing
|
1774 |
|
1775 | Please take a moment to read our contributing guidelines if you haven't yet done so.
|
1776 |
|
1777 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
|
1778 |
|
1779 | ## License
|
1780 |
|
1781 | [MIT](./LICENSE)
|
1782 |
|
1783 | [npm]: https://img.shields.io/npm/v/css-loader.svg
|
1784 | [npm-url]: https://npmjs.com/package/css-loader
|
1785 | [node]: https://img.shields.io/node/v/css-loader.svg
|
1786 | [node-url]: https://nodejs.org
|
1787 | [deps]: https://david-dm.org/webpack-contrib/css-loader.svg
|
1788 | [deps-url]: https://david-dm.org/webpack-contrib/css-loader
|
1789 | [tests]: https://github.com/webpack-contrib/css-loader/workflows/css-loader/badge.svg
|
1790 | [tests-url]: https://github.com/webpack-contrib/css-loader/actions
|
1791 | [cover]: https://codecov.io/gh/webpack-contrib/css-loader/branch/master/graph/badge.svg
|
1792 | [cover-url]: https://codecov.io/gh/webpack-contrib/css-loader
|
1793 | [chat]: https://badges.gitter.im/webpack/webpack.svg
|
1794 | [chat-url]: https://gitter.im/webpack/webpack
|
1795 | [size]: https://packagephobia.now.sh/badge?p=css-loader
|
1796 | [size-url]: https://packagephobia.now.sh/result?p=css-loader
|