css-loader 5.2.4 → 6.0.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 +254 -150
- package/dist/index.js +32 -29
- package/dist/options.json +38 -15
- package/dist/plugins/postcss-icss-parser.js +1 -0
- package/dist/plugins/postcss-import-parser.js +6 -0
- package/dist/plugins/postcss-url-parser.js +43 -8
- package/dist/runtime/cssWithMappingToString.js +5 -1
- package/dist/runtime/getUrl.js +6 -7
- package/dist/utils.js +295 -79
- package/package.json +30 -33
- package/CHANGELOG.md +0 -631
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,22 +109,24 @@ module.exports = {
|
|
|
111
109
|
|
|
112
110
|
## Options
|
|
113
111
|
|
|
114
|
-
| Name | Type | Default | Description
|
|
115
|
-
| :-----------------------------------: | :-------------------------: | :----------------: |
|
|
116
|
-
| **[`url`](#url)** |
|
|
117
|
-
| **[`import`](#import)** |
|
|
118
|
-
| **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `{auto: true}` |
|
|
119
|
-
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps
|
|
120
|
-
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` |
|
|
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|
|
|
123
|
+
Type: `Boolean|Object`
|
|
126
124
|
Default: `true`
|
|
127
125
|
|
|
128
|
-
|
|
129
|
-
|
|
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.
|
|
130
130
|
|
|
131
131
|
Examples resolutions:
|
|
132
132
|
|
|
@@ -169,7 +169,7 @@ module.exports = {
|
|
|
169
169
|
};
|
|
170
170
|
```
|
|
171
171
|
|
|
172
|
-
#### `
|
|
172
|
+
#### `Object`
|
|
173
173
|
|
|
174
174
|
Allow to filter `url()`. All filtered `url()` will not be resolved (left in the code as they were written).
|
|
175
175
|
|
|
@@ -183,15 +183,17 @@ module.exports = {
|
|
|
183
183
|
test: /\.css$/i,
|
|
184
184
|
loader: "css-loader",
|
|
185
185
|
options: {
|
|
186
|
-
url:
|
|
187
|
-
|
|
186
|
+
url: {
|
|
187
|
+
filter: (url, resourcePath) => {
|
|
188
|
+
// resourcePath - path to css file
|
|
188
189
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
190
|
+
// Don't handle `img.png` urls
|
|
191
|
+
if (url.includes("img.png")) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
193
194
|
|
|
194
|
-
|
|
195
|
+
return true;
|
|
196
|
+
},
|
|
195
197
|
},
|
|
196
198
|
},
|
|
197
199
|
},
|
|
@@ -202,10 +204,10 @@ module.exports = {
|
|
|
202
204
|
|
|
203
205
|
### `import`
|
|
204
206
|
|
|
205
|
-
Type: `Boolean|
|
|
207
|
+
Type: `Boolean|Object`
|
|
206
208
|
Default: `true`
|
|
207
209
|
|
|
208
|
-
|
|
210
|
+
Allows to enables/disables `@import` at-rules handling.
|
|
209
211
|
Control `@import` resolving. Absolute urls in `@import` will be moved in runtime code.
|
|
210
212
|
|
|
211
213
|
Examples resolutions:
|
|
@@ -250,7 +252,16 @@ module.exports = {
|
|
|
250
252
|
};
|
|
251
253
|
```
|
|
252
254
|
|
|
253
|
-
#### `
|
|
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`
|
|
254
265
|
|
|
255
266
|
Allow to filter `@import`. All filtered `@import` will not be resolved (left in the code as they were written).
|
|
256
267
|
|
|
@@ -264,15 +275,17 @@ module.exports = {
|
|
|
264
275
|
test: /\.css$/i,
|
|
265
276
|
loader: "css-loader",
|
|
266
277
|
options: {
|
|
267
|
-
import:
|
|
268
|
-
|
|
278
|
+
import: {
|
|
279
|
+
filter: (url, media, resourcePath) => {
|
|
280
|
+
// resourcePath - path to css file
|
|
269
281
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
282
|
+
// Don't handle `style.css` import
|
|
283
|
+
if (url.includes("style.css")) {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
274
286
|
|
|
275
|
-
|
|
287
|
+
return true;
|
|
288
|
+
},
|
|
276
289
|
},
|
|
277
290
|
},
|
|
278
291
|
},
|
|
@@ -284,9 +297,15 @@ module.exports = {
|
|
|
284
297
|
### `modules`
|
|
285
298
|
|
|
286
299
|
Type: `Boolean|String|Object`
|
|
287
|
-
Default:
|
|
300
|
+
Default: `undefined`
|
|
288
301
|
|
|
289
|
-
|
|
302
|
+
Allows to enable/disable CSS Modules or ICSS and setup configuration:
|
|
303
|
+
|
|
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)
|
|
290
309
|
|
|
291
310
|
The `modules` option enables/disables the **[CSS Modules](https://github.com/css-modules/css-modules)** specification and setup basic behaviour.
|
|
292
311
|
|
|
@@ -527,13 +546,12 @@ module.exports = {
|
|
|
527
546
|
loader: "css-loader",
|
|
528
547
|
options: {
|
|
529
548
|
modules: {
|
|
530
|
-
compileType: "module",
|
|
531
549
|
mode: "local",
|
|
532
550
|
auto: true,
|
|
533
551
|
exportGlobals: true,
|
|
534
552
|
localIdentName: "[path][name]__[local]--[hash:base64:5]",
|
|
535
553
|
localIdentContext: path.resolve(__dirname, "src"),
|
|
536
|
-
|
|
554
|
+
localIdentHashSalt: "my-custom-hash",
|
|
537
555
|
namedExport: true,
|
|
538
556
|
exportLocalsConvention: "camelCase",
|
|
539
557
|
exportOnlyLocals: false,
|
|
@@ -545,50 +563,26 @@ module.exports = {
|
|
|
545
563
|
};
|
|
546
564
|
```
|
|
547
565
|
|
|
548
|
-
##### `compileType`
|
|
549
|
-
|
|
550
|
-
Type: `'module' | 'icss'`
|
|
551
|
-
Default: `'module'`
|
|
552
|
-
|
|
553
|
-
Controls the level of compilation applied to the input styles.
|
|
554
|
-
|
|
555
|
-
The `module` handles `class` and `id` scoping and `@value` values.
|
|
556
|
-
The `icss` will only compile the low level `Interoperable CSS` format for declaring `:import` and `:export` dependencies between CSS and other languages.
|
|
557
|
-
|
|
558
|
-
ICSS underpins CSS Module support, and provides a low level syntax for other tools to implement CSS-module variations of their own.
|
|
559
|
-
|
|
560
|
-
**webpack.config.js**
|
|
561
|
-
|
|
562
|
-
```js
|
|
563
|
-
module.exports = {
|
|
564
|
-
module: {
|
|
565
|
-
rules: [
|
|
566
|
-
{
|
|
567
|
-
test: /\.css$/i,
|
|
568
|
-
loader: "css-loader",
|
|
569
|
-
options: {
|
|
570
|
-
modules: {
|
|
571
|
-
compileType: "icss",
|
|
572
|
-
},
|
|
573
|
-
},
|
|
574
|
-
},
|
|
575
|
-
],
|
|
576
|
-
},
|
|
577
|
-
};
|
|
578
|
-
```
|
|
579
|
-
|
|
580
566
|
##### `auto`
|
|
581
567
|
|
|
582
568
|
Type: `Boolean|RegExp|Function`
|
|
583
|
-
Default: `
|
|
569
|
+
Default: `undefined`
|
|
570
|
+
|
|
571
|
+
Allows auto enable CSS modules/ICSS based on filename when `modules` option is object.
|
|
572
|
+
|
|
573
|
+
Possible values:
|
|
584
574
|
|
|
585
|
-
|
|
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.
|
|
586
580
|
|
|
587
581
|
###### `Boolean`
|
|
588
582
|
|
|
589
583
|
Possible values:
|
|
590
584
|
|
|
591
|
-
- `true` - enables CSS modules or interoperable CSS format, sets the [`modules.
|
|
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
|
|
592
586
|
- `false` - disables CSS modules or interoperable CSS format based on filename
|
|
593
587
|
|
|
594
588
|
**webpack.config.js**
|
|
@@ -666,9 +660,16 @@ Default: `'local'`
|
|
|
666
660
|
|
|
667
661
|
Setup `mode` option. You can omit the value when you want `local` mode.
|
|
668
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
|
+
|
|
669
670
|
###### `String`
|
|
670
671
|
|
|
671
|
-
Possible values - `local`, `global`, and `
|
|
672
|
+
Possible values - `local`, `global`, `pure`, and `icss`.
|
|
672
673
|
|
|
673
674
|
**webpack.config.js**
|
|
674
675
|
|
|
@@ -694,7 +695,7 @@ module.exports = {
|
|
|
694
695
|
|
|
695
696
|
Allows set different values for the `mode` option based on a filename
|
|
696
697
|
|
|
697
|
-
Possible return values - `local`, `global`, and `
|
|
698
|
+
Possible return values - `local`, `global`, `pure` and `icss`.
|
|
698
699
|
|
|
699
700
|
**webpack.config.js**
|
|
700
701
|
|
|
@@ -733,7 +734,24 @@ Type: `String`
|
|
|
733
734
|
Default: `'[hash:base64]'`
|
|
734
735
|
|
|
735
736
|
Allows to configure the generated local ident name.
|
|
736
|
-
|
|
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.
|
|
737
755
|
|
|
738
756
|
Recommendations:
|
|
739
757
|
|
|
@@ -791,12 +809,97 @@ module.exports = {
|
|
|
791
809
|
};
|
|
792
810
|
```
|
|
793
811
|
|
|
794
|
-
##### `
|
|
812
|
+
##### `localIdentHashSalt`
|
|
795
813
|
|
|
796
814
|
Type: `String`
|
|
797
815
|
Default: `undefined`
|
|
798
816
|
|
|
799
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).
|
|
819
|
+
|
|
820
|
+
**webpack.config.js**
|
|
821
|
+
|
|
822
|
+
```js
|
|
823
|
+
module.exports = {
|
|
824
|
+
module: {
|
|
825
|
+
rules: [
|
|
826
|
+
{
|
|
827
|
+
test: /\.css$/i,
|
|
828
|
+
loader: "css-loader",
|
|
829
|
+
options: {
|
|
830
|
+
modules: {
|
|
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).
|
|
800
903
|
|
|
801
904
|
**webpack.config.js**
|
|
802
905
|
|
|
@@ -809,7 +912,7 @@ module.exports = {
|
|
|
809
912
|
loader: "css-loader",
|
|
810
913
|
options: {
|
|
811
914
|
modules: {
|
|
812
|
-
|
|
915
|
+
localIdentHashDigestLength: 5,
|
|
813
916
|
},
|
|
814
917
|
},
|
|
815
918
|
},
|
|
@@ -1038,39 +1141,14 @@ module.exports = {
|
|
|
1038
1141
|
};
|
|
1039
1142
|
```
|
|
1040
1143
|
|
|
1041
|
-
### `sourceMap`
|
|
1042
|
-
|
|
1043
|
-
Type: `Boolean`
|
|
1044
|
-
Default: depends on the `compiler.devtool` value
|
|
1045
|
-
|
|
1046
|
-
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.
|
|
1047
|
-
|
|
1048
|
-
**webpack.config.js**
|
|
1049
|
-
|
|
1050
|
-
```js
|
|
1051
|
-
module.exports = {
|
|
1052
|
-
module: {
|
|
1053
|
-
rules: [
|
|
1054
|
-
{
|
|
1055
|
-
test: /\.css$/i,
|
|
1056
|
-
loader: "css-loader",
|
|
1057
|
-
options: {
|
|
1058
|
-
sourceMap: true,
|
|
1059
|
-
},
|
|
1060
|
-
},
|
|
1061
|
-
],
|
|
1062
|
-
},
|
|
1063
|
-
};
|
|
1064
|
-
```
|
|
1065
|
-
|
|
1066
1144
|
### `importLoaders`
|
|
1067
1145
|
|
|
1068
1146
|
Type: `Number`
|
|
1069
1147
|
Default: `0`
|
|
1070
1148
|
|
|
1071
|
-
|
|
1149
|
+
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.
|
|
1072
1150
|
|
|
1073
|
-
The option `importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources.
|
|
1151
|
+
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.
|
|
1074
1152
|
|
|
1075
1153
|
**webpack.config.js**
|
|
1076
1154
|
|
|
@@ -1102,6 +1180,31 @@ module.exports = {
|
|
|
1102
1180
|
|
|
1103
1181
|
This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
|
|
1104
1182
|
|
|
1183
|
+
### `sourceMap`
|
|
1184
|
+
|
|
1185
|
+
Type: `Boolean`
|
|
1186
|
+
Default: depends on the `compiler.devtool` value
|
|
1187
|
+
|
|
1188
|
+
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.
|
|
1189
|
+
|
|
1190
|
+
**webpack.config.js**
|
|
1191
|
+
|
|
1192
|
+
```js
|
|
1193
|
+
module.exports = {
|
|
1194
|
+
module: {
|
|
1195
|
+
rules: [
|
|
1196
|
+
{
|
|
1197
|
+
test: /\.css$/i,
|
|
1198
|
+
loader: "css-loader",
|
|
1199
|
+
options: {
|
|
1200
|
+
sourceMap: true,
|
|
1201
|
+
},
|
|
1202
|
+
},
|
|
1203
|
+
],
|
|
1204
|
+
},
|
|
1205
|
+
};
|
|
1206
|
+
```
|
|
1207
|
+
|
|
1105
1208
|
### `esModule`
|
|
1106
1209
|
|
|
1107
1210
|
Type: `Boolean`
|
|
@@ -1132,6 +1235,38 @@ module.exports = {
|
|
|
1132
1235
|
|
|
1133
1236
|
## Examples
|
|
1134
1237
|
|
|
1238
|
+
### Recommend
|
|
1239
|
+
|
|
1240
|
+
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.
|
|
1241
|
+
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.
|
|
1242
|
+
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.
|
|
1243
|
+
|
|
1244
|
+
> i Do not use together `style-loader` and `mini-css-extract-plugin`.
|
|
1245
|
+
|
|
1246
|
+
**webpack.config.js**
|
|
1247
|
+
|
|
1248
|
+
```js
|
|
1249
|
+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
|
|
1250
|
+
const devMode = process.env.NODE_ENV !== "production";
|
|
1251
|
+
|
|
1252
|
+
module.exports = {
|
|
1253
|
+
module: {
|
|
1254
|
+
rules: [
|
|
1255
|
+
{
|
|
1256
|
+
test: /\.(sa|sc|c)ss$/,
|
|
1257
|
+
use: [
|
|
1258
|
+
devMode ? "style-loader" : MiniCssExtractPlugin.loader,
|
|
1259
|
+
"css-loader",
|
|
1260
|
+
"postcss-loader",
|
|
1261
|
+
"sass-loader",
|
|
1262
|
+
],
|
|
1263
|
+
},
|
|
1264
|
+
],
|
|
1265
|
+
},
|
|
1266
|
+
plugins: [].concat(devMode ? [] : [new MiniCssExtractPlugin()]),
|
|
1267
|
+
};
|
|
1268
|
+
```
|
|
1269
|
+
|
|
1135
1270
|
### Disable url resolving using the `/* webpackIgnore: true */` comment
|
|
1136
1271
|
|
|
1137
1272
|
With the help of the `/* webpackIgnore: true */`comment, it is possible to disable sources handling for rules and for individual declarations.
|
|
@@ -1212,30 +1347,6 @@ module.exports = {
|
|
|
1212
1347
|
};
|
|
1213
1348
|
```
|
|
1214
1349
|
|
|
1215
|
-
**For webpack v4:**
|
|
1216
|
-
|
|
1217
|
-
**webpack.config.js**
|
|
1218
|
-
|
|
1219
|
-
```js
|
|
1220
|
-
module.exports = {
|
|
1221
|
-
module: {
|
|
1222
|
-
rules: [
|
|
1223
|
-
{
|
|
1224
|
-
test: /\.css$/i,
|
|
1225
|
-
use: ["style-loader", "css-loader"],
|
|
1226
|
-
},
|
|
1227
|
-
{
|
|
1228
|
-
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
|
|
1229
|
-
loader: "url-loader",
|
|
1230
|
-
options: {
|
|
1231
|
-
limit: 8192,
|
|
1232
|
-
},
|
|
1233
|
-
},
|
|
1234
|
-
],
|
|
1235
|
-
},
|
|
1236
|
-
};
|
|
1237
|
-
```
|
|
1238
|
-
|
|
1239
1350
|
### Extract
|
|
1240
1351
|
|
|
1241
1352
|
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.
|
|
@@ -1264,7 +1375,7 @@ module.exports = {
|
|
|
1264
1375
|
{
|
|
1265
1376
|
loader: "css-loader",
|
|
1266
1377
|
options: {
|
|
1267
|
-
// Run `postcss-loader` on each CSS `@import
|
|
1378
|
+
// 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
|
|
1268
1379
|
// If you need run `sass-loader` and `postcss-loader` on each CSS `@import` please set it to `2`
|
|
1269
1380
|
importLoaders: 1,
|
|
1270
1381
|
},
|
|
@@ -1285,14 +1396,6 @@ module.exports = {
|
|
|
1285
1396
|
// More information here https://webpack.js.org/guides/asset-modules/
|
|
1286
1397
|
type: "asset",
|
|
1287
1398
|
},
|
|
1288
|
-
// For webpack v4
|
|
1289
|
-
// {
|
|
1290
|
-
// test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
|
|
1291
|
-
// loader: "url-loader",
|
|
1292
|
-
// options: {
|
|
1293
|
-
// limit: 8192,
|
|
1294
|
-
// },
|
|
1295
|
-
// },
|
|
1296
1399
|
],
|
|
1297
1400
|
},
|
|
1298
1401
|
};
|
|
@@ -1333,7 +1436,7 @@ module.exports = {
|
|
|
1333
1436
|
|
|
1334
1437
|
### Separating `Interoperable CSS`-only and `CSS Module` features
|
|
1335
1438
|
|
|
1336
|
-
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 `
|
|
1439
|
+
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.
|
|
1337
1440
|
Meanwhile all files matching `*.module.scss` are treated as `CSS Modules` in this example.
|
|
1338
1441
|
|
|
1339
1442
|
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).
|
|
@@ -1352,19 +1455,19 @@ module.exports = {
|
|
|
1352
1455
|
exclude: /\.module\.scss$/,
|
|
1353
1456
|
use: [
|
|
1354
1457
|
{
|
|
1355
|
-
loader:
|
|
1458
|
+
loader: "style-loader",
|
|
1356
1459
|
},
|
|
1357
1460
|
{
|
|
1358
|
-
loader:
|
|
1461
|
+
loader: "css-loader",
|
|
1359
1462
|
options: {
|
|
1360
1463
|
importLoaders: 1,
|
|
1361
1464
|
modules: {
|
|
1362
|
-
|
|
1363
|
-
}
|
|
1364
|
-
}
|
|
1465
|
+
mode: "icss",
|
|
1466
|
+
},
|
|
1467
|
+
},
|
|
1365
1468
|
},
|
|
1366
1469
|
{
|
|
1367
|
-
loader:
|
|
1470
|
+
loader: "sass-loader",
|
|
1368
1471
|
},
|
|
1369
1472
|
],
|
|
1370
1473
|
},
|
|
@@ -1374,24 +1477,25 @@ module.exports = {
|
|
|
1374
1477
|
test: /\.module\.scss$/,
|
|
1375
1478
|
use: [
|
|
1376
1479
|
{
|
|
1377
|
-
loader:
|
|
1480
|
+
loader: "style-loader",
|
|
1378
1481
|
},
|
|
1379
1482
|
{
|
|
1380
|
-
loader:
|
|
1483
|
+
loader: "css-loader",
|
|
1381
1484
|
options: {
|
|
1382
1485
|
importLoaders: 1,
|
|
1383
1486
|
modules: {
|
|
1384
|
-
|
|
1385
|
-
}
|
|
1386
|
-
}
|
|
1487
|
+
mode: "local",
|
|
1488
|
+
},
|
|
1489
|
+
},
|
|
1387
1490
|
},
|
|
1388
1491
|
{
|
|
1389
|
-
loader:
|
|
1492
|
+
loader: "sass-loader",
|
|
1390
1493
|
},
|
|
1391
1494
|
],
|
|
1392
1495
|
},
|
|
1393
1496
|
// --------
|
|
1394
1497
|
// ...
|
|
1498
|
+
],
|
|
1395
1499
|
},
|
|
1396
1500
|
};
|
|
1397
1501
|
```
|