UNPKG

12.4 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
91By 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`).
92
93**webpack.config.js**
94```js
95{
96 module: {
97 rules: [
98 {
99 test: /\.css$/,
100 use: [
101 { loader: "style-loader" },
102 { loader: "css-loader" },
103 ],
104 },
105 {
106 test: /\.useable\.css$/,
107 use: [
108 {
109 loader: "style-loader/useable"
110 },
111 { loader: "css-loader" },
112 ],
113 },
114 ],
115 },
116}
117```
118
119#### `Reference Counter API`
120
121**component.js**
122```js
123import style from './file.css'
124
125style.use(); // = style.ref();
126style.unuse(); // = style.unref();
127```
128
129Styles 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`.
130
131:warning: Behavior is undefined when `unuse`/`unref` is called more often than `use`/`ref`. Don't do that.
132
133<h2 align="center">Options</h2>
134
135|Name|Type|Default|Description|
136|:--:|:--:|:-----:|:----------|
137|**`hmr`**|`{Boolean}`|`true`|Enable/disable Hot Module Replacement (HMR), if disabled no HMR Code will be added (good for non local development/production)|
138|**`base`** |`{Number}`|`true`|Set module ID base (DLLPlugin)|
139|**`attrs`**|`{Object}`|`{}`|Add custom attrs to `<style></style>`|
140|**`transform`** |`{Function}`|`false`|Transform/Conditionally load CSS by passing a transform/condition function|
141|**`insertAt`**|`{String\|Object}`|`bottom`|Inserts `<style></style>` at the given position|
142|**`insertInto`**|`{String\|Function}`|`<head>`|Inserts `<style></style>` into the given position|
143|**`singleton`**|`{Boolean}`|`undefined`|Reuses a single `<style></style>` element, instead of adding/removing individual elements for each required module.|
144|**`sourceMap`**|`{Boolean}`|`false`|Enable/Disable Sourcemaps|
145|**`convertToAbsoluteUrls`**|`{Boolean}`|`false`|Converts relative URLs to absolute urls, when source maps are enabled|
146
147### `hmr`
148
149Enable/disable Hot Module Replacement (HMR), if disabled no HMR Code will be added.
150This could be used for non local development and production.
151
152**webpack.config.js**
153```js
154{
155 loader: 'style-loader',
156 options: {
157 hmr: false
158 }
159}
160```
161
162### `base`
163
164This 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.:
165
166**webpack.dll1.config.js**
167```js
168{
169 test: /\.css$/,
170 use: [
171 'style-loader',
172 'css-loader'
173 ]
174}
175```
176
177**webpack.dll2.config.js**
178```js
179{
180 test: /\.css$/,
181 use: [
182 { loader: 'style-loader', options: { base: 1000 } },
183 'css-loader'
184 ]
185}
186```
187
188**webpack.app.config.js**
189```
190{
191 test: /\.css$/,
192 use: [
193 { loader: 'style-loader', options: { base: 2000 } },
194 'css-loader'
195 ]
196}
197```
198
199### `attrs`
200
201If defined, style-loader will attach given attributes with their values on `<style>` / `<link>` element.
202
203**component.js**
204```js
205import style from './file.css'
206```
207
208**webpack.config.js**
209```js
210{
211 test: /\.css$/,
212 use: [
213 { loader: 'style-loader', options: { attrs: { id: 'id' } } }
214 { loader: 'css-loader' }
215 ]
216}
217```
218
219```html
220<style id="id"></style>
221```
222
223#### `Url`
224
225**component.js**
226```js
227import link from './file.css'
228```
229
230**webpack.config.js**
231```js
232{
233 test: /\.css$/,
234 use: [
235 { loader: 'style-loader/url', options: { attrs: { id: 'id' } } }
236 { loader: 'file-loader' }
237 ]
238}
239```
240
241### `transform`
242
243A `transform` is a function that can modify the css just before it is loaded into the page by the style-loader.
244This 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.
245If the return value of the `transform` function is falsy, the css will not be loaded into the page at all.
246
247**webpack.config.js**
248```js
249{
250 loader: 'style-loader',
251 options: {
252 transform: 'path/to/transform.js'
253 }
254}
255```
256
257**transform.js**
258```js
259module.exports = function (css) {
260 // Here we can change the original css
261 const transformed = css.replace('.classNameA', '.classNameB')
262
263 return transformed
264}
265```
266
267#### `Conditional`
268
269**webpack.config.js**
270```js
271{
272 loader: 'style-loader',
273 options: {
274 transform: 'path/to/conditional.js'
275 }
276}
277```
278
279**conditional.js**
280```js
281module.exports = function (css) {
282 // If the condition is matched load [and transform] the CSS
283 if (css.includes('something I want to check')) {
284 return css;
285 }
286 // If a falsy value is returned, the CSS won't be loaded
287 return false
288}
289```
290
291### `insertAt`
292
293By 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
294
295**webpack.config.js**
296```js
297{
298 loader: 'style-loader',
299 options: {
300 insertAt: 'top'
301 }
302}
303```
304
305A new `<style>` element can be inserted before a specific element by passing an object, e.g.
306
307**webpack.config.js**
308```js
309{
310 loader: 'style-loader',
311 options: {
312 insertAt: {
313 before: '#id'
314 }
315 }
316}
317```
318
319### `insertInto`
320By 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.
321
322You can also pass function to override default behavior and insert styles in your container, e.g
323
324**webpack.config.js**
325```js
326{
327 loader: 'style-loader',
328 options: {
329 insertInto: () => document.querySelector("#root"),
330 }
331}
332```
333
334Using function you can insert the styles into a [ShadowRoot](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot), e.g
335
336**webpack.config.js**
337```js
338{
339 loader: 'style-loader',
340 options: {
341 insertInto: () => document.querySelector("#root").shadowRoot,
342 }
343}
344```
345
346### `singleton`
347
348If defined, the style-loader will reuse a single `<style></style>` element, instead of adding/removing individual elements for each required module.
349
350> ℹ️ 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.
351
352**webpack.config.js**
353```js
354{
355 loader: 'style-loader',
356 options: {
357 singleton: true
358 }
359}
360```
361
362### `sourceMap`
363
364Enable/Disable source map loading
365
366**webpack.config.js**
367```js
368{
369 loader: 'style-loader',
370 options: {
371 sourceMap: true
372 }
373}
374```
375
376### `convertToAbsoluteUrls`
377
378If 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.
379
380**webpack.config.js**
381```js
382{
383 loader: 'style-loader',
384 options: {
385 sourceMap: true,
386 convertToAbsoluteUrls: true
387 }
388}
389```
390
391<h2 align="center">Maintainers</h2>
392
393<table>
394 <tbody>
395 <tr>
396 <td align="center">
397 <a href="https://github.com/bebraw">
398 <img width="150" height="150" src="https://github.com/bebraw.png?v=3&s=150">
399 </br>
400 Juho Vepsäläinen
401 </a>
402 </td>
403 <td align="center">
404 <a href="https://github.com/d3viant0ne">
405 <img width="150" height="150" src="https://github.com/d3viant0ne.png?v=3&s=150">
406 </br>
407 Joshua Wiens
408 </a>
409 </td>
410 <td align="center">
411 <a href="https://github.com/sapegin">
412 <img width="150" height="150" src="https://github.com/sapegin.png?v=3&s=150">
413 </br>
414 Artem Sapegin
415 </a>
416 </td>
417 <td align="center">
418 <a href="https://github.com/michael-ciniawsky">
419 <img width="150" height="150" src="https://github.com/michael-ciniawsky.png?v=3&s=150">
420 </br>
421 Michael Ciniawsky
422 </a>
423 </td>
424 <td align="center">
425 <a href="https://github.com/evilebottnawi">
426 <img width="150" height="150" src="https://github.com/evilebottnawi.png?v=3&s=150">
427 </br>
428 Alexander Krasnoyarov
429 </a>
430 </td>
431 </tr>
432 <tr>
433 <td align="center">
434 <a href="https://github.com/sokra">
435 <img width="150" height="150" src="https://github.com/sokra.png?v=3&s=150">
436 </br>
437 Tobias Koppers
438 </a>
439 </td>
440 <td align="center">
441 <a href="https://github.com/SpaceK33z">
442 <img width="150" height="150" src="https://github.com/SpaceK33z.png?v=3&s=150">
443 </br>
444 Kees Kluskens
445 </a>
446 </td>
447 <tr>
448 <tbody>
449</table>
450
451
452[npm]: https://img.shields.io/npm/v/style-loader.svg
453[npm-url]: https://npmjs.com/package/style-loader
454
455[node]: https://img.shields.io/node/v/style-loader.svg
456[node-url]: https://nodejs.org
457
458[deps]: https://david-dm.org/webpack/style-loader.svg
459[deps-url]: https://david-dm.org/webpack/file-loader
460
461[chat]: https://badges.gitter.im/webpack/webpack.svg
462[chat-url]: https://gitter.im/webpack/webpack