newer-universal-style-loader/README.md

133 lines
4.1 KiB
Markdown
Raw Permalink Normal View History

2021-08-11 21:12:43 +03:00
[![npm][npm]][npm-url]
[![node][node]][node-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>
2021-08-11 21:34:33 +03:00
<h1>Simple Universal Style Loader</h1>
2021-08-11 21:12:43 +03:00
</div>
Adds CSS to the DOM by injecting a `<style>` tag
2021-08-11 21:34:33 +03:00
## Install
2021-08-11 21:12:43 +03:00
```
2021-08-12 00:54:16 +03:00
npm install newer-universal-style-loader --save-dev
2021-08-11 21:12:43 +03:00
```
2021-08-11 21:34:33 +03:00
## Usage
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
[Documentation: Using loaders](https://webpack.js.org/concepts/loaders/)
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
### Webpack Configuration
2021-08-11 21:12:43 +03:00
2021-08-12 00:54:16 +03:00
`newer-universal-style-loader` is a drop-in replacement for the usual `style-loader`.
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
It's recommended to combine it with the [`css-loader`](https://github.com/webpack/css-loader).
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
Example for Webpack 5 with CSS module support:
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
```js
module: {
rules: [ {
test: /\.css$/,
use: [ {
2021-08-12 00:54:16 +03:00
loader: "newer-universal-style-loader",
2021-08-11 21:34:33 +03:00
options: { singleton: true }
}, {
loader: "css-loader",
options: {
modules: { localIdentName: "[name]--[local]--[hash:base64:8]" },
sourceMap: true,
importLoaders: 1
}
} ]
} ]
},
2021-08-11 21:12:43 +03:00
```
2021-08-11 21:34:33 +03:00
Then, in your JS code:
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
```js
import css from './file.css';
2021-08-11 21:12:43 +03:00
```
2021-08-11 21:34:33 +03:00
This will add rules in file.css to the document.
2021-08-11 21:12:43 +03:00
### Server environment
On server side we can't load styles into the DOM but to collect them and use when assembling the layout.
2021-08-11 21:34:33 +03:00
Use `getStyles()` to get captured styles in the form of a `<style>` element ready to be added into `<head>`.
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
Use this piece of code somewhere in your server-side bundle:
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
```js
2021-08-12 00:54:16 +03:00
import universal from 'newer-universal-style-loader/universal';
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
const html = universal.getStyles();
2021-08-11 21:12:43 +03:00
```
### Options
#### `insertAt`
2021-08-11 21:34:33 +03:00
By default, the 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`.
2021-08-11 21:12:43 +03:00
#### `insertInto`
2021-08-11 21:34:33 +03:00
By default, the loader inserts the `<style>` elements into the `<head>` tag of the page. If you
want the tags to be inserted somewhere else, e.g. into a [ShadowRoot](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot),
you can specify a CSS selector for that element here, e.g. `options: { insertInto: "#host::shadow>#root" }`.
2021-08-11 21:12:43 +03:00
#### `singleton`
2021-08-11 21:34:33 +03:00
If defined, the loader will re-use a single `<style>` element, instead of adding/removing individual
elements for each required module. This option is ON by default. To disable it, set `options: { singleton: false }`.
2021-08-11 21:12:43 +03:00
#### `convertToAbsoluteUrls`
2021-08-11 21:34:33 +03:00
If 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.
2021-08-11 21:12:43 +03:00
#### `attrs`
2021-08-11 21:34:33 +03:00
If defined, style-loader will attach given attributes with their values on each `<style>` / `<link>` element.
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03: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. Alternatively you can
enable the `convertToAbsoluteUrls` option mentioned above.
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
## Contributing
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
Don't hesitate to create a pull request. Every contribution is appreciated. In development you can start the
tests by calling `npm test`.
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
## Maintainer
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
Vitaliy Filippov https://github.com/vitalif
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
Also contains code authored by:
- Terence Chow https://github.com/terencechow
- Istvan Jano https://github.com/janoist1
- Tobias Koppers https://github.com/sokra
- Kees Kluskens https://github.com/SpaceK33z
2021-08-11 21:12:43 +03:00
2021-08-11 21:34:33 +03:00
## LICENSE
2021-08-11 21:12:43 +03:00
MIT
2021-08-12 00:54:16 +03:00
[npm]: https://img.shields.io/npm/v/newer-universal-style-loader.svg
[npm-url]: https://npmjs.com/package/newer-universal-style-loader
2021-08-11 21:12:43 +03:00
2021-08-12 00:54:16 +03:00
[node]: https://img.shields.io/node/v/newer-universal-style-loader.svg
2021-08-11 21:12:43 +03:00
[node-url]: https://nodejs.org