css-loader 2.1.1 → 3.2.1
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/CHANGELOG.md +70 -1
- package/README.md +317 -102
- package/dist/index.js +45 -181
- package/dist/options.json +47 -40
- package/dist/plugins/postcss-icss-parser.js +48 -62
- package/dist/plugins/postcss-import-parser.js +4 -6
- package/dist/plugins/postcss-url-parser.js +60 -40
- package/dist/runtime/api.js +15 -10
- package/dist/runtime/getUrl.js +34 -0
- package/dist/utils.js +292 -31
- package/package.json +54 -84
- package/dist/runtime/url-escape.js +0 -20
package/README.md
CHANGED
|
@@ -42,7 +42,7 @@ module.exports = {
|
|
|
42
42
|
module: {
|
|
43
43
|
rules: [
|
|
44
44
|
{
|
|
45
|
-
test: /\.css
|
|
45
|
+
test: /\.css$/i,
|
|
46
46
|
use: ['style-loader', 'css-loader'],
|
|
47
47
|
},
|
|
48
48
|
],
|
|
@@ -65,7 +65,7 @@ module.exports = {
|
|
|
65
65
|
module: {
|
|
66
66
|
rules: [
|
|
67
67
|
{
|
|
68
|
-
test: /\.css
|
|
68
|
+
test: /\.css$/i,
|
|
69
69
|
use: ['to-string-loader', 'css-loader'],
|
|
70
70
|
},
|
|
71
71
|
],
|
|
@@ -95,7 +95,7 @@ module.exports = {
|
|
|
95
95
|
module: {
|
|
96
96
|
rules: [
|
|
97
97
|
{
|
|
98
|
-
test: /\.css
|
|
98
|
+
test: /\.css$/i,
|
|
99
99
|
use: [
|
|
100
100
|
'handlebars-loader', // handlebars loader expects raw resource string
|
|
101
101
|
'extract-loader',
|
|
@@ -109,25 +109,22 @@ module.exports = {
|
|
|
109
109
|
|
|
110
110
|
## Options
|
|
111
111
|
|
|
112
|
-
| Name |
|
|
113
|
-
| :-----------------------------------------: |
|
|
114
|
-
| **[`url`](#url)** |
|
|
115
|
-
| **[`import`](#import)** |
|
|
116
|
-
| **[`modules`](#modules)** |
|
|
117
|
-
|
|
|
118
|
-
|
|
|
119
|
-
|
|
|
120
|
-
|
|
|
121
|
-
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
|
|
122
|
-
| **[`camelCase`](#camelcase)** | `{Boolean\|String}` | `false` | Export Classnames in CamelCase |
|
|
123
|
-
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
|
|
124
|
-
| **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
|
|
112
|
+
| Name | Type | Default | Description |
|
|
113
|
+
| :-----------------------------------------: | :-------------------------: | :------: | :--------------------------------------------------------------------- |
|
|
114
|
+
| **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enables/Disables `url`/`image-set` functions handling |
|
|
115
|
+
| **[`import`](#import)** | `{Boolean\|Function}` | `true` | Enables/Disables `@import` at-rules handling |
|
|
116
|
+
| **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `false` | Enables/Disables CSS Modules and their configuration |
|
|
117
|
+
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enables/Disables generation of source maps |
|
|
118
|
+
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Enables/Disables or setups number of loaders applied before CSS loader |
|
|
119
|
+
| **[`localsConvention`](#localsconvention)** | `{String}` | `'asIs'` | Style of exported classnames |
|
|
120
|
+
| **[`onlyLocals`](#onlylocals)** | `{Boolean}` | `false` | Export only locals |
|
|
125
121
|
|
|
126
122
|
### `url`
|
|
127
123
|
|
|
128
124
|
Type: `Boolean|Function`
|
|
129
125
|
Default: `true`
|
|
130
126
|
|
|
127
|
+
Enables/Disables `url`/`image-set` functions handling.
|
|
131
128
|
Control `url()` resolving. Absolute URLs and root-relative URLs are not resolving.
|
|
132
129
|
|
|
133
130
|
Examples resolutions:
|
|
@@ -160,7 +157,7 @@ module.exports = {
|
|
|
160
157
|
module: {
|
|
161
158
|
rules: [
|
|
162
159
|
{
|
|
163
|
-
test: /\.css
|
|
160
|
+
test: /\.css$/i,
|
|
164
161
|
loader: 'css-loader',
|
|
165
162
|
options: {
|
|
166
163
|
url: true,
|
|
@@ -182,14 +179,18 @@ module.exports = {
|
|
|
182
179
|
module: {
|
|
183
180
|
rules: [
|
|
184
181
|
{
|
|
185
|
-
test: /\.css
|
|
182
|
+
test: /\.css$/i,
|
|
186
183
|
loader: 'css-loader',
|
|
187
184
|
options: {
|
|
188
185
|
url: (url, resourcePath) => {
|
|
189
186
|
// resourcePath - path to css file
|
|
190
187
|
|
|
191
|
-
//
|
|
192
|
-
|
|
188
|
+
// Don't handle `img.png` urls
|
|
189
|
+
if (url.includes('img.png')) {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return true;
|
|
193
194
|
},
|
|
194
195
|
},
|
|
195
196
|
},
|
|
@@ -200,9 +201,10 @@ module.exports = {
|
|
|
200
201
|
|
|
201
202
|
### `import`
|
|
202
203
|
|
|
203
|
-
Type: `Boolean`
|
|
204
|
+
Type: `Boolean|Function`
|
|
204
205
|
Default: `true`
|
|
205
206
|
|
|
207
|
+
Enables/Disables `@import` at-rules handling.
|
|
206
208
|
Control `@import` resolving. Absolute urls in `@import` will be moved in runtime code.
|
|
207
209
|
|
|
208
210
|
Examples resolutions:
|
|
@@ -236,7 +238,7 @@ module.exports = {
|
|
|
236
238
|
module: {
|
|
237
239
|
rules: [
|
|
238
240
|
{
|
|
239
|
-
test: /\.css
|
|
241
|
+
test: /\.css$/i,
|
|
240
242
|
loader: 'css-loader',
|
|
241
243
|
options: {
|
|
242
244
|
import: true,
|
|
@@ -258,7 +260,7 @@ module.exports = {
|
|
|
258
260
|
module: {
|
|
259
261
|
rules: [
|
|
260
262
|
{
|
|
261
|
-
test: /\.css
|
|
263
|
+
test: /\.css$/i,
|
|
262
264
|
loader: 'css-loader',
|
|
263
265
|
options: {
|
|
264
266
|
import: (parsedImport, resourcePath) => {
|
|
@@ -266,8 +268,12 @@ module.exports = {
|
|
|
266
268
|
// parsedImport.media - media query of `@import`
|
|
267
269
|
// resourcePath - path to css file
|
|
268
270
|
|
|
269
|
-
//
|
|
270
|
-
|
|
271
|
+
// Don't handle `style.css` import
|
|
272
|
+
if (parsedImport.url.includes('style.css')) {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return true;
|
|
271
277
|
},
|
|
272
278
|
},
|
|
273
279
|
},
|
|
@@ -276,24 +282,17 @@ module.exports = {
|
|
|
276
282
|
};
|
|
277
283
|
```
|
|
278
284
|
|
|
279
|
-
###
|
|
285
|
+
### `modules`
|
|
280
286
|
|
|
281
|
-
Type: `Boolean|String`
|
|
287
|
+
Type: `Boolean|String|Object`
|
|
282
288
|
Default: `false`
|
|
283
289
|
|
|
284
|
-
|
|
290
|
+
Enables/Disables CSS Modules and their configuration.
|
|
285
291
|
|
|
286
|
-
|
|
287
|
-
| :------------: | :---------: | :------------------------------------------------------------------------------------------------------------------------------- |
|
|
288
|
-
| **`true`** | `{Boolean}` | Enables local scoped CSS by default (use **local** mode by default) |
|
|
289
|
-
| **`false`** | `{Boolean}` | Disable the **CSS Modules** spec, all **CSS Modules** features (like `@value`, `:local`, `:global` and `composes`) will not work |
|
|
290
|
-
| **`'local'`** | `{String}` | Enables local scoped CSS by default (same as `true` value) |
|
|
291
|
-
| **`'global'`** | `{String}` | Enables global scoped CSS by default |
|
|
292
|
+
The `modules` option enables/disables the **[CSS Modules](https://github.com/css-modules/css-modules)** specification and setup basic behaviour.
|
|
292
293
|
|
|
293
294
|
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.
|
|
294
295
|
|
|
295
|
-
You can read about **modes** below.
|
|
296
|
-
|
|
297
296
|
**webpack.config.js**
|
|
298
297
|
|
|
299
298
|
```js
|
|
@@ -301,7 +300,7 @@ module.exports = {
|
|
|
301
300
|
module: {
|
|
302
301
|
rules: [
|
|
303
302
|
{
|
|
304
|
-
test: /\.css
|
|
303
|
+
test: /\.css$/i,
|
|
305
304
|
loader: 'css-loader',
|
|
306
305
|
options: {
|
|
307
306
|
modules: true,
|
|
@@ -312,6 +311,8 @@ module.exports = {
|
|
|
312
311
|
};
|
|
313
312
|
```
|
|
314
313
|
|
|
314
|
+
#### `Features`
|
|
315
|
+
|
|
315
316
|
##### `Scope`
|
|
316
317
|
|
|
317
318
|
Using `local` value requires you to specify `:global` classes.
|
|
@@ -324,7 +325,7 @@ Styles can be locally scoped to avoid globally scoping styles.
|
|
|
324
325
|
The syntax `:local(.className)` can be used to declare `className` in the local scope. The local identifiers are exported by the module.
|
|
325
326
|
|
|
326
327
|
With `:local` (without brackets) local mode can be switched on for this selector.
|
|
327
|
-
The `:global(.className)`
|
|
328
|
+
The `:global(.className)` notation can be used to declare an explicit global selector.
|
|
328
329
|
With `:global` (without brackets) global mode can be switched on for this selector.
|
|
329
330
|
|
|
330
331
|
The loader replaces local selectors with unique identifiers. The chosen unique identifiers are exported by the module.
|
|
@@ -437,14 +438,149 @@ To import from multiple modules use multiple `composes:` rules.
|
|
|
437
438
|
}
|
|
438
439
|
```
|
|
439
440
|
|
|
440
|
-
|
|
441
|
+
##### `Values`
|
|
442
|
+
|
|
443
|
+
You can use `@value` to specific values to be reused throughout a document.
|
|
444
|
+
|
|
445
|
+
We recommend use prefix `v-` for values, `s-` for selectors and `m-` for media at-rules.
|
|
446
|
+
|
|
447
|
+
```css
|
|
448
|
+
@value v-primary: #BF4040;
|
|
449
|
+
@value s-black: black-selector;
|
|
450
|
+
@value m-large: (min-width: 960px);
|
|
451
|
+
|
|
452
|
+
.header {
|
|
453
|
+
color: v-primary;
|
|
454
|
+
padding: 0 10px;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
.s-black {
|
|
458
|
+
color: black;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
@media m-large {
|
|
462
|
+
.header {
|
|
463
|
+
padding: 0 20px;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
#### `Boolean`
|
|
469
|
+
|
|
470
|
+
Enable **CSS Modules** features.
|
|
471
|
+
|
|
472
|
+
**webpack.config.js**
|
|
473
|
+
|
|
474
|
+
```js
|
|
475
|
+
module.exports = {
|
|
476
|
+
module: {
|
|
477
|
+
rules: [
|
|
478
|
+
{
|
|
479
|
+
test: /\.css$/i,
|
|
480
|
+
loader: 'css-loader',
|
|
481
|
+
options: {
|
|
482
|
+
modules: true,
|
|
483
|
+
},
|
|
484
|
+
},
|
|
485
|
+
],
|
|
486
|
+
},
|
|
487
|
+
};
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
#### `String`
|
|
491
|
+
|
|
492
|
+
Enable **CSS Modules** features and setup `mode`.
|
|
493
|
+
|
|
494
|
+
**webpack.config.js**
|
|
495
|
+
|
|
496
|
+
```js
|
|
497
|
+
module.exports = {
|
|
498
|
+
module: {
|
|
499
|
+
rules: [
|
|
500
|
+
{
|
|
501
|
+
test: /\.css$/i,
|
|
502
|
+
loader: 'css-loader',
|
|
503
|
+
options: {
|
|
504
|
+
// Using `local` value has same effect like using `modules: true`
|
|
505
|
+
modules: 'global',
|
|
506
|
+
},
|
|
507
|
+
},
|
|
508
|
+
],
|
|
509
|
+
},
|
|
510
|
+
};
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
#### `Object`
|
|
514
|
+
|
|
515
|
+
Enable **CSS Modules** features and setup options for them.
|
|
516
|
+
|
|
517
|
+
**webpack.config.js**
|
|
518
|
+
|
|
519
|
+
```js
|
|
520
|
+
module.exports = {
|
|
521
|
+
module: {
|
|
522
|
+
rules: [
|
|
523
|
+
{
|
|
524
|
+
test: /\.css$/i,
|
|
525
|
+
loader: 'css-loader',
|
|
526
|
+
options: {
|
|
527
|
+
modules: {
|
|
528
|
+
mode: 'local',
|
|
529
|
+
localIdentName: '[path][name]__[local]--[hash:base64:5]',
|
|
530
|
+
context: path.resolve(__dirname, 'src'),
|
|
531
|
+
hashPrefix: 'my-custom-hash',
|
|
532
|
+
},
|
|
533
|
+
},
|
|
534
|
+
},
|
|
535
|
+
],
|
|
536
|
+
},
|
|
537
|
+
};
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
##### `mode`
|
|
541
|
+
|
|
542
|
+
Type: `String`
|
|
543
|
+
Default: `'local'`
|
|
544
|
+
|
|
545
|
+
Setup `mode` option. You can omit the value when you want `local` mode.
|
|
546
|
+
|
|
547
|
+
**webpack.config.js**
|
|
548
|
+
|
|
549
|
+
```js
|
|
550
|
+
module.exports = {
|
|
551
|
+
module: {
|
|
552
|
+
rules: [
|
|
553
|
+
{
|
|
554
|
+
test: /\.css$/i,
|
|
555
|
+
loader: 'css-loader',
|
|
556
|
+
options: {
|
|
557
|
+
modules: {
|
|
558
|
+
mode: 'global',
|
|
559
|
+
},
|
|
560
|
+
},
|
|
561
|
+
},
|
|
562
|
+
],
|
|
563
|
+
},
|
|
564
|
+
};
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
##### `localIdentName`
|
|
441
568
|
|
|
442
569
|
Type: `String`
|
|
443
|
-
Default: `[hash:base64]`
|
|
570
|
+
Default: `'[hash:base64]'`
|
|
444
571
|
|
|
445
572
|
You can configure the generated ident with the `localIdentName` query parameter.
|
|
446
573
|
See [loader-utils's documentation](https://github.com/webpack/loader-utils#interpolatename) for more information on options.
|
|
447
574
|
|
|
575
|
+
Recommendations:
|
|
576
|
+
|
|
577
|
+
- use `'[path][name]__[local]'` for development
|
|
578
|
+
- use `'[hash:base64]'` for production
|
|
579
|
+
|
|
580
|
+
The `[local]` placeholder contains original class.
|
|
581
|
+
|
|
582
|
+
**Note:** all reserved (`<>:"/\|?*`) and control filesystem characters (excluding characters in the `[local]` placeholder) will be converted to `-`.
|
|
583
|
+
|
|
448
584
|
**webpack.config.js**
|
|
449
585
|
|
|
450
586
|
```js
|
|
@@ -452,11 +588,12 @@ module.exports = {
|
|
|
452
588
|
module: {
|
|
453
589
|
rules: [
|
|
454
590
|
{
|
|
455
|
-
test: /\.css
|
|
591
|
+
test: /\.css$/i,
|
|
456
592
|
loader: 'css-loader',
|
|
457
593
|
options: {
|
|
458
|
-
modules:
|
|
459
|
-
|
|
594
|
+
modules: {
|
|
595
|
+
localIdentName: '[path][name]__[local]--[hash:base64:5]',
|
|
596
|
+
},
|
|
460
597
|
},
|
|
461
598
|
},
|
|
462
599
|
],
|
|
@@ -464,7 +601,7 @@ module.exports = {
|
|
|
464
601
|
};
|
|
465
602
|
```
|
|
466
603
|
|
|
467
|
-
|
|
604
|
+
##### `context`
|
|
468
605
|
|
|
469
606
|
Type: `String`
|
|
470
607
|
Default: `undefined`
|
|
@@ -479,11 +616,12 @@ module.exports = {
|
|
|
479
616
|
module: {
|
|
480
617
|
rules: [
|
|
481
618
|
{
|
|
482
|
-
test: /\.css
|
|
619
|
+
test: /\.css$/i,
|
|
483
620
|
loader: 'css-loader',
|
|
484
621
|
options: {
|
|
485
|
-
modules:
|
|
486
|
-
|
|
622
|
+
modules: {
|
|
623
|
+
context: path.resolve(__dirname, 'context'),
|
|
624
|
+
},
|
|
487
625
|
},
|
|
488
626
|
},
|
|
489
627
|
],
|
|
@@ -491,7 +629,7 @@ module.exports = {
|
|
|
491
629
|
};
|
|
492
630
|
```
|
|
493
631
|
|
|
494
|
-
|
|
632
|
+
##### `hashPrefix`
|
|
495
633
|
|
|
496
634
|
Type: `String`
|
|
497
635
|
Default: `undefined`
|
|
@@ -505,11 +643,12 @@ module.exports = {
|
|
|
505
643
|
module: {
|
|
506
644
|
rules: [
|
|
507
645
|
{
|
|
508
|
-
test: /\.css
|
|
646
|
+
test: /\.css$/i,
|
|
509
647
|
loader: 'css-loader',
|
|
510
648
|
options: {
|
|
511
|
-
modules:
|
|
512
|
-
|
|
649
|
+
modules: {
|
|
650
|
+
hashPrefix: 'hash',
|
|
651
|
+
},
|
|
513
652
|
},
|
|
514
653
|
},
|
|
515
654
|
],
|
|
@@ -517,7 +656,7 @@ module.exports = {
|
|
|
517
656
|
};
|
|
518
657
|
```
|
|
519
658
|
|
|
520
|
-
|
|
659
|
+
##### `getLocalIdent`
|
|
521
660
|
|
|
522
661
|
Type: `Function`
|
|
523
662
|
Default: `undefined`
|
|
@@ -532,12 +671,13 @@ module.exports = {
|
|
|
532
671
|
module: {
|
|
533
672
|
rules: [
|
|
534
673
|
{
|
|
535
|
-
test: /\.css
|
|
674
|
+
test: /\.css$/i,
|
|
536
675
|
loader: 'css-loader',
|
|
537
676
|
options: {
|
|
538
|
-
modules:
|
|
539
|
-
|
|
540
|
-
|
|
677
|
+
modules: {
|
|
678
|
+
getLocalIdent: (context, localIdentName, localName, options) => {
|
|
679
|
+
return 'whatever_random_class_name';
|
|
680
|
+
},
|
|
541
681
|
},
|
|
542
682
|
},
|
|
543
683
|
},
|
|
@@ -546,16 +686,10 @@ module.exports = {
|
|
|
546
686
|
};
|
|
547
687
|
```
|
|
548
688
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
Type: `Boolean`
|
|
552
|
-
Default: `false`
|
|
553
|
-
|
|
554
|
-
To include source maps set the `sourceMap` option.
|
|
555
|
-
|
|
556
|
-
I.e. the `mini-css-extract-plugin` can handle them.
|
|
689
|
+
##### `localIdentRegExp`
|
|
557
690
|
|
|
558
|
-
|
|
691
|
+
Type: `String|RegExp`
|
|
692
|
+
Default: `undefined`
|
|
559
693
|
|
|
560
694
|
**webpack.config.js**
|
|
561
695
|
|
|
@@ -564,10 +698,12 @@ module.exports = {
|
|
|
564
698
|
module: {
|
|
565
699
|
rules: [
|
|
566
700
|
{
|
|
567
|
-
test: /\.css
|
|
701
|
+
test: /\.css$/i,
|
|
568
702
|
loader: 'css-loader',
|
|
569
703
|
options: {
|
|
570
|
-
|
|
704
|
+
modules: {
|
|
705
|
+
localIdentRegExp: /page-(.*)\.css/i,
|
|
706
|
+
},
|
|
571
707
|
},
|
|
572
708
|
},
|
|
573
709
|
],
|
|
@@ -575,33 +711,16 @@ module.exports = {
|
|
|
575
711
|
};
|
|
576
712
|
```
|
|
577
713
|
|
|
578
|
-
### `
|
|
714
|
+
### `sourceMap`
|
|
579
715
|
|
|
580
|
-
Type: `Boolean
|
|
716
|
+
Type: `Boolean`
|
|
581
717
|
Default: `false`
|
|
582
718
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
| Name | Type | Description |
|
|
586
|
-
| :----------------: | :---------: | :----------------------------------------------------------------------------------------------------------------------- |
|
|
587
|
-
| **`false`** | `{Boolean}` | Class names will be camelized, the original class name will not to be removed from the locals |
|
|
588
|
-
| **`true`** | `{Boolean}` | Class names will be camelized |
|
|
589
|
-
| **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
|
|
590
|
-
| **`'only'`** | `{String}` | Introduced in `0.27.1`. Class names will be camelized, the original class name will be removed from the locals |
|
|
591
|
-
| **`'dashesOnly'`** | `{String}` | Introduced in `0.27.1`. Dashes in class names will be camelized, the original class name will be removed from the locals |
|
|
719
|
+
Enables/Disables generation of source maps.
|
|
592
720
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
```css
|
|
596
|
-
.class-name {
|
|
597
|
-
}
|
|
598
|
-
```
|
|
721
|
+
To include source maps set the `sourceMap` option.
|
|
599
722
|
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
```js
|
|
603
|
-
import { className } from 'file.css';
|
|
604
|
-
```
|
|
723
|
+
They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS source maps do not).
|
|
605
724
|
|
|
606
725
|
**webpack.config.js**
|
|
607
726
|
|
|
@@ -610,10 +729,10 @@ module.exports = {
|
|
|
610
729
|
module: {
|
|
611
730
|
rules: [
|
|
612
731
|
{
|
|
613
|
-
test: /\.css
|
|
732
|
+
test: /\.css$/i,
|
|
614
733
|
loader: 'css-loader',
|
|
615
734
|
options: {
|
|
616
|
-
|
|
735
|
+
sourceMap: true,
|
|
617
736
|
},
|
|
618
737
|
},
|
|
619
738
|
],
|
|
@@ -626,6 +745,8 @@ module.exports = {
|
|
|
626
745
|
Type: `Number`
|
|
627
746
|
Default: `0`
|
|
628
747
|
|
|
748
|
+
Enables/Disables or setups number of loaders applied before CSS loader.
|
|
749
|
+
|
|
629
750
|
The option `importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources.
|
|
630
751
|
|
|
631
752
|
**webpack.config.js**
|
|
@@ -635,13 +756,16 @@ module.exports = {
|
|
|
635
756
|
module: {
|
|
636
757
|
rules: [
|
|
637
758
|
{
|
|
638
|
-
test: /\.css
|
|
759
|
+
test: /\.css$/i,
|
|
639
760
|
use: [
|
|
640
761
|
'style-loader',
|
|
641
762
|
{
|
|
642
763
|
loader: 'css-loader',
|
|
643
764
|
options: {
|
|
644
|
-
importLoaders: 2,
|
|
765
|
+
importLoaders: 2,
|
|
766
|
+
// 0 => no loaders (default);
|
|
767
|
+
// 1 => postcss-loader;
|
|
768
|
+
// 2 => postcss-loader, sass-loader
|
|
645
769
|
},
|
|
646
770
|
},
|
|
647
771
|
'postcss-loader',
|
|
@@ -655,12 +779,62 @@ module.exports = {
|
|
|
655
779
|
|
|
656
780
|
This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
|
|
657
781
|
|
|
658
|
-
### `
|
|
782
|
+
### `localsConvention`
|
|
783
|
+
|
|
784
|
+
Type: `String`
|
|
785
|
+
Default: `'asIs'`
|
|
786
|
+
|
|
787
|
+
Style of exported classnames.
|
|
788
|
+
|
|
789
|
+
By default, the exported JSON keys mirror the class names (i.e `asIs` value).
|
|
790
|
+
|
|
791
|
+
| Name | Type | Description |
|
|
792
|
+
| :-------------------: | :--------: | :----------------------------------------------------------------------------------------------- |
|
|
793
|
+
| **`'asIs'`** | `{String}` | Class names will be exported as is. |
|
|
794
|
+
| **`'camelCase'`** | `{String}` | Class names will be camelized, the original class name will not to be removed from the locals |
|
|
795
|
+
| **`'camelCaseOnly'`** | `{String}` | Class names will be camelized, the original class name will be removed from the locals |
|
|
796
|
+
| **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
|
|
797
|
+
| **`'dashesOnly'`** | `{String}` | Dashes in class names will be camelized, the original class name will be removed from the locals |
|
|
798
|
+
|
|
799
|
+
**file.css**
|
|
800
|
+
|
|
801
|
+
```css
|
|
802
|
+
.class-name {
|
|
803
|
+
}
|
|
804
|
+
```
|
|
805
|
+
|
|
806
|
+
**file.js**
|
|
807
|
+
|
|
808
|
+
```js
|
|
809
|
+
import { className } from 'file.css';
|
|
810
|
+
```
|
|
811
|
+
|
|
812
|
+
**webpack.config.js**
|
|
813
|
+
|
|
814
|
+
```js
|
|
815
|
+
module.exports = {
|
|
816
|
+
module: {
|
|
817
|
+
rules: [
|
|
818
|
+
{
|
|
819
|
+
test: /\.css$/i,
|
|
820
|
+
loader: 'css-loader',
|
|
821
|
+
options: {
|
|
822
|
+
localsConvention: 'camelCase',
|
|
823
|
+
},
|
|
824
|
+
},
|
|
825
|
+
],
|
|
826
|
+
},
|
|
827
|
+
};
|
|
828
|
+
```
|
|
829
|
+
|
|
830
|
+
### `onlyLocals`
|
|
659
831
|
|
|
660
832
|
Type: `Boolean`
|
|
661
833
|
Default: `false`
|
|
662
834
|
|
|
663
|
-
Export only locals
|
|
835
|
+
Export only locals.
|
|
836
|
+
|
|
837
|
+
**Useful** when you use **css modules** for pre-rendering (for example SSR).
|
|
664
838
|
For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
|
|
665
839
|
It doesn't embed CSS but only exports the identifier mappings.
|
|
666
840
|
|
|
@@ -671,10 +845,10 @@ module.exports = {
|
|
|
671
845
|
module: {
|
|
672
846
|
rules: [
|
|
673
847
|
{
|
|
674
|
-
test: /\.css
|
|
848
|
+
test: /\.css$/i,
|
|
675
849
|
loader: 'css-loader',
|
|
676
850
|
options: {
|
|
677
|
-
|
|
851
|
+
onlyLocals: true,
|
|
678
852
|
},
|
|
679
853
|
},
|
|
680
854
|
],
|
|
@@ -695,14 +869,14 @@ module.exports = {
|
|
|
695
869
|
module: {
|
|
696
870
|
rules: [
|
|
697
871
|
{
|
|
698
|
-
test: /\.css
|
|
872
|
+
test: /\.css$/i,
|
|
699
873
|
use: ['style-loader', 'css-loader'],
|
|
700
874
|
},
|
|
701
875
|
{
|
|
702
|
-
test: /\.(png|
|
|
876
|
+
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
|
|
703
877
|
loader: 'url-loader',
|
|
704
878
|
options: {
|
|
705
|
-
limit:
|
|
879
|
+
limit: 8192,
|
|
706
880
|
},
|
|
707
881
|
},
|
|
708
882
|
],
|
|
@@ -718,6 +892,47 @@ For production builds it's recommended to extract the CSS from your bundle being
|
|
|
718
892
|
|
|
719
893
|
- 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
|
|
720
894
|
|
|
895
|
+
### CSS modules and pure CSS
|
|
896
|
+
|
|
897
|
+
When you have pure CSS (without CSS modules) and CSS modules in project you can use this setup:
|
|
898
|
+
|
|
899
|
+
**webpack.config.js**
|
|
900
|
+
|
|
901
|
+
```js
|
|
902
|
+
module.exports = {
|
|
903
|
+
module: {
|
|
904
|
+
rules: [
|
|
905
|
+
{
|
|
906
|
+
// For pure CSS (without CSS modules)
|
|
907
|
+
test: /\.css$/i,
|
|
908
|
+
exclude: /\.module\.css$/i,
|
|
909
|
+
use: ['style-loader', 'css-loader'],
|
|
910
|
+
},
|
|
911
|
+
{
|
|
912
|
+
// For CSS modules
|
|
913
|
+
test: /\.module\.css$/i,
|
|
914
|
+
use: [
|
|
915
|
+
'style-loader',
|
|
916
|
+
{
|
|
917
|
+
loader: 'css-loader',
|
|
918
|
+
options: {
|
|
919
|
+
modules: true,
|
|
920
|
+
},
|
|
921
|
+
},
|
|
922
|
+
],
|
|
923
|
+
},
|
|
924
|
+
{
|
|
925
|
+
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
|
|
926
|
+
loader: 'url-loader',
|
|
927
|
+
options: {
|
|
928
|
+
limit: 8192,
|
|
929
|
+
},
|
|
930
|
+
},
|
|
931
|
+
],
|
|
932
|
+
},
|
|
933
|
+
};
|
|
934
|
+
```
|
|
935
|
+
|
|
721
936
|
## Contributing
|
|
722
937
|
|
|
723
938
|
Please take a moment to read our contributing guidelines if you haven't yet done so.
|
|
@@ -734,8 +949,8 @@ Please take a moment to read our contributing guidelines if you haven't yet done
|
|
|
734
949
|
[node-url]: https://nodejs.org
|
|
735
950
|
[deps]: https://david-dm.org/webpack-contrib/css-loader.svg
|
|
736
951
|
[deps-url]: https://david-dm.org/webpack-contrib/css-loader
|
|
737
|
-
[tests]: https://
|
|
738
|
-
[tests-url]: https://
|
|
952
|
+
[tests]: https://dev.azure.com/webpack-contrib/css-loader/_apis/build/status/webpack-contrib.css-loader?branchName=master
|
|
953
|
+
[tests-url]: https://dev.azure.com/webpack-contrib/css-loader/_build/latest?definitionId=2&branchName=master
|
|
739
954
|
[cover]: https://codecov.io/gh/webpack-contrib/css-loader/branch/master/graph/badge.svg
|
|
740
955
|
[cover-url]: https://codecov.io/gh/webpack-contrib/css-loader
|
|
741
956
|
[chat]: https://badges.gitter.im/webpack/webpack.svg
|