css-loader 5.2.6 → 6.2.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,10 +52,6 @@ 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
57
  ### `toString`
@@ -111,21 +109,24 @@ module.exports = {
111
109
 
112
110
  ## Options
113
111
 
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 |
112
+ | Name | Type | Default | Description |
113
+ | :-----------------------------------: | :-------------------------: | :----------------: | :----------------------------------------------------------------------------------------------------------------------- |
114
+ | **[`url`](#url)** | `{Boolean\|Object}` | `true` | Allows to enables/disables `url()`/`image-set()` functions handling |
115
+ | **[`import`](#import)** | `{Boolean\|Object}` | `true` | Allows to enables/disables `@import` at-rules handling |
116
+ | **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `{auto: true}` | Allows to enables/disables or setup CSS Modules options |
117
+ | **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
118
+ | **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Allows enables/disables or setups number of loaders applied before CSS loader for `@import`/CSS Modules and ICSS imports |
119
+ | **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax |
122
120
 
123
121
  ### `url`
124
122
 
125
- Type: `Boolean|Function`
123
+ Type: `Boolean|Object`
126
124
  Default: `true`
127
125
 
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.
126
+ Allow to enable/disables handling the CSS functions `url` and `image-set`.
127
+ If set to `false`, `css-loader` will not parse any paths specified in `url` or `image-set`.
128
+ A function can also be passed to control this behavior dynamically based on the path to the asset.
129
+ 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
130
 
130
131
  Examples resolutions:
131
132
 
@@ -168,7 +169,7 @@ module.exports = {
168
169
  };
169
170
  ```
170
171
 
171
- #### `Function`
172
+ #### `Object`
172
173
 
173
174
  Allow to filter `url()`. All filtered `url()` will not be resolved (left in the code as they were written).
174
175
 
@@ -182,15 +183,17 @@ module.exports = {
182
183
  test: /\.css$/i,
183
184
  loader: "css-loader",
184
185
  options: {
185
- url: (url, resourcePath) => {
186
- // resourcePath - path to css file
186
+ url: {
187
+ filter: (url, resourcePath) => {
188
+ // resourcePath - path to css file
187
189
 
188
- // Don't handle `img.png` urls
189
- if (url.includes("img.png")) {
190
- return false;
191
- }
190
+ // Don't handle `img.png` urls
191
+ if (url.includes("img.png")) {
192
+ return false;
193
+ }
192
194
 
193
- return true;
195
+ return true;
196
+ },
194
197
  },
195
198
  },
196
199
  },
@@ -201,10 +204,10 @@ module.exports = {
201
204
 
202
205
  ### `import`
203
206
 
204
- Type: `Boolean|Function`
207
+ Type: `Boolean|Object`
205
208
  Default: `true`
206
209
 
207
- Enables/Disables `@import` at-rules handling.
210
+ Allows to enables/disables `@import` at-rules handling.
208
211
  Control `@import` resolving. Absolute urls in `@import` will be moved in runtime code.
209
212
 
210
213
  Examples resolutions:
@@ -249,7 +252,16 @@ module.exports = {
249
252
  };
250
253
  ```
251
254
 
252
- #### `Function`
255
+ #### `Object`
256
+
257
+ | Name | Type | Default | Description |
258
+ | :---------------------: | :----------: | :---------: | :------------------------ |
259
+ | **[`filter`](#filter)** | `{Function}` | `undefined` | Allow to filter `@import` |
260
+
261
+ ##### `filter`
262
+
263
+ Type: `Function`
264
+ Default: `undefined`
253
265
 
254
266
  Allow to filter `@import`. All filtered `@import` will not be resolved (left in the code as they were written).
255
267
 
@@ -263,15 +275,17 @@ module.exports = {
263
275
  test: /\.css$/i,
264
276
  loader: "css-loader",
265
277
  options: {
266
- import: (url, media, resourcePath) => {
267
- // resourcePath - path to css file
278
+ import: {
279
+ filter: (url, media, resourcePath) => {
280
+ // resourcePath - path to css file
268
281
 
269
- // Don't handle `style.css` import
270
- if (url.includes("style.css")) {
271
- return false;
272
- }
282
+ // Don't handle `style.css` import
283
+ if (url.includes("style.css")) {
284
+ return false;
285
+ }
273
286
 
274
- return true;
287
+ return true;
288
+ },
275
289
  },
276
290
  },
277
291
  },
@@ -283,9 +297,15 @@ module.exports = {
283
297
  ### `modules`
284
298
 
285
299
  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)
300
+ Default: `undefined`
301
+
302
+ Allows to enable/disable CSS Modules or ICSS and setup configuration:
287
303
 
288
- Enables/Disables CSS Modules and their configuration.
304
+ - `undefined` - enable CSS modules for all files matching `/\.module\.\w+$/i.test(filename)` and `/\.icss\.\w+$/i.test(filename)` regexp.
305
+ - `true` - enable CSS modules for all files.
306
+ - `false` - disables CSS Modules for all files.
307
+ - `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)
308
+ - `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
309
 
290
310
  The `modules` option enables/disables the **[CSS Modules](https://github.com/css-modules/css-modules)** specification and setup basic behaviour.
291
311
 
@@ -526,13 +546,12 @@ module.exports = {
526
546
  loader: "css-loader",
527
547
  options: {
528
548
  modules: {
529
- compileType: "module",
530
549
  mode: "local",
531
550
  auto: true,
532
551
  exportGlobals: true,
533
552
  localIdentName: "[path][name]__[local]--[hash:base64:5]",
534
553
  localIdentContext: path.resolve(__dirname, "src"),
535
- localIdentHashPrefix: "my-custom-hash",
554
+ localIdentHashSalt: "my-custom-hash",
536
555
  namedExport: true,
537
556
  exportLocalsConvention: "camelCase",
538
557
  exportOnlyLocals: false,
@@ -544,50 +563,26 @@ module.exports = {
544
563
  };
545
564
  ```
546
565
 
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
566
  ##### `auto`
580
567
 
581
568
  Type: `Boolean|RegExp|Function`
582
- Default: `'true'`
569
+ Default: `undefined`
583
570
 
584
- Allows auto enable CSS modules based on filename.
571
+ Allows auto enable CSS modules/ICSS based on filename when `modules` option is object.
572
+
573
+ Possible values:
574
+
575
+ - `undefined` - enable CSS modules for all files.
576
+ - `true` - enable CSS modules for all files matching `/\.module\.\w+$/i.test(filename)` and `/\.icss\.\w+$/i.test(filename)` regexp.
577
+ - `false` - disables CSS Modules.
578
+ - `RegExp` - enable CSS modules for all files matching `/RegExp/i.test(filename)` regexp.
579
+ - `function` - enable CSS Modules for files based on the filename satisfying your filter function check.
585
580
 
586
581
  ###### `Boolean`
587
582
 
588
583
  Possible values:
589
584
 
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
585
+ - `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
586
  - `false` - disables CSS modules or interoperable CSS format based on filename
592
587
 
593
588
  **webpack.config.js**
@@ -665,9 +660,16 @@ Default: `'local'`
665
660
 
666
661
  Setup `mode` option. You can omit the value when you want `local` mode.
667
662
 
663
+ Controls the level of compilation applied to the input styles.
664
+
665
+ The `local`, `global`, and `pure` handles `class` and `id` scoping and `@value` values.
666
+ The `icss` will only compile the low level `Interoperable CSS` format for declaring `:import` and `:export` dependencies between CSS and other languages.
667
+
668
+ ICSS underpins CSS Module support, and provides a low level syntax for other tools to implement CSS-module variations of their own.
669
+
668
670
  ###### `String`
669
671
 
670
- Possible values - `local`, `global`, and `pure`.
672
+ Possible values - `local`, `global`, `pure`, and `icss`.
671
673
 
672
674
  **webpack.config.js**
673
675
 
@@ -693,7 +695,7 @@ module.exports = {
693
695
 
694
696
  Allows set different values for the `mode` option based on a filename
695
697
 
696
- Possible return values - `local`, `global`, and `pure`.
698
+ Possible return values - `local`, `global`, `pure` and `icss`.
697
699
 
698
700
  **webpack.config.js**
699
701
 
@@ -732,7 +734,24 @@ Type: `String`
732
734
  Default: `'[hash:base64]'`
733
735
 
734
736
  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.
737
+
738
+ For more information on options see:
739
+
740
+ - [webpack template strings](https://webpack.js.org/configuration/output/#template-strings),
741
+ - [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest),
742
+ - [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength),
743
+ - [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction),
744
+ - [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).
745
+
746
+ Supported template strings:
747
+
748
+ - [name] the basename of the resource
749
+ - [path] the path of the resource relative to the `compiler.context` option or `modules.localIdentContext` option.
750
+ - [file] - filename and path.
751
+ - [ext] - extension with leading .
752
+ - [hash] - the hash of the string, generated based on `localIdentHashSalt`, `localIdentHashFunction`, `localIdentHashDigest`, `localIdentHashDigestLength`, `localIdentContext`, `resourcePath` and `exportName`
753
+ - [<hashFunction>:hash:<hashDigest>:<hashDigestLength>] - hash with hash settings.
754
+ - [local] - original class.
736
755
 
737
756
  Recommendations:
738
757
 
@@ -790,12 +809,13 @@ module.exports = {
790
809
  };
791
810
  ```
792
811
 
793
- ##### `localIdentHashPrefix`
812
+ ##### `localIdentHashSalt`
794
813
 
795
814
  Type: `String`
796
815
  Default: `undefined`
797
816
 
798
817
  Allows to add custom hash to generate more unique classes.
818
+ For more information see [output.hashSalt](https://webpack.js.org/configuration/output/#outputhashsalt).
799
819
 
800
820
  **webpack.config.js**
801
821
 
@@ -808,7 +828,91 @@ module.exports = {
808
828
  loader: "css-loader",
809
829
  options: {
810
830
  modules: {
811
- localIdentHashPrefix: "hash",
831
+ localIdentHashSalt: "hash",
832
+ },
833
+ },
834
+ },
835
+ ],
836
+ },
837
+ };
838
+ ```
839
+
840
+ ##### `localIdentHashFunction`
841
+
842
+ Type: `String`
843
+ Default: `md4`
844
+
845
+ Allows to specify hash function to generate classes .
846
+ For more information see [output.hashFunction](https://webpack.js.org/configuration/output/#outputhashfunction).
847
+
848
+ **webpack.config.js**
849
+
850
+ ```js
851
+ module.exports = {
852
+ module: {
853
+ rules: [
854
+ {
855
+ test: /\.css$/i,
856
+ loader: "css-loader",
857
+ options: {
858
+ modules: {
859
+ localIdentHashFunction: "md4",
860
+ },
861
+ },
862
+ },
863
+ ],
864
+ },
865
+ };
866
+ ```
867
+
868
+ ##### `localIdentHashDigest`
869
+
870
+ Type: `String`
871
+ Default: `hex`
872
+
873
+ Allows to specify hash digest to generate classes.
874
+ For more information see [output.hashDigest](https://webpack.js.org/configuration/output/#outputhashdigest).
875
+
876
+ **webpack.config.js**
877
+
878
+ ```js
879
+ module.exports = {
880
+ module: {
881
+ rules: [
882
+ {
883
+ test: /\.css$/i,
884
+ loader: "css-loader",
885
+ options: {
886
+ modules: {
887
+ localIdentHashDigest: "base64",
888
+ },
889
+ },
890
+ },
891
+ ],
892
+ },
893
+ };
894
+ ```
895
+
896
+ ##### `localIdentHashDigestLength`
897
+
898
+ Type: `Number`
899
+ Default: `20`
900
+
901
+ Allows to specify hash digest length to generate classes.
902
+ For more information see [output.hashDigestLength](https://webpack.js.org/configuration/output/#outputhashdigestlength).
903
+
904
+ **webpack.config.js**
905
+
906
+ ```js
907
+ module.exports = {
908
+ module: {
909
+ rules: [
910
+ {
911
+ test: /\.css$/i,
912
+ loader: "css-loader",
913
+ options: {
914
+ modules: {
915
+ localIdentHashDigestLength: 5,
812
916
  },
813
917
  },
814
918
  },
@@ -927,6 +1031,9 @@ module.exports = {
927
1031
  };
928
1032
  ```
929
1033
 
1034
+ To set a custom name for namedExport, can use [`exportLocalsConvention`](#exportLocalsConvention) option as a function.
1035
+ Example below in the [`examples`](#examples) section.
1036
+
930
1037
  ##### `exportGlobals`
931
1038
 
932
1039
  Type: `Boolean`
@@ -956,11 +1063,13 @@ module.exports = {
956
1063
 
957
1064
  ##### `exportLocalsConvention`
958
1065
 
959
- Type: `String`
1066
+ Type: `String|Function`
960
1067
  Default: based on the `modules.namedExport` option value, if `true` - `camelCaseOnly`, otherwise `asIs`
961
1068
 
962
1069
  Style of exported class names.
963
1070
 
1071
+ ###### `String`
1072
+
964
1073
  By default, the exported JSON keys mirror the class names (i.e `asIs` value).
965
1074
 
966
1075
  > ⚠ Only `camelCaseOnly` value allowed if you set the `namedExport` value to `true`.
@@ -1006,16 +1115,29 @@ module.exports = {
1006
1115
  };
1007
1116
  ```
1008
1117
 
1009
- ##### `exportOnlyLocals`
1010
-
1011
- Type: `Boolean`
1012
- Default: `false`
1118
+ ###### `Function`
1013
1119
 
1014
- Export only locals.
1120
+ **webpack.config.js**
1015
1121
 
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.
1122
+ ```js
1123
+ module.exports = {
1124
+ module: {
1125
+ rules: [
1126
+ {
1127
+ test: /\.css$/i,
1128
+ loader: "css-loader",
1129
+ options: {
1130
+ modules: {
1131
+ exportLocalsConvention: function (name) {
1132
+ return name.replace(/-/g, "_");
1133
+ },
1134
+ },
1135
+ },
1136
+ },
1137
+ ],
1138
+ },
1139
+ };
1140
+ ```
1019
1141
 
1020
1142
  **webpack.config.js**
1021
1143
 
@@ -1028,7 +1150,15 @@ module.exports = {
1028
1150
  loader: "css-loader",
1029
1151
  options: {
1030
1152
  modules: {
1031
- exportOnlyLocals: true,
1153
+ exportLocalsConvention: function (name) {
1154
+ return [
1155
+ name.replace(/-/g, "_"),
1156
+ // dashesCamelCase
1157
+ name.replace(/-+(\w)/g, (match, firstLetter) =>
1158
+ firstLetter.toUpperCase()
1159
+ ),
1160
+ ];
1161
+ },
1032
1162
  },
1033
1163
  },
1034
1164
  },
@@ -1037,12 +1167,16 @@ module.exports = {
1037
1167
  };
1038
1168
  ```
1039
1169
 
1040
- ### `sourceMap`
1170
+ ##### `exportOnlyLocals`
1041
1171
 
1042
1172
  Type: `Boolean`
1043
- Default: depends on the `compiler.devtool` value
1173
+ Default: `false`
1044
1174
 
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.
1175
+ Export only locals.
1176
+
1177
+ **Useful** when you use **css modules** for pre-rendering (for example SSR).
1178
+ For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
1179
+ It doesn't embed CSS but only exports the identifier mappings.
1046
1180
 
1047
1181
  **webpack.config.js**
1048
1182
 
@@ -1054,7 +1188,9 @@ module.exports = {
1054
1188
  test: /\.css$/i,
1055
1189
  loader: "css-loader",
1056
1190
  options: {
1057
- sourceMap: true,
1191
+ modules: {
1192
+ exportOnlyLocals: true,
1193
+ },
1058
1194
  },
1059
1195
  },
1060
1196
  ],
@@ -1067,9 +1203,9 @@ module.exports = {
1067
1203
  Type: `Number`
1068
1204
  Default: `0`
1069
1205
 
1070
- Enables/Disables or setups number of loaders applied before CSS loader.
1206
+ 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
1207
 
1072
- The option `importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources.
1208
+ 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
1209
 
1074
1210
  **webpack.config.js**
1075
1211
 
@@ -1101,6 +1237,31 @@ module.exports = {
1101
1237
 
1102
1238
  This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
1103
1239
 
1240
+ ### `sourceMap`
1241
+
1242
+ Type: `Boolean`
1243
+ Default: depends on the `compiler.devtool` value
1244
+
1245
+ 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.
1246
+
1247
+ **webpack.config.js**
1248
+
1249
+ ```js
1250
+ module.exports = {
1251
+ module: {
1252
+ rules: [
1253
+ {
1254
+ test: /\.css$/i,
1255
+ loader: "css-loader",
1256
+ options: {
1257
+ sourceMap: true,
1258
+ },
1259
+ },
1260
+ ],
1261
+ },
1262
+ };
1263
+ ```
1264
+
1104
1265
  ### `esModule`
1105
1266
 
1106
1267
  Type: `Boolean`
@@ -1243,29 +1404,13 @@ module.exports = {
1243
1404
  };
1244
1405
  ```
1245
1406
 
1246
- **For webpack v4:**
1407
+ ### Extract
1247
1408
 
1248
- **webpack.config.js**
1409
+ 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
1410
 
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
- ```
1411
+ - 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.
1412
+
1413
+ - 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
1414
 
1270
1415
  ### Pure CSS, CSS modules and PostCSS
1271
1416
 
@@ -1287,7 +1432,7 @@ module.exports = {
1287
1432
  {
1288
1433
  loader: "css-loader",
1289
1434
  options: {
1290
- // Run `postcss-loader` on each CSS `@import`, do not forget that `sass-loader` compile non CSS `@import`'s into a single file
1435
+ // 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
1436
  // If you need run `sass-loader` and `postcss-loader` on each CSS `@import` please set it to `2`
1292
1437
  importLoaders: 1,
1293
1438
  },
@@ -1308,14 +1453,6 @@ module.exports = {
1308
1453
  // More information here https://webpack.js.org/guides/asset-modules/
1309
1454
  type: "asset",
1310
1455
  },
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
1456
  ],
1320
1457
  },
1321
1458
  };
@@ -1354,9 +1491,34 @@ module.exports = {
1354
1491
  };
1355
1492
  ```
1356
1493
 
1494
+ ### Named export with custom export names
1495
+
1496
+ **webpack.config.js**
1497
+
1498
+ ```js
1499
+ module.exports = {
1500
+ module: {
1501
+ rules: [
1502
+ {
1503
+ test: /\.css$/i,
1504
+ loader: "css-loader",
1505
+ options: {
1506
+ modules: {
1507
+ namedExport: true,
1508
+ exportLocalsConvention: function (name) {
1509
+ return name.replace(/-/g, "_");
1510
+ },
1511
+ },
1512
+ },
1513
+ },
1514
+ ],
1515
+ },
1516
+ };
1517
+ ```
1518
+
1357
1519
  ### Separating `Interoperable CSS`-only and `CSS Module` features
1358
1520
 
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.
1521
+ 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
1522
  Meanwhile all files matching `*.module.scss` are treated as `CSS Modules` in this example.
1361
1523
 
1362
1524
  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).
@@ -1375,19 +1537,19 @@ module.exports = {
1375
1537
  exclude: /\.module\.scss$/,
1376
1538
  use: [
1377
1539
  {
1378
- loader: 'style-loader'
1540
+ loader: "style-loader",
1379
1541
  },
1380
1542
  {
1381
- loader: 'css-loader',
1543
+ loader: "css-loader",
1382
1544
  options: {
1383
1545
  importLoaders: 1,
1384
1546
  modules: {
1385
- compileType: 'icss'
1386
- }
1387
- }
1547
+ mode: "icss",
1548
+ },
1549
+ },
1388
1550
  },
1389
1551
  {
1390
- loader: 'sass-loader'
1552
+ loader: "sass-loader",
1391
1553
  },
1392
1554
  ],
1393
1555
  },
@@ -1397,24 +1559,25 @@ module.exports = {
1397
1559
  test: /\.module\.scss$/,
1398
1560
  use: [
1399
1561
  {
1400
- loader: 'style-loader'
1562
+ loader: "style-loader",
1401
1563
  },
1402
1564
  {
1403
- loader: 'css-loader',
1565
+ loader: "css-loader",
1404
1566
  options: {
1405
1567
  importLoaders: 1,
1406
1568
  modules: {
1407
- compileType: 'module'
1408
- }
1409
- }
1569
+ mode: "local",
1570
+ },
1571
+ },
1410
1572
  },
1411
1573
  {
1412
- loader: 'sass-loader'
1574
+ loader: "sass-loader",
1413
1575
  },
1414
1576
  ],
1415
1577
  },
1416
1578
  // --------
1417
1579
  // ...
1580
+ ],
1418
1581
  },
1419
1582
  };
1420
1583
  ```