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