source: frontend/node_modules/css-loader/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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