css-loader 2.0.1 → 3.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 CHANGED
@@ -19,10 +19,6 @@
19
19
 
20
20
  The `css-loader` interprets `@import` and `url()` like `import/require()` and will resolve them.
21
21
 
22
- ## Requirements
23
-
24
- This module requires a minimum of Node v6.9.0 and Webpack v4.0.0.
25
-
26
22
  ## Getting Started
27
23
 
28
24
  To begin, you'll need to install `css-loader`:
@@ -46,7 +42,7 @@ module.exports = {
46
42
  module: {
47
43
  rules: [
48
44
  {
49
- test: /\.css$/,
45
+ test: /\.css$/i,
50
46
  use: ['style-loader', 'css-loader'],
51
47
  },
52
48
  ],
@@ -69,7 +65,7 @@ module.exports = {
69
65
  module: {
70
66
  rules: [
71
67
  {
72
- test: /\.css$/,
68
+ test: /\.css$/i,
73
69
  use: ['to-string-loader', 'css-loader'],
74
70
  },
75
71
  ],
@@ -99,7 +95,7 @@ module.exports = {
99
95
  module: {
100
96
  rules: [
101
97
  {
102
- test: /\.css$/,
98
+ test: /\.css$/i,
103
99
  use: [
104
100
  'handlebars-loader', // handlebars loader expects raw resource string
105
101
  'extract-loader',
@@ -113,23 +109,22 @@ module.exports = {
113
109
 
114
110
  ## Options
115
111
 
116
- | Name | Type | Default | Description |
117
- | :-----------------------------------------: | :-------------------: | :-------------: | :------------------------------------------ |
118
- | **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enable/Disable `url()` handling |
119
- | **[`import`](#import)** | `{Boolean\/Function}` | `true` | Enable/Disable @import handling |
120
- | **[`modules`](#modules)** | `{Boolean\|String}` | `false` | Enable/Disable CSS Modules and setup mode |
121
- | **[`localIdentName`](#localidentname)** | `{String}` | `[hash:base64]` | Configure the generated ident |
122
- | **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
123
- | **[`camelCase`](#camelcase)** | `{Boolean\|String}` | `false` | Export Classnames in CamelCase |
124
- | **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
125
- | **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
112
+ | Name | Type | Default | Description |
113
+ | :-----------------------------------------: | :-------------------------: | :-----: | :------------------------------------------------- |
114
+ | **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enable/Disable `url()` handling |
115
+ | **[`import`](#import)** | `{Boolean\|Function}` | `true` | Enable/Disable @import handling |
116
+ | **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `false` | Enable/Disable CSS Modules and setup their options |
117
+ | **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
118
+ | **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
119
+ | **[`localsConvention`](#localsconvention)** | `{String}` | `asIs` | Setup style of exported classnames |
120
+ | **[`onlyLocals`](#onlylocals)** | `{Boolean}` | `false` | Export only locals |
126
121
 
127
122
  ### `url`
128
123
 
129
124
  Type: `Boolean|Function`
130
125
  Default: `true`
131
126
 
132
- Control `url()` resolving. Absolute urls are not resolving.
127
+ Control `url()` resolving. Absolute URLs and root-relative URLs are not resolving.
133
128
 
134
129
  Examples resolutions:
135
130
 
@@ -161,7 +156,7 @@ module.exports = {
161
156
  module: {
162
157
  rules: [
163
158
  {
164
- test: /\.css$/,
159
+ test: /\.css$/i,
165
160
  loader: 'css-loader',
166
161
  options: {
167
162
  url: true,
@@ -183,14 +178,18 @@ module.exports = {
183
178
  module: {
184
179
  rules: [
185
180
  {
186
- test: /\.css$/,
181
+ test: /\.css$/i,
187
182
  loader: 'css-loader',
188
183
  options: {
189
184
  url: (url, resourcePath) => {
190
185
  // resourcePath - path to css file
191
186
 
192
- // `url()` with `img.png` stay untouched
193
- return url.includes('img.png');
187
+ // Don't handle `img.png` urls
188
+ if (url.includes('img.png')) {
189
+ return false;
190
+ }
191
+
192
+ return true;
194
193
  },
195
194
  },
196
195
  },
@@ -237,7 +236,7 @@ module.exports = {
237
236
  module: {
238
237
  rules: [
239
238
  {
240
- test: /\.css$/,
239
+ test: /\.css$/i,
241
240
  loader: 'css-loader',
242
241
  options: {
243
242
  import: true,
@@ -259,7 +258,7 @@ module.exports = {
259
258
  module: {
260
259
  rules: [
261
260
  {
262
- test: /\.css$/,
261
+ test: /\.css$/i,
263
262
  loader: 'css-loader',
264
263
  options: {
265
264
  import: (parsedImport, resourcePath) => {
@@ -267,8 +266,12 @@ module.exports = {
267
266
  // parsedImport.media - media query of `@import`
268
267
  // resourcePath - path to css file
269
268
 
270
- // `@import` with `style.css` stay untouched
271
- return parsedImport.url.includes('style.css');
269
+ // Don't handle `style.css` import
270
+ if (parsedImport.url.includes('style.css')) {
271
+ return false;
272
+ }
273
+
274
+ return true;
272
275
  },
273
276
  },
274
277
  },
@@ -277,24 +280,15 @@ module.exports = {
277
280
  };
278
281
  ```
279
282
 
280
- ### [`modules`](https://github.com/css-modules/css-modules)
283
+ ### `modules`
281
284
 
282
- Type: `Boolean|String`
285
+ Type: `Boolean|String|Object`
283
286
  Default: `false`
284
287
 
285
- The `modules` option enables/disables the **CSS Modules** spec and setup basic behaviour.
286
-
287
- | Name | Type | Description |
288
- | :------------: | :---------: | :------------------------------------------------------------------------------------------------------------------------------- |
289
- | **`true`** | `{Boolean}` | Enables local scoped CSS by default (use **local** mode by default) |
290
- | **`false`** | `{Boolean}` | Disable the **CSS Modules** spec, all **CSS Modules** features (like `@value`, `:local`, `:global` and `composes`) will not work |
291
- | **`'local'`** | `{String}` | Enables local scoped CSS by default (same as `true` value) |
292
- | **`'global'`** | `{String}` | Enables global scoped CSS by default |
288
+ The `modules` option enables/disables the **[CSS Modules](https://github.com/css-modules/css-modules)** specification and setup basic behaviour.
293
289
 
294
290
  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.
295
291
 
296
- You can read about **modes** below.
297
-
298
292
  **webpack.config.js**
299
293
 
300
294
  ```js
@@ -302,7 +296,7 @@ module.exports = {
302
296
  module: {
303
297
  rules: [
304
298
  {
305
- test: /\.css$/,
299
+ test: /\.css$/i,
306
300
  loader: 'css-loader',
307
301
  options: {
308
302
  modules: true,
@@ -313,6 +307,8 @@ module.exports = {
313
307
  };
314
308
  ```
315
309
 
310
+ #### `Features`
311
+
316
312
  ##### `Scope`
317
313
 
318
314
  Using `local` value requires you to specify `:global` classes.
@@ -325,7 +321,7 @@ Styles can be locally scoped to avoid globally scoping styles.
325
321
  The syntax `:local(.className)` can be used to declare `className` in the local scope. The local identifiers are exported by the module.
326
322
 
327
323
  With `:local` (without brackets) local mode can be switched on for this selector.
328
- The `:global(.className)` nocation can be used to declare an explicit global selector.
324
+ The `:global(.className)` notation can be used to declare an explicit global selector.
329
325
  With `:global` (without brackets) global mode can be switched on for this selector.
330
326
 
331
327
  The loader replaces local selectors with unique identifiers. The chosen unique identifiers are exported by the module.
@@ -438,13 +434,36 @@ To import from multiple modules use multiple `composes:` rules.
438
434
  }
439
435
  ```
440
436
 
441
- ### `localIdentName`
437
+ ##### `Values`
442
438
 
443
- Type: `String`
444
- Default: `[hash:base64]`
439
+ You can use `@value` to specific values to be reused throughout a document.
445
440
 
446
- You can configure the generated ident with the `localIdentName` query parameter.
447
- See [loader-utils's documentation](https://github.com/webpack/loader-utils#interpolatename) for more information on options.
441
+ We recommend use prefix `v-` for values, `s-` for selectors and `m-` for media at-rules.
442
+
443
+ ```css
444
+ @value v-primary: #BF4040;
445
+ @value s-black: black-selector;
446
+ @value m-large: (min-width: 960px);
447
+
448
+ .header {
449
+ color: v-primary;
450
+ padding: 0 10px;
451
+ }
452
+
453
+ .s-black {
454
+ color: black;
455
+ }
456
+
457
+ @media m-large {
458
+ .header {
459
+ padding: 0 20px;
460
+ }
461
+ }
462
+ ```
463
+
464
+ #### `Boolean`
465
+
466
+ Enable **CSS Modules** features.
448
467
 
449
468
  **webpack.config.js**
450
469
 
@@ -453,11 +472,10 @@ module.exports = {
453
472
  module: {
454
473
  rules: [
455
474
  {
456
- test: /\.css$/,
475
+ test: /\.css$/i,
457
476
  loader: 'css-loader',
458
477
  options: {
459
478
  modules: true,
460
- localIdentName: '[path][name]__[local]--[hash:base64:5]',
461
479
  },
462
480
  },
463
481
  ],
@@ -465,7 +483,9 @@ module.exports = {
465
483
  };
466
484
  ```
467
485
 
468
- You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema.
486
+ #### `String`
487
+
488
+ Enable **CSS Modules** features and setup `mode`.
469
489
 
470
490
  **webpack.config.js**
471
491
 
@@ -474,15 +494,37 @@ module.exports = {
474
494
  module: {
475
495
  rules: [
476
496
  {
477
- test: /\.css$/,
497
+ test: /\.css$/i,
478
498
  loader: 'css-loader',
479
499
  options: {
480
- modules: true,
481
- context: path.resolve(__dirname, 'context'), // Allow to redefine basic loader context for `local-ident-name`
482
- hashPrefix: 'hash', // Allow to add custom hash to generate more unique classes
483
- localIdentName: '[path][name]__[local]--[hash:base64:5]',
484
- getLocalIdent: (context, localIdentName, localName, options) => {
485
- return 'whatever_random_class_name';
500
+ // Using `local` value has same effect like using `modules: true`
501
+ modules: 'global',
502
+ },
503
+ },
504
+ ],
505
+ },
506
+ };
507
+ ```
508
+
509
+ #### `Object`
510
+
511
+ Enable **CSS Modules** features and setup options for them.
512
+
513
+ **webpack.config.js**
514
+
515
+ ```js
516
+ module.exports = {
517
+ module: {
518
+ rules: [
519
+ {
520
+ test: /\.css$/i,
521
+ loader: 'css-loader',
522
+ options: {
523
+ modules: {
524
+ mode: 'local',
525
+ localIdentName: '[path][name]__[local]--[hash:base64:5]',
526
+ context: path.resolve(__dirname, 'src'),
527
+ hashPrefix: 'my-custom-hash',
486
528
  },
487
529
  },
488
530
  },
@@ -491,16 +533,40 @@ module.exports = {
491
533
  };
492
534
  ```
493
535
 
494
- ### `sourceMap`
536
+ ##### `mode`
495
537
 
496
- Type: `Boolean`
497
- Default: `true`
538
+ Type: `String`
539
+ Default: `local`
498
540
 
499
- To include source maps set the `sourceMap` option.
541
+ Setup `mode` option. You can omit the value when you want `local` mode.
500
542
 
501
- I.e. the `mini-css-extract-plugin` can handle them.
543
+ **webpack.config.js**
544
+
545
+ ```js
546
+ module.exports = {
547
+ module: {
548
+ rules: [
549
+ {
550
+ test: /\.css$/i,
551
+ loader: 'css-loader',
552
+ options: {
553
+ modules: {
554
+ mode: 'global',
555
+ },
556
+ },
557
+ },
558
+ ],
559
+ },
560
+ };
561
+ ```
562
+
563
+ ##### `localIdentName`
502
564
 
503
- They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS source maps do not). In addition to that relative paths are buggy and you need to use an absolute public path which includes the server URL.
565
+ Type: `String`
566
+ Default: `[hash:base64]`
567
+
568
+ You can configure the generated ident with the `localIdentName` query parameter.
569
+ See [loader-utils's documentation](https://github.com/webpack/loader-utils#interpolatename) for more information on options.
504
570
 
505
571
  **webpack.config.js**
506
572
 
@@ -509,10 +575,12 @@ module.exports = {
509
575
  module: {
510
576
  rules: [
511
577
  {
512
- test: /\.css$/,
578
+ test: /\.css$/i,
513
579
  loader: 'css-loader',
514
580
  options: {
515
- sourceMap: true,
581
+ modules: {
582
+ localIdentName: '[path][name]__[local]--[hash:base64:5]',
583
+ },
516
584
  },
517
585
  },
518
586
  ],
@@ -520,34 +588,96 @@ module.exports = {
520
588
  };
521
589
  ```
522
590
 
523
- ### `camelCase`
591
+ ##### `context`
524
592
 
525
- Type: `Boolean|String`
526
- Default: `false`
593
+ Type: `String`
594
+ Default: `undefined`
527
595
 
528
- By default, the exported JSON keys mirror the class names. If you want to camelize class names (useful in JS), pass the query parameter `camelCase` to css-loader.
596
+ Allow to redefine basic loader context for local ident name.
597
+ By default we use `rootContext` of loader.
529
598
 
530
- | Name | Type | Description |
531
- | :----------------: | :---------: | :----------------------------------------------------------------------------------------------------------------------- |
532
- | **`false`** | `{Boolean}` | Class names will be camelized, the original class name will not to be removed from the locals |
533
- | **`true`** | `{Boolean}` | Class names will be camelized |
534
- | **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
535
- | **`'only'`** | `{String}` | Introduced in `0.27.1`. Class names will be camelized, the original class name will be removed from the locals |
536
- | **`'dashesOnly'`** | `{String}` | Introduced in `0.27.1`. Dashes in class names will be camelized, the original class name will be removed from the locals |
599
+ **webpack.config.js**
537
600
 
538
- **file.css**
601
+ ```js
602
+ module.exports = {
603
+ module: {
604
+ rules: [
605
+ {
606
+ test: /\.css$/i,
607
+ loader: 'css-loader',
608
+ options: {
609
+ modules: {
610
+ context: path.resolve(__dirname, 'context'),
611
+ },
612
+ },
613
+ },
614
+ ],
615
+ },
616
+ };
617
+ ```
539
618
 
540
- ```css
541
- .class-name {
542
- }
619
+ ##### `hashPrefix`
620
+
621
+ Type: `String`
622
+ Default: `undefined`
623
+
624
+ Allow to add custom hash to generate more unique classes.
625
+
626
+ **webpack.config.js**
627
+
628
+ ```js
629
+ module.exports = {
630
+ module: {
631
+ rules: [
632
+ {
633
+ test: /\.css$/i,
634
+ loader: 'css-loader',
635
+ options: {
636
+ modules: {
637
+ hashPrefix: 'hash',
638
+ },
639
+ },
640
+ },
641
+ ],
642
+ },
643
+ };
543
644
  ```
544
645
 
545
- **file.js**
646
+ ##### `getLocalIdent`
647
+
648
+ Type: `Function`
649
+ Default: `undefined`
650
+
651
+ You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema.
652
+ By default we use built-in function to generate a classname.
653
+
654
+ **webpack.config.js**
546
655
 
547
656
  ```js
548
- import { className } from 'file.css';
657
+ module.exports = {
658
+ module: {
659
+ rules: [
660
+ {
661
+ test: /\.css$/i,
662
+ loader: 'css-loader',
663
+ options: {
664
+ modules: {
665
+ getLocalIdent: (context, localIdentName, localName, options) => {
666
+ return 'whatever_random_class_name';
667
+ },
668
+ },
669
+ },
670
+ },
671
+ ],
672
+ },
673
+ };
549
674
  ```
550
675
 
676
+ ##### `localIdentRegExp`
677
+
678
+ Type: `String|RegExp`
679
+ Default: `undefined`
680
+
551
681
  **webpack.config.js**
552
682
 
553
683
  ```js
@@ -555,10 +685,43 @@ module.exports = {
555
685
  module: {
556
686
  rules: [
557
687
  {
558
- test: /\.css$/,
688
+ test: /\.css$/i,
559
689
  loader: 'css-loader',
560
690
  options: {
561
- camelCase: true,
691
+ modules: {
692
+ localIdentRegExp: /page-(.*)\.css/i,
693
+ },
694
+ },
695
+ },
696
+ ],
697
+ },
698
+ };
699
+ ```
700
+
701
+ ### `sourceMap`
702
+
703
+ Type: `Boolean`
704
+ Default: `false`
705
+
706
+ To include source maps set the `sourceMap` option.
707
+
708
+ I.e. the `mini-css-extract-plugin` can handle them.
709
+
710
+ They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS source maps do not).
711
+
712
+ In addition to that relative paths are buggy and you need to use an absolute public path which includes the server URL.
713
+
714
+ **webpack.config.js**
715
+
716
+ ```js
717
+ module.exports = {
718
+ module: {
719
+ rules: [
720
+ {
721
+ test: /\.css$/i,
722
+ loader: 'css-loader',
723
+ options: {
724
+ sourceMap: true,
562
725
  },
563
726
  },
564
727
  ],
@@ -580,7 +743,7 @@ module.exports = {
580
743
  module: {
581
744
  rules: [
582
745
  {
583
- test: /\.css$/,
746
+ test: /\.css$/i,
584
747
  use: [
585
748
  'style-loader',
586
749
  {
@@ -600,7 +763,53 @@ module.exports = {
600
763
 
601
764
  This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
602
765
 
603
- ### `exportOnlyLocals`
766
+ ### `localsConvention`
767
+
768
+ Type: `String`
769
+ Default: `undefined`
770
+
771
+ By default, the exported JSON keys mirror the class names (i.e `asIs` value).
772
+
773
+ | Name | Type | Description |
774
+ | :-------------------: | :--------: | :----------------------------------------------------------------------------------------------- |
775
+ | **`'asIs'`** | `{String}` | Class names will be exported as is. |
776
+ | **`'camelCase'`** | `{String}` | Class names will be camelized, the original class name will not to be removed from the locals |
777
+ | **`'camelCaseOnly'`** | `{String}` | Class names will be camelized, the original class name will be removed from the locals |
778
+ | **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
779
+ | **`'dashesOnly'`** | `{String}` | Dashes in class names will be camelized, the original class name will be removed from the locals |
780
+
781
+ **file.css**
782
+
783
+ ```css
784
+ .class-name {
785
+ }
786
+ ```
787
+
788
+ **file.js**
789
+
790
+ ```js
791
+ import { className } from 'file.css';
792
+ ```
793
+
794
+ **webpack.config.js**
795
+
796
+ ```js
797
+ module.exports = {
798
+ module: {
799
+ rules: [
800
+ {
801
+ test: /\.css$/i,
802
+ loader: 'css-loader',
803
+ options: {
804
+ localsConvention: 'camelCase',
805
+ },
806
+ },
807
+ ],
808
+ },
809
+ };
810
+ ```
811
+
812
+ ### `onlyLocals`
604
813
 
605
814
  Type: `Boolean`
606
815
  Default: `false`
@@ -616,10 +825,10 @@ module.exports = {
616
825
  module: {
617
826
  rules: [
618
827
  {
619
- test: /\.css$/,
828
+ test: /\.css$/i,
620
829
  loader: 'css-loader',
621
830
  options: {
622
- exportOnlyLocals: true,
831
+ onlyLocals: true,
623
832
  },
624
833
  },
625
834
  ],
@@ -640,14 +849,14 @@ module.exports = {
640
849
  module: {
641
850
  rules: [
642
851
  {
643
- test: /\.css$/,
852
+ test: /\.css$/i,
644
853
  use: ['style-loader', 'css-loader'],
645
854
  },
646
855
  {
647
- test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
856
+ test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/,
648
857
  loader: 'url-loader',
649
858
  options: {
650
- limit: 10000,
859
+ limit: 8192,
651
860
  },
652
861
  },
653
862
  ],
@@ -658,7 +867,10 @@ module.exports = {
658
867
  ### Extract
659
868
 
660
869
  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.
661
- 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.
870
+
871
+ - 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.
872
+
873
+ - 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
662
874
 
663
875
  ## Contributing
664
876
 
@@ -672,21 +884,15 @@ Please take a moment to read our contributing guidelines if you haven't yet done
672
884
 
673
885
  [npm]: https://img.shields.io/npm/v/css-loader.svg
674
886
  [npm-url]: https://npmjs.com/package/css-loader
675
-
676
887
  [node]: https://img.shields.io/node/v/css-loader.svg
677
888
  [node-url]: https://nodejs.org
678
-
679
889
  [deps]: https://david-dm.org/webpack-contrib/css-loader.svg
680
890
  [deps-url]: https://david-dm.org/webpack-contrib/css-loader
681
-
682
- [tests]: https://img.shields.io/circleci/project/github/webpack-contrib/css-loader.svg
683
- [tests-url]: https://circleci.com/gh/webpack-contrib/css-loader
684
-
891
+ [tests]: https://dev.azure.com/webpack-contrib/css-loader/_apis/build/status/webpack-contrib.css-loader?branchName=master
892
+ [tests-url]: https://dev.azure.com/webpack-contrib/css-loader/_build/latest?definitionId=2&branchName=master
685
893
  [cover]: https://codecov.io/gh/webpack-contrib/css-loader/branch/master/graph/badge.svg
686
894
  [cover-url]: https://codecov.io/gh/webpack-contrib/css-loader
687
-
688
895
  [chat]: https://badges.gitter.im/webpack/webpack.svg
689
896
  [chat-url]: https://gitter.im/webpack/webpack
690
-
691
897
  [size]: https://packagephobia.now.sh/badge?p=css-loader
692
898
  [size-url]: https://packagephobia.now.sh/result?p=css-loader