UNPKG

17.8 kBMarkdownView Raw
1<div align="center">
2 <a href="https://github.com/webpack/webpack">
3 <img width="200" height="200"
4 src="https://webpack.js.org/assets/icon-square-big.svg">
5 </a>
6 <h1>Style Loader</h1>
7</div>
8
9[![npm][npm]][npm-url]
10[![node][node]][node-url]
11[![deps][deps]][deps-url]
12[![tests][tests]][tests-url]
13[![coverage][cover]][cover-url]
14[![chat][chat]][chat-url]
15[![size][size]][size-url]
16
17# style-loader
18
19Inject CSS into the DOM.
20
21## Getting Started
22
23To begin, you'll need to install `style-loader`:
24
25```console
26npm install --save-dev style-loader
27```
28
29It's recommended to combine `style-loader` with the [`css-loader`](https://github.com/webpack-contrib/css-loader)
30
31Then add the loader to your `webpack` config. For example:
32
33**style.css**
34
35```css
36body {
37 background: green;
38}
39```
40
41**component.js**
42
43```js
44import './style.css';
45```
46
47**webpack.config.js**
48
49```js
50module.exports = {
51 module: {
52 rules: [
53 {
54 test: /\.css$/i,
55 use: ['style-loader', 'css-loader'],
56 },
57 ],
58 },
59};
60```
61
62## Options
63
64| Name | Type | Default | Description |
65| :-----------------------------: | :------------------: | :--------: | :------------------------------------------------------- |
66| [**`injectType`**](#injecttype) | `{String}` | `styleTag` | Allows to setup how styles will be injected into the DOM |
67| [**`attributes`**](#attributes) | `{Object}` | `{}` | Adds custom attributes to tag |
68| [**`insert`**](#insert) | `{String\|Function}` | `head` | Inserts tag at the given position into the DOM |
69| [**`base`**](#base) | `{Number}` | `true` | Sets module ID base (DLLPlugin) |
70| [**`esModule`**](#esmodule) | `{Boolean}` | `false` | Use ES modules syntax |
71
72### `injectType`
73
74Type: `String`
75Default: `styleTag`
76
77Allows to setup how styles will be injected into the DOM.
78
79Possible values:
80
81- `styleTag`
82- `singletonStyleTag`
83- `lazyStyleTag`
84- `lazySingletonStyleTag`
85- `linkTag`
86
87#### `styleTag`
88
89Automatically injects styles into the DOM using multiple `<style></style>`. It is **default** behaviour.
90
91**component.js**
92
93```js
94import './styles.css';
95```
96
97Example with Locals (CSS Modules):
98
99**component-with-css-modules.js**
100
101```js
102import styles from './styles.css';
103
104const divElement = document.createElement('div');
105divElement.className = styles['my-class'];
106```
107
108All locals (class names) stored in imported object.
109
110**webpack.config.js**
111
112```js
113module.exports = {
114 module: {
115 rules: [
116 {
117 test: /\.css$/i,
118 use: [
119 // The `injectType` option can be avoided because it is default behaviour
120 { loader: 'style-loader', options: { injectType: 'styleTag' } },
121 'css-loader',
122 ],
123 },
124 ],
125 },
126};
127```
128
129The loader inject styles like:
130
131```html
132<style>
133 .foo {
134 color: red;
135 }
136</style>
137<style>
138 .bar {
139 color: blue;
140 }
141</style>
142```
143
144#### `singletonStyleTag`
145
146Automatically injects styles into the DOM using one `<style></style>`.
147
148> ⚠ Source maps do not work.
149
150**component.js**
151
152```js
153import './styles.css';
154```
155
156**component-with-css-modules.js**
157
158```js
159import styles from './styles.css';
160
161const divElement = document.createElement('div');
162divElement.className = styles['my-class'];
163```
164
165All locals (class names) stored in imported object.
166
167**webpack.config.js**
168
169```js
170module.exports = {
171 module: {
172 rules: [
173 {
174 test: /\.css$/i,
175 use: [
176 {
177 loader: 'style-loader',
178 options: { injectType: 'singletonStyleTag' },
179 },
180 'css-loader',
181 ],
182 },
183 ],
184 },
185};
186```
187
188The loader inject styles like:
189
190```html
191<style>
192 .foo {
193 color: red;
194 }
195 .bar {
196 color: blue;
197 }
198</style>
199```
200
201#### `lazyStyleTag`
202
203Injects styles into the DOM using multiple `<style></style>` on demand.
204We recommend following `.lazy.css` naming convention for lazy styles and the `.css` for basic `style-loader` usage (similar to other file types, i.e. `.lazy.less` and `.less`).
205When you `lazyStyleTag` value the `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`.
206
207> ⚠️ Behavior is undefined when `unuse` is called more often than `use`. Don't do that.
208
209**component.js**
210
211```js
212import styles from './styles.lazy.css';
213
214styles.use();
215// For removing styles you can use
216// styles.unuse();
217```
218
219**component-with-css-modules.js**
220
221```js
222import styles from './styles.lazy.css';
223
224styles.use();
225
226const divElement = document.createElement('div');
227divElement.className = styles.locals['my-class'];
228```
229
230All locals (class names) stored in `locals` property of imported object.
231
232**webpack.config.js**
233
234```js
235module.exports = {
236 module: {
237 rules: [
238 {
239 test: /\.css$/i,
240 exclude: /\.lazy\.css$/i,
241 use: ['style-loader', 'css-loader'],
242 },
243 {
244 test: /\.lazy\.css$/i,
245 use: [
246 { loader: 'style-loader', options: { injectType: 'lazyStyleTag' } },
247 'css-loader',
248 ],
249 },
250 ],
251 },
252};
253```
254
255The loader inject styles like:
256
257```html
258<style>
259 .foo {
260 color: red;
261 }
262</style>
263<style>
264 .bar {
265 color: blue;
266 }
267</style>
268```
269
270#### `lazySingletonStyleTag`
271
272Injects styles into the DOM using one `<style></style>` on demand.
273We recommend following `.lazy.css` naming convention for lazy styles and the `.css` for basic `style-loader` usage (similar to other file types, i.e. `.lazy.less` and `.less`).
274When you `lazySingletonStyleTag` value the `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`.
275
276> ⚠️ Source maps do not work.
277
278> ⚠️ Behavior is undefined when `unuse` is called more often than `use`. Don't do that.
279
280**component.js**
281
282```js
283import styles from './styles.css';
284
285styles.use();
286// For removing styles you can use
287// styles.unuse();
288```
289
290**component-with-css-modules.js**
291
292```js
293import styles from './styles.lazy.css';
294
295styles.use();
296
297const divElement = document.createElement('div');
298divElement.className = styles.locals['my-class'];
299```
300
301All locals (class names) stored in `locals` property of imported object.
302
303**webpack.config.js**
304
305```js
306module.exports = {
307 module: {
308 rules: [
309 {
310 test: /\.css$/i,
311 exclude: /\.lazy\.css$/i,
312 use: ['style-loader', 'css-loader'],
313 },
314 {
315 test: /\.lazy\.css$/i,
316 use: [
317 {
318 loader: 'style-loader',
319 options: { injectType: 'lazySingletonStyleTag' },
320 },
321 'css-loader',
322 ],
323 },
324 ],
325 },
326};
327```
328
329The loader generate this:
330
331```html
332<style>
333 .foo {
334 color: red;
335 }
336 .bar {
337 color: blue;
338 }
339</style>
340```
341
342#### `linkTag`
343
344Injects styles into the DOM using multiple `<link rel="stylesheet" href="path/to/file.css">` .
345
346> ℹ️ The loader will dynamically insert the `<link href="path/to/file.css" rel="stylesheet">` tag at runtime via JavaScript. You should use [MiniCssExtractPlugin](https://webpack.js.org/plugins/mini-css-extract-plugin/) if you want to include a static `<link href="path/to/file.css" rel="stylesheet">`.
347
348```js
349import './styles.css';
350import './other-styles.css';
351```
352
353**webpack.config.js**
354
355```js
356module.exports = {
357 module: {
358 rules: [
359 {
360 test: /\.link\.css$/i,
361 use: [
362 { loader: 'style-loader', options: { injectType: 'linkTag' } },
363 { loader: 'file-loader' },
364 ],
365 },
366 ],
367 },
368};
369```
370
371The loader generate this:
372
373```html
374<link rel="stylesheet" href="path/to/style.css" />
375<link rel="stylesheet" href="path/to/other-styles.css" />
376```
377
378### `attributes`
379
380Type: `Object`
381Default: `{}`
382
383If defined, the `style-loader` will attach given attributes with their values on `<style>` / `<link>` element.
384
385**component.js**
386
387```js
388import style from './file.css';
389```
390
391**webpack.config.js**
392
393```js
394module.exports = {
395 module: {
396 rules: [
397 {
398 test: /\.css$/i,
399 use: [
400 { loader: 'style-loader', options: { attributes: { id: 'id' } } },
401 { loader: 'css-loader' },
402 ],
403 },
404 ],
405 },
406};
407```
408
409```html
410<style id="id"></style>
411```
412
413### `insert`
414
415Type: `String|Function`
416Default: `head`
417
418By default, the `style-loader` appends `<style>`/`<link>` elements to the end of the style target, which is the `<head>` tag of the page unless specified by `insert`.
419This will cause CSS created by the loader to take priority over CSS already present in the target.
420You can use other values if the standard behavior is not suitable for you, but we do not recommend doing this.
421If you target an [iframe](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement) make sure you have sufficient access rights, the styles will be injected into the content document head.
422
423#### `String`
424
425Allows to setup custom [query selector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) where styles inject into the DOM.
426
427**webpack.config.js**
428
429```js
430module.exports = {
431 module: {
432 rules: [
433 {
434 test: /\.css$/i,
435 use: [
436 {
437 loader: 'style-loader',
438 options: {
439 insert: 'body',
440 },
441 },
442 'css-loader',
443 ],
444 },
445 ],
446 },
447};
448```
449
450A new `<style>`/`<link>` elements will be inserted into at bottom of `body` tag.
451
452#### `Function`
453
454Allows to override default behavior and insert styles at any position.
455
456> ⚠ Do not forget that this code will be used in the browser and not all browsers support latest ECMA features like `let`, `const`, `arrow function expression` and etc, we recommend use only ECMA 5 features, but it is depends what browsers you want to support
457> ⚠ Do not forget that some DOM methods may not be available in older browsers, we recommended use only [DOM core level 2 properties](https://caniuse.com/#search=DOM%20Core), but it is depends what browsers you want to support
458
459**webpack.config.js**
460
461```js
462module.exports = {
463 module: {
464 rules: [
465 {
466 test: /\.css$/i,
467 use: [
468 {
469 loader: 'style-loader',
470 options: {
471 insert: function insertAtTop(element) {
472 var parent = document.querySelector('head');
473 // eslint-disable-next-line no-underscore-dangle
474 var lastInsertedElement =
475 window._lastElementInsertedByStyleLoader;
476
477 if (!lastInsertedElement) {
478 parent.insertBefore(element, parent.firstChild);
479 } else if (lastInsertedElement.nextSibling) {
480 parent.insertBefore(element, lastInsertedElement.nextSibling);
481 } else {
482 parent.appendChild(element);
483 }
484
485 // eslint-disable-next-line no-underscore-dangle
486 window._lastElementInsertedByStyleLoader = element;
487 },
488 },
489 },
490 'css-loader',
491 ],
492 },
493 ],
494 },
495};
496```
497
498Insert styles at top of `head` tag.
499
500### `base`
501
502This setting is primarily used as a workaround for [css clashes](https://github.com/webpack-contrib/style-loader/issues/163) when using one or more [DllPlugin](https://robertknight.github.io/posts/webpack-dll-plugins/)'s. `base` allows you to prevent either the _app_'s css (or _DllPlugin2_'s css) from overwriting _DllPlugin1_'s css by specifying a css module id base which is greater than the range used by _DllPlugin1_ e.g.:
503
504**webpack.dll1.config.js**
505
506```js
507module.exports = {
508 module: {
509 rules: [
510 {
511 test: /\.css$/i,
512 use: ['style-loader', 'css-loader'],
513 },
514 ],
515 },
516};
517```
518
519**webpack.dll2.config.js**
520
521```js
522module.exports = {
523 module: {
524 rules: [
525 {
526 test: /\.css$/i,
527 use: [
528 { loader: 'style-loader', options: { base: 1000 } },
529 'css-loader',
530 ],
531 },
532 ],
533 },
534};
535```
536
537**webpack.app.config.js**
538
539```js
540module.exports = {
541 module: {
542 rules: [
543 {
544 test: /\.css$/i,
545 use: [
546 { loader: 'style-loader', options: { base: 2000 } },
547 'css-loader',
548 ],
549 },
550 ],
551 },
552};
553```
554
555### `esModule`
556
557Type: `Boolean`
558Default: `false`
559
560By default, `style-loader` generates JS modules that use the CommonJS modules syntax.
561There are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
562
563You can enable a ES module syntax using:
564
565**webpack.config.js**
566
567```js
568module.exports = {
569 module: {
570 rules: [
571 {
572 test: /\.css$/i,
573 loader: 'css-loader',
574 options: {
575 esModule: true,
576 },
577 },
578 ],
579 },
580};
581```
582
583## Examples
584
585### Source maps
586
587The loader automatically inject source maps when previous loader emit them.
588Therefore, to generate source maps, set the `sourceMap` option to `true` for the previous loader.
589
590**webpack.config.js**
591
592```js
593module.exports = {
594 module: {
595 rules: [
596 {
597 test: /\.css$/i,
598 use: [
599 'style-loader',
600 { loader: 'css-loader', options: { sourceMap: true } },
601 ],
602 },
603 ],
604 },
605};
606```
607
608### Nonce
609
610There are two ways to work with `nonce`:
611
612- using the `attributes` option
613- using the `__webpack_nonce__` variable
614
615> ⚠ the `attributes` option takes precedence over the `__webpack_nonce__` variable
616
617#### `attributes`
618
619**component.js**
620
621```js
622import './style.css';
623```
624
625**webpack.config.js**
626
627```js
628module.exports = {
629 module: {
630 rules: [
631 {
632 test: /\.css$/i,
633 use: [
634 {
635 loader: 'style-loader',
636 options: {
637 attributes: {
638 nonce: '12345678',
639 },
640 },
641 },
642 'css-loader',
643 ],
644 },
645 ],
646 },
647};
648```
649
650The loader generate:
651
652```html
653<style nonce="12345678">
654 .foo {
655 color: red;
656 }
657</style>
658```
659
660#### `__webpack_nonce__`
661
662**create-nonce.js**
663
664```js
665__webpack_nonce__ = '12345678';
666```
667
668**component.js**
669
670```js
671import './create-nonce.js';
672import './style.css';
673```
674
675Alternative example for `require`:
676
677**component.js**
678
679```js
680__webpack_nonce__ = '12345678';
681
682require('./style.css');
683```
684
685**webpack.config.js**
686
687```js
688module.exports = {
689 module: {
690 rules: [
691 {
692 test: /\.css$/i,
693 use: ['style-loader', 'css-loader'],
694 },
695 ],
696 },
697};
698```
699
700The loader generate:
701
702```html
703<style nonce="12345678">
704 .foo {
705 color: red;
706 }
707</style>
708```
709
710#### Insert styles at top
711
712Inserts styles at top of `head` tag.
713
714**webpack.config.js**
715
716```js
717module.exports = {
718 module: {
719 rules: [
720 {
721 test: /\.css$/i,
722 use: [
723 {
724 loader: 'style-loader',
725 options: {
726 insert: function insertAtTop(element) {
727 var parent = document.querySelector('head');
728 var lastInsertedElement =
729 window._lastElementInsertedByStyleLoader;
730
731 if (!lastInsertedElement) {
732 parent.insertBefore(element, parent.firstChild);
733 } else if (lastInsertedElement.nextSibling) {
734 parent.insertBefore(element, lastInsertedElement.nextSibling);
735 } else {
736 parent.appendChild(element);
737 }
738
739 window._lastElementInsertedByStyleLoader = element;
740 },
741 },
742 },
743 'css-loader',
744 ],
745 },
746 ],
747 },
748};
749```
750
751#### Insert styles before target element
752
753Inserts styles before `#id` element.
754
755**webpack.config.js**
756
757```js
758module.exports = {
759 module: {
760 rules: [
761 {
762 test: /\.css$/i,
763 use: [
764 {
765 loader: 'style-loader',
766 options: {
767 insert: function insertBeforeAt(element) {
768 const parent = document.querySelector('head');
769 const target = document.querySelector('#id');
770
771 const lastInsertedElement =
772 window._lastElementInsertedByStyleLoader;
773
774 if (!lastInsertedElement) {
775 parent.insertBefore(element, target);
776 } else if (lastInsertedElement.nextSibling) {
777 parent.insertBefore(element, lastInsertedElement.nextSibling);
778 } else {
779 parent.appendChild(element);
780 }
781
782 window._lastElementInsertedByStyleLoader = element;
783 },
784 },
785 },
786 'css-loader',
787 ],
788 },
789 ],
790 },
791};
792```
793
794## Contributing
795
796Please take a moment to read our contributing guidelines if you haven't yet done so.
797
798[CONTRIBUTING](./.github/CONTRIBUTING.md)
799
800## License
801
802[MIT](./LICENSE)
803
804[npm]: https://img.shields.io/npm/v/style-loader.svg
805[npm-url]: https://npmjs.com/package/style-loader
806[node]: https://img.shields.io/node/v/style-loader.svg
807[node-url]: https://nodejs.org
808[deps]: https://david-dm.org/webpack-contrib/style-loader.svg
809[deps-url]: https://david-dm.org/webpack-contrib/style-loader
810[tests]: https://github.com/webpack-contrib/style-loader/workflows/style-loader/badge.svg
811[tests-url]: https://github.com/webpack-contrib/style-loader/actions
812[cover]: https://codecov.io/gh/webpack-contrib/style-loader/branch/master/graph/badge.svg
813[cover-url]: https://codecov.io/gh/webpack-contrib/style-loader
814[chat]: https://badges.gitter.im/webpack/webpack.svg
815[chat-url]: https://gitter.im/webpack/webpack
816[size]: https://packagephobia.now.sh/badge?p=style-loader
817[size-url]: https://packagephobia.now.sh/result?p=style-loader