UNPKG

12.5 kBMarkdownView Raw
1[![npm][npm]][npm-url]
2[![node][node]][node-url]
3[![deps][deps]][deps-url]
4[![chat][chat]][chat-url]
5
6<div align="center">
7 <a href="https://github.com/webpack/webpack">
8 <img width="200" height="200"
9 src="https://webpack.js.org/assets/icon-square-big.svg">
10 </a>
11 <h1>Style Loader</h1>
12 <p>Adds CSS to the DOM by injecting a <code>&lt;style&gt;</code> tag</p>
13</div>
14
15<h2 align="center">Install</h2>
16
17```bash
18npm install style-loader --save-dev
19```
20
21<h2 align="center"><a href="https://webpack.js.org/concepts/loaders">Usage</a></h2>
22
23It's recommended to combine `style-loader` with the [`css-loader`](https://github.com/webpack/css-loader)
24
25**component.js**
26```js
27import style from './file.css'
28```
29
30**webpack.config.js**
31```js
32{
33 module: {
34 rules: [
35 {
36 test: /\.css$/,
37 use: [
38 { loader: "style-loader" },
39 { loader: "css-loader" }
40 ]
41 }
42 ]
43 }
44}
45```
46
47#### `Locals (CSS Modules)`
48
49When using [local scoped CSS](https://github.com/webpack/css-loader#css-scope) the module exports the generated identifiers (locals).
50
51**component.js**
52```js
53import style from './file.css'
54
55style.className === "z849f98ca812"
56```
57
58### `Url`
59
60It's also possible to add a URL `<link href="path/to/file.css" rel="stylesheet">` instead of inlining the CSS `{String}` with `<style></style>` tag.
61
62```js
63import url from 'file.css'
64```
65
66**webpack.config.js**
67```js
68{
69 module: {
70 rules: [
71 {
72 test: /\.css$/,
73 use: [
74 { loader: "style-loader/url" },
75 { loader: "file-loader" }
76 ]
77 }
78 ]
79 }
80}
81```
82
83```html
84<link rel="stylesheet" href="path/to/file.css">
85```
86
87> :information_source: Source maps and assets referenced with `url`: when style loader is used with `{ options: { sourceMap: true } }` option, the CSS modules will be generated as `Blob`s, so relative paths don't work (they would be relative to `chrome:blob` or `chrome:devtools`). In order for assets to maintain correct paths setting `output.publicPath` property of webpack configuration must be set, so that absolute paths are generated. Alternatively you can enable the `convertToAbsoluteUrls` option mentioned above.
88
89### `Useable`
90
91The `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`
92
93By convention the `Reference Counter API` should be bound to `.useable.css` and the `.css` should be loaded with basic `style-loader` usage.(similar to other file types, i.e. `.useable.less` and `.less`).
94
95**webpack.config.js**
96```js
97{
98 module: {
99 rules: [
100 {
101 test: /\.css$/,
102 use: [
103 { loader: "style-loader" },
104 { loader: "css-loader" },
105 ],
106 },
107 {
108 test: /\.useable\.css$/,
109 use: [
110 {
111 loader: "style-loader/useable"
112 },
113 { loader: "css-loader" },
114 ],
115 },
116 ],
117 },
118}
119```
120
121#### `Reference Counter API`
122
123**component.js**
124```js
125import style from './file.css'
126
127style.use(); // = style.ref();
128style.unuse(); // = style.unref();
129```
130
131Styles are not added on `import/require()`, but instead on call to `use`/`ref`. Styles are removed from page if `unuse`/`unref` is called exactly as often as `use`/`ref`.
132
133:warning: Behavior is undefined when `unuse`/`unref` is called more often than `use`/`ref`. Don't do that.
134
135<h2 align="center">Options</h2>
136
137|Name|Type|Default|Description|
138|:--:|:--:|:-----:|:----------|
139|**`hmr`**|`{Boolean}`|`true`|Enable/disable Hot Module Replacement (HMR), if disabled no HMR Code will be added (good for non local development/production)|
140|**`base`** |`{Number}`|`true`|Set module ID base (DLLPlugin)|
141|**`attrs`**|`{Object}`|`{}`|Add custom attrs to `<style></style>`|
142|**`transform`** |`{Function}`|`false`|Transform/Conditionally load CSS by passing a transform/condition function|
143|**`insertAt`**|`{String\|Object}`|`bottom`|Inserts `<style></style>` at the given position|
144|**`insertInto`**|`{String\|Function}`|`<head>`|Inserts `<style></style>` into the given position|
145|**`singleton`**|`{Boolean}`|`undefined`|Reuses a single `<style></style>` element, instead of adding/removing individual elements for each required module.|
146|**`sourceMap`**|`{Boolean}`|`false`|Enable/Disable Sourcemaps|
147|**`convertToAbsoluteUrls`**|`{Boolean}`|`false`|Converts relative URLs to absolute urls, when source maps are enabled|
148
149### `hmr`
150
151Enable/disable Hot Module Replacement (HMR), if disabled no HMR Code will be added.
152This could be used for non local development and production.
153
154**webpack.config.js**
155```js
156{
157 loader: 'style-loader',
158 options: {
159 hmr: false
160 }
161}
162```
163
164### `base`
165
166This 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.:
167
168**webpack.dll1.config.js**
169```js
170{
171 test: /\.css$/,
172 use: [
173 'style-loader',
174 'css-loader'
175 ]
176}
177```
178
179**webpack.dll2.config.js**
180```js
181{
182 test: /\.css$/,
183 use: [
184 { loader: 'style-loader', options: { base: 1000 } },
185 'css-loader'
186 ]
187}
188```
189
190**webpack.app.config.js**
191```
192{
193 test: /\.css$/,
194 use: [
195 { loader: 'style-loader', options: { base: 2000 } },
196 'css-loader'
197 ]
198}
199```
200
201### `attrs`
202
203If defined, style-loader will attach given attributes with their values on `<style>` / `<link>` element.
204
205**component.js**
206```js
207import style from './file.css'
208```
209
210**webpack.config.js**
211```js
212{
213 test: /\.css$/,
214 use: [
215 { loader: 'style-loader', options: { attrs: { id: 'id' } } }
216 { loader: 'css-loader' }
217 ]
218}
219```
220
221```html
222<style id="id"></style>
223```
224
225#### `Url`
226
227**component.js**
228```js
229import link from './file.css'
230```
231
232**webpack.config.js**
233```js
234{
235 test: /\.css$/,
236 use: [
237 { loader: 'style-loader/url', options: { attrs: { id: 'id' } } }
238 { loader: 'file-loader' }
239 ]
240}
241```
242
243### `transform`
244
245A `transform` is a function that can modify the css just before it is loaded into the page by the style-loader.
246This function will be called on the css that is about to be loaded and the return value of the function will be loaded into the page instead of the original css.
247If the return value of the `transform` function is falsy, the css will not be loaded into the page at all.
248
249**webpack.config.js**
250```js
251{
252 loader: 'style-loader',
253 options: {
254 transform: 'path/to/transform.js'
255 }
256}
257```
258
259**transform.js**
260```js
261module.exports = function (css) {
262 // Here we can change the original css
263 const transformed = css.replace('.classNameA', '.classNameB')
264
265 return transformed
266}
267```
268
269#### `Conditional`
270
271**webpack.config.js**
272```js
273{
274 loader: 'style-loader',
275 options: {
276 transform: 'path/to/conditional.js'
277 }
278}
279```
280
281**conditional.js**
282```js
283module.exports = function (css) {
284 // If the condition is matched load [and transform] the CSS
285 if (css.includes('something I want to check')) {
286 return css;
287 }
288 // If a falsy value is returned, the CSS won't be loaded
289 return false
290}
291```
292
293### `insertAt`
294
295By default, the style-loader appends `<style>` elements to the end of the style target, which is the `<head>` tag of the page unless specified by `insertInto`. This will cause CSS created by the loader to take priority over CSS already present in the target. To insert style elements at the beginning of the target, set this query parameter to 'top', e.g
296
297**webpack.config.js**
298```js
299{
300 loader: 'style-loader',
301 options: {
302 insertAt: 'top'
303 }
304}
305```
306
307A new `<style>` element can be inserted before a specific element by passing an object, e.g.
308
309**webpack.config.js**
310```js
311{
312 loader: 'style-loader',
313 options: {
314 insertAt: {
315 before: '#id'
316 }
317 }
318}
319```
320
321### `insertInto`
322By default, the style-loader inserts the `<style>` elements into the `<head>` tag of the page. If you want the tags to be inserted somewhere else you can specify a CSS selector for that element here. If 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.
323
324You can also pass function to override default behavior and insert styles in your container, e.g
325
326**webpack.config.js**
327```js
328{
329 loader: 'style-loader',
330 options: {
331 insertInto: () => document.querySelector("#root"),
332 }
333}
334```
335
336Using function you can insert the styles into a [ShadowRoot](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot), e.g
337
338**webpack.config.js**
339```js
340{
341 loader: 'style-loader',
342 options: {
343 insertInto: () => document.querySelector("#root").shadowRoot,
344 }
345}
346```
347
348### `singleton`
349
350If defined, the style-loader will reuse a single `<style></style>` element, instead of adding/removing individual elements for each required module.
351
352> ℹ️ This option is on by default in IE9, which has strict limitations on the number of style tags allowed on a page. You can enable or disable it with the singleton option.
353
354**webpack.config.js**
355```js
356{
357 loader: 'style-loader',
358 options: {
359 singleton: true
360 }
361}
362```
363
364### `sourceMap`
365
366Enable/Disable source map loading
367
368**webpack.config.js**
369```js
370{
371 loader: 'style-loader',
372 options: {
373 sourceMap: true
374 }
375}
376```
377
378### `convertToAbsoluteUrls`
379
380If convertToAbsoluteUrls and sourceMaps are both enabled, relative urls will be converted to absolute urls right before the css is injected into the page. This resolves [an issue](https://github.com/webpack/style-loader/pull/96) where relative resources fail to load when source maps are enabled. You can enable it with the convertToAbsoluteUrls option.
381
382**webpack.config.js**
383```js
384{
385 loader: 'style-loader',
386 options: {
387 sourceMap: true,
388 convertToAbsoluteUrls: true
389 }
390}
391```
392
393<h2 align="center">Maintainers</h2>
394
395<table>
396 <tbody>
397 <tr>
398 <td align="center">
399 <a href="https://github.com/bebraw">
400 <img width="150" height="150" src="https://github.com/bebraw.png?v=3&s=150">
401 </br>
402 Juho Vepsäläinen
403 </a>
404 </td>
405 <td align="center">
406 <a href="https://github.com/d3viant0ne">
407 <img width="150" height="150" src="https://github.com/d3viant0ne.png?v=3&s=150">
408 </br>
409 Joshua Wiens
410 </a>
411 </td>
412 <td align="center">
413 <a href="https://github.com/sapegin">
414 <img width="150" height="150" src="https://github.com/sapegin.png?v=3&s=150">
415 </br>
416 Artem Sapegin
417 </a>
418 </td>
419 <td align="center">
420 <a href="https://github.com/michael-ciniawsky">
421 <img width="150" height="150" src="https://github.com/michael-ciniawsky.png?v=3&s=150">
422 </br>
423 Michael Ciniawsky
424 </a>
425 </td>
426 <td align="center">
427 <a href="https://github.com/evilebottnawi">
428 <img width="150" height="150" src="https://github.com/evilebottnawi.png?v=3&s=150">
429 </br>
430 Alexander Krasnoyarov
431 </a>
432 </td>
433 </tr>
434 <tr>
435 <td align="center">
436 <a href="https://github.com/sokra">
437 <img width="150" height="150" src="https://github.com/sokra.png?v=3&s=150">
438 </br>
439 Tobias Koppers
440 </a>
441 </td>
442 <td align="center">
443 <a href="https://github.com/SpaceK33z">
444 <img width="150" height="150" src="https://github.com/SpaceK33z.png?v=3&s=150">
445 </br>
446 Kees Kluskens
447 </a>
448 </td>
449 <tr>
450 <tbody>
451</table>
452
453
454[npm]: https://img.shields.io/npm/v/style-loader.svg
455[npm-url]: https://npmjs.com/package/style-loader
456
457[node]: https://img.shields.io/node/v/style-loader.svg
458[node-url]: https://nodejs.org
459
460[deps]: https://david-dm.org/webpack/style-loader.svg
461[deps-url]: https://david-dm.org/webpack/file-loader
462
463[chat]: https://badges.gitter.im/webpack/webpack.svg
464[chat-url]: https://gitter.im/webpack/webpack