newer-universal-style-loader/README.md

159 lines
5.0 KiB
Markdown
Raw Normal View History

[![npm][npm]][npm-url]
[![node][node]][node-url]
[![deps][deps]][deps-url]
[![chat][chat]][chat-url]
<div align="center">
<a href="https://github.com/webpack/webpack">
<img width="200" height="200"
src="https://webpack.js.org/assets/icon-square-big.svg">
</a>
<h1>Style Loader</h1>
</div>
2012-04-07 05:06:57 +04:00
Adds CSS to the DOM by injecting a `<style>` tag
<h2 align="center">Install</h2>
```
npm install style-loader --save-dev
```
<h2 align="center">Usage</h2>
2012-04-07 05:06:57 +04:00
2014-03-11 22:40:59 +04:00
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
2014-01-27 14:41:56 +04:00
### Simple API
2012-04-07 05:06:57 +04:00
``` javascript
require("style-loader!raw-loader!./file.css");
2012-04-07 05:06:57 +04:00
// => add rules in file.css to document
```
It's recommended to combine it with the [`css-loader`](https://github.com/webpack/css-loader): `require("style-loader!css-loader!./file.css")`.
2012-07-17 04:11:39 +04:00
2015-12-06 08:01:04 +03:00
It's also possible to add a URL instead of a CSS string:
2013-03-26 14:51:42 +04:00
``` javascript
require("style-loader/url!file-loader!./file.css");
2014-10-23 16:31:22 +04:00
// => add a <link rel="stylesheet"> to file.css to document
2013-03-26 14:51:42 +04:00
```
### Local scope CSS
2015-04-22 00:40:24 +03:00
(experimental)
When using [local scope CSS](https://github.com/webpack/css-loader#css-scope) the module exports the generated identifiers:
2015-04-22 00:40:24 +03:00
2015-12-06 08:01:04 +03:00
``` javascript
var style = require("style-loader!css-loader!./file.css");
2015-04-22 00:40:24 +03:00
style.placeholder1 === "z849f98ca812bc0d099a43e0f90184"
```
2014-01-27 14:41:56 +04:00
### Reference-counted API
``` javascript
var style = require("style-loader/useable!css-loader!./file.css");
2014-01-27 14:41:56 +04:00
style.use(); // = style.ref();
style.unuse(); // = style.unref();
```
2015-12-06 08:01:04 +03:00
Styles are not added on `require`, but instead on call to `use`/`ref`. Styles are removed from page if `unuse`/`unref` is called exactly as often as `use`/`ref`.
2014-01-27 14:41:56 +04:00
Note: Behavior is undefined when `unuse`/`unref` is called more often than `use`/`ref`. Don't do that.
2014-10-20 20:26:18 +04:00
### Options
#### `insertAt`
2015-10-19 21:54:35 +03:00
By default, the style-loader appends `<style>` elements to the end of the `<head>` tag of the page. This will cause CSS created by the loader to take priority over CSS already present in the document head. To insert style elements at the beginning of the head, set this query parameter to 'top', e.g. `require('../style.css?insertAt=top')`.
2014-10-20 20:26:18 +04:00
#### `singleton`
2015-06-19 02:00:39 +03:00
If defined, the style-loader will re-use a single `<style>` element, instead of adding/removing individual elements for each required module. **Note:** 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 query parameter (`?singleton` or `?-singleton`).
2014-10-20 20:26:18 +04:00
#### `attrs`
If defined, style-loader will attach given attributes with their values on `<style>` / `<link>` element.
Usage:
```javascript
require('style-loader?{attrs:{id: "style-tag-id"}}!style.scss');
// will create style tag <style id="style-tag-id">
```
### Recommended configuration
2014-03-01 19:50:30 +04:00
2015-12-06 08:01:04 +03:00
By convention the reference-counted API should be bound to `.useable.css` and the simple API to `.css` (similar to other file types, i.e. `.useable.less` and `.less`).
2014-03-01 19:50:30 +04:00
So the recommended configuration for webpack is:
``` javascript
{
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" },
],
},
{
test: /\.useable\.css$/,
use: [
{
loader: "style-loader/useable"
},
{ loader: "css-loader" },
],
},
],
},
2014-03-01 19:50:30 +04:00
}
```
**Note** about source maps support and assets referenced with `url`: when style loader is used with ?sourceMap 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.
<h2 align="center">Contributing</h2>
2012-04-07 05:06:57 +04:00
Don't hesitate to create a pull request. Every contribution is appreciated. In development you can start the tests by calling `npm test`.
<h2 align="center">Maintainers</h2>
<table>
<tbody>
<tr>
<td align="center">
<img width="150 height="150"
src="https://avatars.githubusercontent.com/sokra?v=3">
<br />
<a href="https://github.com/">Tobias Koppers</a>
</td>
<td align="center">
<img width="150 height="150"
src="https://avatars.githubusercontent.com/SpaceK33z?v=3">
<br />
<a href="https://github.com/">Kees Kluskens</a>
</td>
<tr>
<tbody>
</table>
<h2 align="center">LICENSE</h2>
MIT
[npm]: https://img.shields.io/npm/v/style-loader.svg
[npm-url]: https://npmjs.com/package/style-loader
[node]: https://img.shields.io/node/v/style-loader.svg
[node-url]: https://nodejs.org
[deps]: https://david-dm.org/webpack/style-loader.svg
[deps-url]: https://david-dm.org/webpack/file-loader
[chat]: https://badges.gitter.im/webpack/webpack.svg
[chat-url]: https://gitter.im/webpack/webpack