css-loader 5.2.7 → 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,6 +21,8 @@ The `css-loader` interprets `@import` and `url()` like `import/require()` and wi
21
21
 
22
22
  ## Getting Started
23
23
 
24
+ > ⚠ To use css-loader, webpack@5 is required
25
+
24
26
  To begin, you'll need to install `css-loader`:
25
27
 
26
28
  ```console
@@ -50,82 +52,31 @@ module.exports = {
50
52
  };
51
53
  ```
52
54
 
53
- **Only for webpack v4:**
54
-
55
- Good loaders for requiring your assets are the [file-loader](https://github.com/webpack/file-loader) and the [url-loader](https://github.com/webpack/url-loader) which you should specify in your config (see [below](https://github.com/webpack-contrib/css-loader#assets)).
56
-
57
55
  And run `webpack` via your preferred method.
58
56
 
59
- ### `toString`
60
-
61
- You can also use the css-loader results directly as a string, such as in Angular's component style.
62
-
63
- **webpack.config.js**
64
-
65
- ```js
66
- module.exports = {
67
- module: {
68
- rules: [
69
- {
70
- test: /\.css$/i,
71
- use: ["to-string-loader", "css-loader"],
72
- },
73
- ],
74
- },
75
- };
76
- ```
77
-
78
- or
79
-
80
- ```js
81
- const css = require("./test.css").toString();
82
-
83
- console.log(css); // {String}
84
- ```
85
-
86
- If there are SourceMaps, they will also be included in the result string.
87
-
88
- If, for one reason or another, you need to extract CSS as a
89
- plain string resource (i.e. not wrapped in a JS module) you
90
- might want to check out the [extract-loader](https://github.com/peerigon/extract-loader).
91
- It's useful when you, for instance, need to post process the CSS as a string.
92
-
93
- **webpack.config.js**
94
-
95
- ```js
96
- module.exports = {
97
- module: {
98
- rules: [
99
- {
100
- test: /\.css$/i,
101
- use: [
102
- "handlebars-loader", // handlebars loader expects raw resource string
103
- "extract-loader",
104
- "css-loader",
105
- ],
106
- },
107
- ],
108
- },
109
- };
110
- ```
57
+ If, for one reason or another, you need to extract CSS as a file (i.e. do not store CSS in a JS module) you might want to check out the [recommend example](https://github.com/webpack-contrib/css-loader#recommend).
111
58
 
112
59
  ## Options
113
60
 
114
- | Name | Type | Default | Description |
115
- | :-----------------------------------: | :-------------------------: | :----------------: | :--------------------------------------------------------------------- |
116
- | **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enables/Disables `url`/`image-set` functions handling |
117
- | **[`import`](#import)** | `{Boolean\|Function}` | `true` | Enables/Disables `@import` at-rules handling |
118
- | **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `{auto: true}` | Enables/Disables CSS Modules and their configuration |
119
- | **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
120
- | **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Enables/Disables or setups number of loaders applied before CSS loader |
121
- | **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax |
61
+ | Name | Type | Default | Description |
62
+ | :-----------------------------------: | :------------------------------------------: | :----------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
63
+ | **[`url`](#url)** | `{Boolean\|Object}` | `true` | Allows to enables/disables `url()`/`image-set()` functions handling |
64
+ | **[`import`](#import)** | `{Boolean\|Object}` | `true` | Allows to enables/disables `@import` at-rules handling |
65
+ | **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `{auto: true}` | Allows to enables/disables or setup CSS Modules options |
66
+ | **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
67
+ | **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Allows enables/disables or setups number of loaders applied before CSS loader for `@import`/CSS Modules and ICSS imports |
68
+ | **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax |
69
+ | **[`exportType`](#exporttype)** | `{'array' \| 'string' \| 'css-style-sheet'}` | `array` | Allows exporting styles as array with modules, string or [constructable stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) (i.e. [`CSSStyleSheet`](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet)) |
122
70
 
123
71
  ### `url`
124
72
 
125
- Type: `Boolean|Function`
73
+ Type: `Boolean|Object`
126
74
  Default: `true`
127
75
 
128
- Enables/Disables handling the CSS functions `url` and `image-set`. If set to `false`, `css-loader` will not parse any paths specified in `url` or `image-set`. A function can also be passed to control this behavior dynamically based on the path to the asset. 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.
76
+ Allow to enable/disables handling the CSS functions `url` and `image-set`.
77
+ If set to `false`, `css-loader` will not parse any paths specified in `url` or `image-set`.
78
+ A function can also be passed to control this behavior dynamically based on the path to the asset.
79
+ Starting with version [4.0.0](https://github.com/webpack-contrib/css-loader/blob/master/CHANGELOG.md#400-2020-07-25), absolute paths are parsed based on the server root.
129
80
 
130
81
  Examples resolutions:
131
82
 
@@ -168,7 +119,7 @@ module.exports = {
168
119
  };
169
120
  ```
170
121
 
171
- #### `Function`
122
+ #### `Object`
172
123
 
173
124
  Allow to filter `url()`. All filtered `url()` will not be resolved (left in the code as they were written).
174
125
 
@@ -182,15 +133,17 @@ module.exports = {
182
133
  test: /\.css$/i,
183
134
  loader: "css-loader",
184
135
  options: {
185
- url: (url, resourcePath) => {
186
- // resourcePath - path to css file
136
+ url: {
137
+ filter: (url, resourcePath) => {
138
+ // resourcePath - path to css file
187
139
 
188
- // Don't handle `img.png` urls
189
- if (url.includes("img.png")) {
190
- return false;
191
- }
140
+ // Don't handle `img.png` urls
141
+ if (url.includes("img.png")) {
142
+ return false;
143
+ }
192
144
 
193
- return true;
145
+ return true;
146
+ },
194
147
  },
195
148
  },
196
149
  },
@@ -201,10 +154,10 @@ module.exports = {
201
154
 
202
155
  ### `import`
203
156
 
204
- Type: `Boolean|Function`
157
+ Type: `Boolean|Object`
205
158
  Default: `true`
206
159
 
207
- Enables/Disables `@import` at-rules handling.
160
+ Allows to enables/disables `@import` at-rules handling.
208
161
  Control `@import` resolving. Absolute urls in `@import` will be moved in runtime code.
209
162
 
210
163
  Examples resolutions:
@@ -249,7 +202,16 @@ module.exports = {
249
202
  };
250
203
  ```
251
204
 
252
- #### `Function`
205
+ #### `Object`
206
+
207
+ | Name | Type | Default | Description |
208
+ | :---------------------: | :----------: | :---------: | :------------------------ |
209
+ | **[`filter`](#filter)** | `{Function}` | `undefined` | Allow to filter `@import` |
210
+
211
+ ##### `filter`
212
+
213
+ Type: `Function`
214
+ Default: `undefined`
253
215
 
254
216
  Allow to filter `@import`. All filtered `@import` will not be resolved (left in the code as they were written).
255
217
 
@@ -263,15 +225,17 @@ module.exports = {
263
225
  test: /\.css$/i,
264
226
  loader: "css-loader",
265
227
  options: {
266
- import: (url, media, resourcePath) => {
267
- // resourcePath - path to css file
228
+ import: {
229
+ filter: (url, media, resourcePath) => {
230
+ // resourcePath - path to css file
268
231
 
269
- // Don't handle `style.css` import
270
- if (url.includes("style.css")) {
271
- return false;
272
- }
232
+ // Don't handle `style.css` import
233
+ if (url.includes("style.css")) {
234
+ return false;
235
+ }
273
236
 
274
- return true;
237
+ return true;
238
+ },
275
239
  },
276
240
  },
277
241
  },
@@ -283,9 +247,15 @@ module.exports = {
283
247
  ### `modules`
284
248
 
285
249
  Type: `Boolean|String|Object`
286
- Default: based on filename, `true` for all files matching `/\.module\.\w+$/i.test(filename)` regular expression, more information you can read [here](https://github.com/webpack-contrib/css-loader#auto)
250
+ Default: `undefined`
251
+
252
+ Allows to enable/disable CSS Modules or ICSS and setup configuration:
287
253
 
288
- Enables/Disables CSS Modules and their configuration.
254
+ - `undefined` - enable CSS modules for all files matching `/\.module\.\w+$/i.test(filename)` and `/\.icss\.\w+$/i.test(filename)` regexp.
255
+ - `true` - enable CSS modules for all files.
256
+ - `false` - disables CSS Modules for all files.
257
+ - `string` - disables CSS Modules for all files and set the `mode` option, more information you can read [here](https://github.com/webpack-contrib/css-loader#mode)
258
+ - `object` - enable CSS modules for all files, if `modules.auto` option is not specified, otherwise the `modules.auto` option will determine whether if it is CSS modules or not, more information you can read [here](https://github.com/webpack-contrib/css-loader#auto)
289
259
 
290
260
  The `modules` option enables/disables the **[CSS Modules](https://github.com/css-modules/css-modules)** specification and setup basic behaviour.
291
261
 
@@ -526,13 +496,12 @@ module.exports = {
526
496
  loader: "css-loader",
527
497
  options: {
528
498
  modules: {
529
- compileType: "module",
530
499
  mode: "local",
531
500
  auto: true,
532
501
  exportGlobals: true,
533
502
  localIdentName: "[path][name]__[local]--[hash:base64:5]",
534
503
  localIdentContext: path.resolve(__dirname, "src"),
535
- localIdentHashPrefix: "my-custom-hash",
504
+ localIdentHashSalt: "my-custom-hash",
536
505
  namedExport: true,
537
506
  exportLocalsConvention: "camelCase",
538
507
  exportOnlyLocals: false,
@@ -544,50 +513,26 @@ module.exports = {
544
513
  };
545
514
  ```
546
515
 
547
- ##### `compileType`
548
-
549
- Type: `'module' | 'icss'`
550
- Default: `'module'`
551
-
552
- Controls the level of compilation applied to the input styles.
553
-
554
- The `module` handles `class` and `id` scoping and `@value` values.
555
- The `icss` will only compile the low level `Interoperable CSS` format for declaring `:import` and `:export` dependencies between CSS and other languages.
556
-
557
- ICSS underpins CSS Module support, and provides a low level syntax for other tools to implement CSS-module variations of their own.
558
-
559
- **webpack.config.js**
560
-
561
- ```js
562
- module.exports = {
563
- module: {
564
- rules: [
565
- {
566
- test: /\.css$/i,
567
- loader: "css-loader",
568
- options: {
569
- modules: {
570
- compileType: "icss",
571
- },
572
- },
573
- },
574
- ],
575
- },
576
- };
577
- ```
578
-
579
516
  ##### `auto`
580
517
 
581
518
  Type: `Boolean|RegExp|Function`
582
- Default: `'true'`
519
+ Default: `undefined`
583
520
 
584
- Allows auto enable CSS modules based on filename.
521
+ Allows auto enable CSS modules/ICSS based on filename when `modules` option is object.
522
+
523
+ Possible values:
524
+
525
+ - `undefined` - enable CSS modules for all files.
526
+ - `true` - enable CSS modules for all files matching `/\.module\.\w+$/i.test(filename)` and `/\.icss\.\w+$/i.test(filename)` regexp.
527
+ - `false` - disables CSS Modules.
528
+ - `RegExp` - enable CSS modules for all files matching `/RegExp/i.test(filename)` regexp.
529
+ - `function` - enable CSS Modules for files based on the filename satisfying your filter function check.
585
530
 
586
531
  ###### `Boolean`
587
532
 
588
533
  Possible values:
589
534
 
590
- - `true` - enables CSS modules or interoperable CSS format, sets the [`modules.compileType`](#compiletype) option to `module` value for all files which satisfy `/\.module(s)?\.\w+$/i.test(filename)` condition or sets the [`modules.compileType`](#compiletype) option to `icss` value for all files which satisfy `/\.icss\.\w+$/i.test(filename)` condition
535
+ - `true` - enables CSS modules or interoperable CSS format, sets the [`modules.mode`](#mode) option to `local` value for all files which satisfy `/\.module(s)?\.\w+$/i.test(filename)` condition or sets the [`modules.mode`](#mode) option to `icss` value for all files which satisfy `/\.icss\.\w+$/i.test(filename)` condition
591
536
  - `false` - disables CSS modules or interoperable CSS format based on filename
592
537
 
593
538
  **webpack.config.js**
@@ -665,9 +610,16 @@ Default: `'local'`
665
610
 
666
611
  Setup `mode` option. You can omit the value when you want `local` mode.
667
612
 
613
+ Controls the level of compilation applied to the input styles.
614
+
615
+ The `local`, `global`, and `pure` handles `class` and `id` scoping and `@value` values.
616
+ The `icss` will only compile the low level `Interoperable CSS` format for declaring `:import` and `:export` dependencies between CSS and other languages.
617
+
618
+ ICSS underpins CSS Module support, and provides a low level syntax for other tools to implement CSS-module variations of their own.
619
+
668
620
  ###### `String`
669
621
 
670
- Possible values - `local`, `global`, and `pure`.
622
+ Possible values - `local`, `global`, `pure`, and `icss`.
671
623
 
672
624
  **webpack.config.js**
673
625
 
@@ -693,7 +645,7 @@ module.exports = {
693
645
 
694
646
  Allows set different values for the `mode` option based on a filename
695
647
 
696
- Possible return values - `local`, `global`, and `pure`.
648
+ Possible return values - `local`, `global`, `pure` and `icss`.
697
649
 
698
650
  **webpack.config.js**
699
651
 
@@ -732,7 +684,25 @@ Type: `String`
732
684
  Default: `'[hash:base64]'`
733
685
 
734
686
  Allows to configure the generated local ident name.
735
- See [loader-utils's documentation](https://github.com/webpack/loader-utils#interpolatename) for more information on options.
687
+
688
+ For more information on options see:
689
+
690
+ - [webpack template strings](https://webpack.js.org/configuration/output/#template-strings),
691
+ - [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest),
692
+ - [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength),
693
+ - [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction),
694
+ - [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).
695
+
696
+ Supported template strings:
697
+
698
+ - `[name]` the basename of the resource
699
+ - `[folder]` the folder the resource relative to the `compiler.context` option or `modules.localIdentContext` option.
700
+ - `[path]` the path of the resource relative to the `compiler.context` option or `modules.localIdentContext` option.
701
+ - `[file]` - filename and path.
702
+ - `[ext]` - extension with leading `.`.
703
+ - `[hash]` - the hash of the string, generated based on `localIdentHashSalt`, `localIdentHashFunction`, `localIdentHashDigest`, `localIdentHashDigestLength`, `localIdentContext`, `resourcePath` and `exportName`
704
+ - `[<hashFunction>:hash:<hashDigest>:<hashDigestLength>]` - hash with hash settings.
705
+ - `[local]` - original class.
736
706
 
737
707
  Recommendations:
738
708
 
@@ -790,12 +760,97 @@ module.exports = {
790
760
  };
791
761
  ```
792
762
 
793
- ##### `localIdentHashPrefix`
763
+ ##### `localIdentHashSalt`
794
764
 
795
765
  Type: `String`
796
766
  Default: `undefined`
797
767
 
798
768
  Allows to add custom hash to generate more unique classes.
769
+ For more information see [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).
770
+
771
+ **webpack.config.js**
772
+
773
+ ```js
774
+ module.exports = {
775
+ module: {
776
+ rules: [
777
+ {
778
+ test: /\.css$/i,
779
+ loader: "css-loader",
780
+ options: {
781
+ modules: {
782
+ localIdentHashSalt: "hash",
783
+ },
784
+ },
785
+ },
786
+ ],
787
+ },
788
+ };
789
+ ```
790
+
791
+ ##### `localIdentHashFunction`
792
+
793
+ Type: `String`
794
+ Default: `md4`
795
+
796
+ Allows to specify hash function to generate classes .
797
+ For more information see [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction).
798
+
799
+ **webpack.config.js**
800
+
801
+ ```js
802
+ module.exports = {
803
+ module: {
804
+ rules: [
805
+ {
806
+ test: /\.css$/i,
807
+ loader: "css-loader",
808
+ options: {
809
+ modules: {
810
+ localIdentHashFunction: "md4",
811
+ },
812
+ },
813
+ },
814
+ ],
815
+ },
816
+ };
817
+ ```
818
+
819
+ ##### `localIdentHashDigest`
820
+
821
+ Type: `String`
822
+ Default: `hex`
823
+
824
+ Allows to specify hash digest to generate classes.
825
+ For more information see [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest).
826
+
827
+ **webpack.config.js**
828
+
829
+ ```js
830
+ module.exports = {
831
+ module: {
832
+ rules: [
833
+ {
834
+ test: /\.css$/i,
835
+ loader: "css-loader",
836
+ options: {
837
+ modules: {
838
+ localIdentHashDigest: "base64",
839
+ },
840
+ },
841
+ },
842
+ ],
843
+ },
844
+ };
845
+ ```
846
+
847
+ ##### `localIdentHashDigestLength`
848
+
849
+ Type: `Number`
850
+ Default: `20`
851
+
852
+ Allows to specify hash digest length to generate classes.
853
+ For more information see [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength).
799
854
 
800
855
  **webpack.config.js**
801
856
 
@@ -808,7 +863,7 @@ module.exports = {
808
863
  loader: "css-loader",
809
864
  options: {
810
865
  modules: {
811
- localIdentHashPrefix: "hash",
866
+ localIdentHashDigestLength: 5,
812
867
  },
813
868
  },
814
869
  },
@@ -927,6 +982,9 @@ module.exports = {
927
982
  };
928
983
  ```
929
984
 
985
+ To set a custom name for namedExport, can use [`exportLocalsConvention`](#exportLocalsConvention) option as a function.
986
+ Example below in the [`examples`](#examples) section.
987
+
930
988
  ##### `exportGlobals`
931
989
 
932
990
  Type: `Boolean`
@@ -956,11 +1014,13 @@ module.exports = {
956
1014
 
957
1015
  ##### `exportLocalsConvention`
958
1016
 
959
- Type: `String`
1017
+ Type: `String|Function`
960
1018
  Default: based on the `modules.namedExport` option value, if `true` - `camelCaseOnly`, otherwise `asIs`
961
1019
 
962
1020
  Style of exported class names.
963
1021
 
1022
+ ###### `String`
1023
+
964
1024
  By default, the exported JSON keys mirror the class names (i.e `asIs` value).
965
1025
 
966
1026
  > ⚠ Only `camelCaseOnly` value allowed if you set the `namedExport` value to `true`.
@@ -1006,16 +1066,29 @@ module.exports = {
1006
1066
  };
1007
1067
  ```
1008
1068
 
1009
- ##### `exportOnlyLocals`
1010
-
1011
- Type: `Boolean`
1012
- Default: `false`
1069
+ ###### `Function`
1013
1070
 
1014
- Export only locals.
1071
+ **webpack.config.js**
1015
1072
 
1016
- **Useful** when you use **css modules** for pre-rendering (for example SSR).
1017
- For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
1018
- It doesn't embed CSS but only exports the identifier mappings.
1073
+ ```js
1074
+ module.exports = {
1075
+ module: {
1076
+ rules: [
1077
+ {
1078
+ test: /\.css$/i,
1079
+ loader: "css-loader",
1080
+ options: {
1081
+ modules: {
1082
+ exportLocalsConvention: function (name) {
1083
+ return name.replace(/-/g, "_");
1084
+ },
1085
+ },
1086
+ },
1087
+ },
1088
+ ],
1089
+ },
1090
+ };
1091
+ ```
1019
1092
 
1020
1093
  **webpack.config.js**
1021
1094
 
@@ -1028,7 +1101,15 @@ module.exports = {
1028
1101
  loader: "css-loader",
1029
1102
  options: {
1030
1103
  modules: {
1031
- exportOnlyLocals: true,
1104
+ exportLocalsConvention: function (name) {
1105
+ return [
1106
+ name.replace(/-/g, "_"),
1107
+ // dashesCamelCase
1108
+ name.replace(/-+(\w)/g, (match, firstLetter) =>
1109
+ firstLetter.toUpperCase()
1110
+ ),
1111
+ ];
1112
+ },
1032
1113
  },
1033
1114
  },
1034
1115
  },
@@ -1037,12 +1118,16 @@ module.exports = {
1037
1118
  };
1038
1119
  ```
1039
1120
 
1040
- ### `sourceMap`
1121
+ ##### `exportOnlyLocals`
1041
1122
 
1042
1123
  Type: `Boolean`
1043
- Default: depends on the `compiler.devtool` value
1124
+ Default: `false`
1044
1125
 
1045
- 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.
1126
+ Export only locals.
1127
+
1128
+ **Useful** when you use **css modules** for pre-rendering (for example SSR).
1129
+ For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
1130
+ It doesn't embed CSS but only exports the identifier mappings.
1046
1131
 
1047
1132
  **webpack.config.js**
1048
1133
 
@@ -1054,7 +1139,9 @@ module.exports = {
1054
1139
  test: /\.css$/i,
1055
1140
  loader: "css-loader",
1056
1141
  options: {
1057
- sourceMap: true,
1142
+ modules: {
1143
+ exportOnlyLocals: true,
1144
+ },
1058
1145
  },
1059
1146
  },
1060
1147
  ],
@@ -1067,9 +1154,9 @@ module.exports = {
1067
1154
  Type: `Number`
1068
1155
  Default: `0`
1069
1156
 
1070
- Enables/Disables or setups number of loaders applied before CSS loader.
1157
+ Allows to enables/disables or setups number of loaders applied before CSS loader for `@import` at-rules, CSS modules and ICSS imports, i.e. `@import`/`composes`/`@value value from './values.css'`/etc.
1071
1158
 
1072
- The option `importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources.
1159
+ The option `importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources and CSS modules/ICSS imports.
1073
1160
 
1074
1161
  **webpack.config.js**
1075
1162
 
@@ -1101,6 +1188,31 @@ module.exports = {
1101
1188
 
1102
1189
  This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
1103
1190
 
1191
+ ### `sourceMap`
1192
+
1193
+ Type: `Boolean`
1194
+ Default: depends on the `compiler.devtool` value
1195
+
1196
+ By default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option. All values enable source map generation except `eval` and `false` value.
1197
+
1198
+ **webpack.config.js**
1199
+
1200
+ ```js
1201
+ module.exports = {
1202
+ module: {
1203
+ rules: [
1204
+ {
1205
+ test: /\.css$/i,
1206
+ loader: "css-loader",
1207
+ options: {
1208
+ sourceMap: true,
1209
+ },
1210
+ },
1211
+ ],
1212
+ },
1213
+ };
1214
+ ```
1215
+
1104
1216
  ### `esModule`
1105
1217
 
1106
1218
  Type: `Boolean`
@@ -1129,6 +1241,203 @@ module.exports = {
1129
1241
  };
1130
1242
  ```
1131
1243
 
1244
+ ### `exportType`
1245
+
1246
+ Type: `'array' | 'string' | 'css-style-sheet'`
1247
+ Default: `'array'`
1248
+
1249
+ Allows exporting styles as array with modules, string or [constructable stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) (i.e. [`CSSStyleSheet`](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet)).
1250
+ Default value is `'array'`, i.e. loader exports array of modules with specific API which is used in `style-loader` or other.
1251
+
1252
+ **webpack.config.js**
1253
+
1254
+ ```js
1255
+ module.exports = {
1256
+ module: {
1257
+ rules: [
1258
+ {
1259
+ assert: { type: "css" },
1260
+ loader: "css-loader",
1261
+ options: {
1262
+ exportType: "css-style-sheet",
1263
+ },
1264
+ },
1265
+ ],
1266
+ },
1267
+ };
1268
+ ```
1269
+
1270
+ **src/index.js**
1271
+
1272
+ ```js
1273
+ import sheet from "./styles.css" assert { type: "css" };
1274
+
1275
+ document.adoptedStyleSheets = [sheet];
1276
+ shadowRoot.adoptedStyleSheets = [sheet];
1277
+ ```
1278
+
1279
+ #### `'array'`
1280
+
1281
+ The default export is array of modules with specific API which is used in `style-loader` or other.
1282
+
1283
+ **webpack.config.js**
1284
+
1285
+ ```js
1286
+ module.exports = {
1287
+ module: {
1288
+ rules: [
1289
+ {
1290
+ test: /\.(sa|sc|c)ss$/i,
1291
+ use: ["style-loader", "css-loader", "postcss-loader", "sass-loader"],
1292
+ },
1293
+ ],
1294
+ },
1295
+ };
1296
+ ```
1297
+
1298
+ **src/index.js**
1299
+
1300
+ ```js
1301
+ // `style-loader` applies styles to DOM
1302
+ import "./styles.css";
1303
+ ```
1304
+
1305
+ #### `'string'`
1306
+
1307
+ > ⚠ You don't need [`style-loader`](https://github.com/webpack-contrib/style-loader) anymore, please remove it.
1308
+ > ⚠ The `esModules` option should be enabled if you want to use it with [`CSS modules`](https://github.com/webpack-contrib/css-loader#modules), by default for locals will be used [named export](https://github.com/webpack-contrib/css-loader#namedexport).
1309
+
1310
+ The default export is `string`.
1311
+
1312
+ **webpack.config.js**
1313
+
1314
+ ```js
1315
+ module.exports = {
1316
+ module: {
1317
+ rules: [
1318
+ {
1319
+ test: /\.(sa|sc|c)ss$/i,
1320
+ use: ["css-loader", "postcss-loader", "sass-loader"],
1321
+ },
1322
+ ],
1323
+ },
1324
+ };
1325
+ ```
1326
+
1327
+ **src/index.js**
1328
+
1329
+ ```js
1330
+ import sheet from "./styles.css";
1331
+
1332
+ console.log(sheet);
1333
+ ```
1334
+
1335
+ #### `'css-style-sheet'`
1336
+
1337
+ > ⚠ `@import` rules not yet allowed, more [information](https://web.dev/css-module-scripts/#@import-rules-not-yet-allowed)
1338
+ > ⚠ You don't need [`style-loader`](https://github.com/webpack-contrib/style-loader) anymore, please remove it.
1339
+ > ⚠ The `esModules` option should be enabled if you want to use it with [`CSS modules`](https://github.com/webpack-contrib/css-loader#modules), by default for locals will be used [named export](https://github.com/webpack-contrib/css-loader#namedexport).
1340
+ > ⚠ Source maps are not currently supported in `Chrome` due [bug](https://bugs.chromium.org/p/chromium/issues/detail?id=1174094&q=CSSStyleSheet%20source%20maps&can=2)
1341
+
1342
+ The default export is a [constructable stylesheet](https://developers.google.com/web/updates/2019/02/constructable-stylesheets) (i.e. [`CSSStyleSheet`](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet)).
1343
+
1344
+ Useful for [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) and shadow DOM.
1345
+
1346
+ More information:
1347
+
1348
+ - [Using CSS Module Scripts to import stylesheets](https://web.dev/css-module-scripts/)
1349
+ - [Constructable Stylesheets: seamless reusable styles](https://developers.google.com/web/updates/2019/02/constructable-stylesheets)
1350
+
1351
+ **webpack.config.js**
1352
+
1353
+ ```js
1354
+ module.exports = {
1355
+ module: {
1356
+ rules: [
1357
+ {
1358
+ assert: { type: "css" },
1359
+ loader: "css-loader",
1360
+ options: {
1361
+ exportType: "css-style-sheet",
1362
+ },
1363
+ },
1364
+
1365
+ // For Sass/SCSS:
1366
+ //
1367
+ // {
1368
+ // assert: { type: "css" },
1369
+ // rules: [
1370
+ // {
1371
+ // loader: "css-loader",
1372
+ // options: {
1373
+ // exportType: "css-style-sheet",
1374
+ // // Other options
1375
+ // },
1376
+ // },
1377
+ // {
1378
+ // loader: "sass-loader",
1379
+ // options: {
1380
+ // // Other options
1381
+ // },
1382
+ // },
1383
+ // ],
1384
+ // },
1385
+ ],
1386
+ },
1387
+ };
1388
+ ```
1389
+
1390
+ **src/index.js**
1391
+
1392
+ ```js
1393
+ // Example for Sass/SCSS:
1394
+ // import sheet from "./styles.scss" assert { type: "css" };
1395
+
1396
+ // Example for CSS modules:
1397
+ // import sheet, { myClass } from "./styles.scss" assert { type: "css" };
1398
+
1399
+ // Example for CSS:
1400
+ import sheet from "./styles.css" assert { type: "css" };
1401
+
1402
+ document.adoptedStyleSheets = [sheet];
1403
+ shadowRoot.adoptedStyleSheets = [sheet];
1404
+ ```
1405
+
1406
+ For migration purposes, you can use the following configuration:
1407
+
1408
+ ```js
1409
+ module.exports = {
1410
+ module: {
1411
+ rules: [
1412
+ {
1413
+ test: /\.css$/i,
1414
+ oneOf: [
1415
+ {
1416
+ assert: { type: "css" },
1417
+ loader: "css-loader",
1418
+ options: {
1419
+ exportType: "css-style-sheet",
1420
+ // Other options
1421
+ },
1422
+ },
1423
+ {
1424
+ use: [
1425
+ "style-loader",
1426
+ {
1427
+ loader: "css-loader",
1428
+ options: {
1429
+ // Other options
1430
+ },
1431
+ },
1432
+ ],
1433
+ },
1434
+ ],
1435
+ },
1436
+ ],
1437
+ },
1438
+ };
1439
+ ```
1440
+
1132
1441
  ## Examples
1133
1442
 
1134
1443
  ### Recommend
@@ -1149,7 +1458,7 @@ module.exports = {
1149
1458
  module: {
1150
1459
  rules: [
1151
1460
  {
1152
- test: /\.(sa|sc|c)ss$/,
1461
+ test: /\.(sa|sc|c)ss$/i,
1153
1462
  use: [
1154
1463
  devMode ? "style-loader" : MiniCssExtractPlugin.loader,
1155
1464
  "css-loader",
@@ -1243,29 +1552,13 @@ module.exports = {
1243
1552
  };
1244
1553
  ```
1245
1554
 
1246
- **For webpack v4:**
1555
+ ### Extract
1247
1556
 
1248
- **webpack.config.js**
1557
+ For production builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.
1249
1558
 
1250
- ```js
1251
- module.exports = {
1252
- module: {
1253
- rules: [
1254
- {
1255
- test: /\.css$/i,
1256
- use: ["style-loader", "css-loader"],
1257
- },
1258
- {
1259
- test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
1260
- loader: "url-loader",
1261
- options: {
1262
- limit: 8192,
1263
- },
1264
- },
1265
- ],
1266
- },
1267
- };
1268
- ```
1559
+ - This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to extract the CSS when running in production mode.
1560
+
1561
+ - As an alternative, if seeking better development performance and css outputs that mimic production. [extract-css-chunks-webpack-plugin](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin) offers a hot module reload friendly, extended version of mini-css-extract-plugin. HMR real CSS files in dev, works like mini-css in non-dev
1269
1562
 
1270
1563
  ### Pure CSS, CSS modules and PostCSS
1271
1564
 
@@ -1287,7 +1580,7 @@ module.exports = {
1287
1580
  {
1288
1581
  loader: "css-loader",
1289
1582
  options: {
1290
- // Run `postcss-loader` on each CSS `@import`, do not forget that `sass-loader` compile non CSS `@import`'s into a single file
1583
+ // Run `postcss-loader` on each CSS `@import` and CSS modules/ICSS imports, do not forget that `sass-loader` compile non CSS `@import`'s into a single file
1291
1584
  // If you need run `sass-loader` and `postcss-loader` on each CSS `@import` please set it to `2`
1292
1585
  importLoaders: 1,
1293
1586
  },
@@ -1308,14 +1601,6 @@ module.exports = {
1308
1601
  // More information here https://webpack.js.org/guides/asset-modules/
1309
1602
  type: "asset",
1310
1603
  },
1311
- // For webpack v4
1312
- // {
1313
- // test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
1314
- // loader: "url-loader",
1315
- // options: {
1316
- // limit: 8192,
1317
- // },
1318
- // },
1319
1604
  ],
1320
1605
  },
1321
1606
  };
@@ -1354,9 +1639,34 @@ module.exports = {
1354
1639
  };
1355
1640
  ```
1356
1641
 
1642
+ ### Named export with custom export names
1643
+
1644
+ **webpack.config.js**
1645
+
1646
+ ```js
1647
+ module.exports = {
1648
+ module: {
1649
+ rules: [
1650
+ {
1651
+ test: /\.css$/i,
1652
+ loader: "css-loader",
1653
+ options: {
1654
+ modules: {
1655
+ namedExport: true,
1656
+ exportLocalsConvention: function (name) {
1657
+ return name.replace(/-/g, "_");
1658
+ },
1659
+ },
1660
+ },
1661
+ },
1662
+ ],
1663
+ },
1664
+ };
1665
+ ```
1666
+
1357
1667
  ### Separating `Interoperable CSS`-only and `CSS Module` features
1358
1668
 
1359
- 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 `compileType` 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.
1669
+ The following setup is an example of allowing `Interoperable CSS` features only (such as `:import` and `:export`) without using further `CSS Module` functionality by setting `mode` option for all files that do not match `*.module.scss` naming convention. This is for reference as having `ICSS` features applied to all files was default `css-loader` behavior before v4.
1360
1670
  Meanwhile all files matching `*.module.scss` are treated as `CSS Modules` in this example.
1361
1671
 
1362
1672
  An example case is assumed where a project requires canvas drawing variables to be synchronized with CSS - canvas drawing uses the same color (set by color name in JavaScript) as HTML background (set by class name in CSS).
@@ -1371,50 +1681,51 @@ module.exports = {
1371
1681
  // --------
1372
1682
  // SCSS ALL EXCEPT MODULES
1373
1683
  {
1374
- test: /\.scss$/,
1375
- exclude: /\.module\.scss$/,
1684
+ test: /\.scss$/i,
1685
+ exclude: /\.module\.scss$/i,
1376
1686
  use: [
1377
1687
  {
1378
- loader: 'style-loader'
1688
+ loader: "style-loader",
1379
1689
  },
1380
1690
  {
1381
- loader: 'css-loader',
1691
+ loader: "css-loader",
1382
1692
  options: {
1383
1693
  importLoaders: 1,
1384
1694
  modules: {
1385
- compileType: 'icss'
1386
- }
1387
- }
1695
+ mode: "icss",
1696
+ },
1697
+ },
1388
1698
  },
1389
1699
  {
1390
- loader: 'sass-loader'
1700
+ loader: "sass-loader",
1391
1701
  },
1392
1702
  ],
1393
1703
  },
1394
1704
  // --------
1395
1705
  // SCSS MODULES
1396
1706
  {
1397
- test: /\.module\.scss$/,
1707
+ test: /\.module\.scss$/i,
1398
1708
  use: [
1399
1709
  {
1400
- loader: 'style-loader'
1710
+ loader: "style-loader",
1401
1711
  },
1402
1712
  {
1403
- loader: 'css-loader',
1713
+ loader: "css-loader",
1404
1714
  options: {
1405
1715
  importLoaders: 1,
1406
1716
  modules: {
1407
- compileType: 'module'
1408
- }
1409
- }
1717
+ mode: "local",
1718
+ },
1719
+ },
1410
1720
  },
1411
1721
  {
1412
- loader: 'sass-loader'
1722
+ loader: "sass-loader",
1413
1723
  },
1414
1724
  ],
1415
1725
  },
1416
1726
  // --------
1417
1727
  // ...
1728
+ ],
1418
1729
  },
1419
1730
  };
1420
1731
  ```