Skip to content

Commit 3824f83

Browse files
docs: improve (#417)
1 parent 1a410df commit 3824f83

File tree

1 file changed

+97
-77
lines changed

1 file changed

+97
-77
lines changed

README.md

Lines changed: 97 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
# style-loader
1818

19-
Adds CSS to the DOM.
19+
Inject CSS into the DOM.
2020

2121
## Getting Started
2222

@@ -41,7 +41,7 @@ body {
4141
**component.js**
4242

4343
```js
44-
import style from './style.css';
44+
import './style.css';
4545
```
4646

4747
**webpack.config.js**
@@ -52,40 +52,28 @@ module.exports = {
5252
rules: [
5353
{
5454
test: /\.css$/i,
55-
use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
55+
use: ['style-loader', 'css-loader'],
5656
},
5757
],
5858
},
5959
};
6060
```
6161

62-
### `Locals (CSS Modules)`
63-
64-
When using [local scoped CSS](https://github.com/webpack/css-loader#css-scope) the module exports the generated identifiers (locals):
65-
66-
**component.js**
67-
68-
```js
69-
import style from './file.css';
70-
71-
style.className === 'z849f98ca812';
72-
```
73-
7462
## Options
7563

76-
| Name | Type | Default | Description |
77-
| :--------------: | :------------------: | :--------: | :--------------------------------------------------- |
78-
| **`injectType`** | `{String}` | `styleTag` | Allows to setup how styles will be injected into DOM |
79-
| **`attributes`** | `{Object}` | `{}` | Adds custom attributes to tag |
80-
| **`insert`** | `{String\|Function}` | `head` | Inserts tag at the given position into DOM |
81-
| **`base`** | `{Number}` | `true` | Sets module ID base (DLLPlugin) |
64+
| Name | Type | Default | Description |
65+
| :--------------: | :------------------: | :--------: | :------------------------------------------------------- |
66+
| **`injectType`** | `{String}` | `styleTag` | Allows to setup how styles will be injected into the DOM |
67+
| **`attributes`** | `{Object}` | `{}` | Adds custom attributes to tag |
68+
| **`insert`** | `{String\|Function}` | `head` | Inserts tag at the given position into the DOM |
69+
| **`base`** | `{Number}` | `true` | Sets module ID base (DLLPlugin) |
8270

8371
### `injectType`
8472

8573
Type: `String`
8674
Default: `styleTag`
8775

88-
Allows to setup how styles will be injected into DOM.
76+
Allows to setup how styles will be injected into the DOM.
8977

9078
Possible values:
9179

@@ -95,60 +83,29 @@ Possible values:
9583
- `lazySingletonStyleTag`
9684
- `linkTag`
9785

98-
When you `lazyStyleTag` or `lazySingletonStyleTag` value the `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`.
86+
#### `styleTag`
87+
88+
Automatically injects styles into the DOM using multiple `<style></style>`. It is **default** behaviour.
9989

10090
**component.js**
10191

10292
```js
103-
import style from './file.css';
104-
105-
style.use();
106-
style.unuse();
93+
import './styles.css';
10794
```
10895

109-
We 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`).
96+
Example with c Locals (CSS Modules):
11097

111-
**webpack.config.js**
98+
**component-with-css-modules.js**
11299

113100
```js
114-
module.exports = {
115-
module: {
116-
rules: [
117-
{
118-
test: /\.css$/i,
119-
exclude: /\.lazy\.css$/i,
120-
use: ['style-loader', 'css-loader'],
121-
},
122-
{
123-
test: /\.lazy\.css$/i,
124-
use: [
125-
{
126-
loader: 'style-loader',
127-
options: {
128-
// Can be `'lazyStyleTag'` or `'lazySingletonStyleTag'`
129-
injectType: 'lazyStyleTag',
130-
},
131-
},
132-
'css-loader',
133-
],
134-
},
135-
],
136-
},
137-
};
138-
```
139-
140-
Styles are not added on `import/require()`, but instead on call to `use`. Styles are removed from page if `unuse` is called exactly as often as `use`.
141-
142-
> ⚠️ Behavior is undefined when `unuse` is called more often than `use`. Don't do that.
143-
144-
#### `styleTag`
145-
146-
Injects styles in multiple `<style></style>`. It is **default** behaviour.
101+
import styles from './styles.css';
147102

148-
```js
149-
import './styles.css';
103+
const divElement = document.createElement('div');
104+
divElement.className = styles['my-class'];
150105
```
151106

107+
All locals (class names) stored in imported object.
108+
152109
**webpack.config.js**
153110

154111
```js
@@ -158,6 +115,7 @@ module.exports = {
158115
{
159116
test: /\.css$/i,
160117
use: [
118+
// The `injectType` option can be avoided because it is default behaviour
161119
{ loader: 'style-loader', options: { injectType: 'styleTag' } },
162120
'css-loader',
163121
],
@@ -184,14 +142,27 @@ The loader inject styles like:
184142

185143
#### `singletonStyleTag`
186144

187-
Injects styles in one `<style></style>`.
145+
Automatically injects styles into the DOM using one `<style></style>`.
188146

189147
> ⚠ Source maps do not work.
190148
149+
**component.js**
150+
191151
```js
192152
import './styles.css';
193153
```
194154

155+
**component-with-css-modules.js**
156+
157+
```js
158+
import styles from './styles.css';
159+
160+
const divElement = document.createElement('div');
161+
divElement.className = styles['my-class'];
162+
```
163+
164+
All locals (class names) stored in imported object.
165+
195166
**webpack.config.js**
196167

197168
```js
@@ -228,20 +199,46 @@ The loader inject styles like:
228199

229200
#### `lazyStyleTag`
230201

231-
Injects styles in multiple `<style></style>` on demand (documentation above).
202+
Injects styles into the DOM using multiple `<style></style>` on demand.
203+
We 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`).
204+
When you `lazyStyleTag` value the `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`.
205+
206+
> ⚠️ Behavior is undefined when `unuse` is called more often than `use`. Don't do that.
207+
208+
**component.js**
209+
210+
```js
211+
import styles from './styles.lazy.css';
212+
213+
styles.use();
214+
// For removing styles you can use
215+
// styles.unuse();
216+
```
217+
218+
**component-with-css-modules.js**
232219

233220
```js
234-
import styles from './styles.css';
221+
import styles from './styles.lazy.css';
235222

236223
styles.use();
224+
225+
const divElement = document.createElement('div');
226+
divElement.className = styles.locals['my-class'];
237227
```
238228

229+
All locals (class names) stored in `locals` property of imported object.
230+
239231
**webpack.config.js**
240232

241233
```js
242234
module.exports = {
243235
module: {
244236
rules: [
237+
{
238+
test: /\.css$/i,
239+
exclude: /\.lazy\.css$/i,
240+
use: ['style-loader', 'css-loader'],
241+
},
245242
{
246243
test: /\.lazy\.css$/i,
247244
use: [
@@ -271,29 +268,52 @@ The loader inject styles like:
271268

272269
#### `lazySingletonStyleTag`
273270

274-
Injects styles in one `<style></style>` on demand (documentation above).
271+
Injects styles into the DOM using one `<style></style>` on demand.
272+
We 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`).
273+
When you `lazySingletonStyleTag` value the `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`.
274+
275+
> ⚠️ Source maps do not work.
275276
276-
> ⚠ Source maps do not work.
277+
> ⚠️ Behavior is undefined when `unuse` is called more often than `use`. Don't do that.
278+
279+
**component.js**
277280

278281
```js
279282
import styles from './styles.css';
280283

281284
styles.use();
285+
// For removing styles you can use
286+
// styles.unuse();
282287
```
283288

289+
**component-with-css-modules.js**
290+
291+
```js
292+
import styles from './styles.lazy.css';
293+
294+
styles.use();
295+
296+
const divElement = document.createElement('div');
297+
divElement.className = styles.locals['my-class'];
298+
```
299+
300+
All locals (class names) stored in `locals` property of imported object.
301+
284302
**webpack.config.js**
285303

286304
```js
287305
module.exports = {
288306
module: {
289307
rules: [
308+
{
309+
test: /\.css$/i,
310+
exclude: /\.lazy\.css$/i,
311+
use: ['style-loader', 'css-loader'],
312+
},
290313
{
291314
test: /\.lazy\.css$/i,
292315
use: [
293-
{
294-
loader: 'style-loader',
295-
options: { injectType: 'lazySingletonStyleTag' },
296-
},
316+
{ loader: 'style-loader', options: { injectType: 'lazyStyleTag' } },
297317
'css-loader',
298318
],
299319
},
@@ -317,7 +337,7 @@ The loader generate this:
317337

318338
#### `linkTag`
319339

320-
Injects styles in multiple `<link rel="stylesheet" href="path/to/file.css">` .
340+
Injects styles into the DOM using multiple `<link rel="stylesheet" href="path/to/file.css">` .
321341

322342
```js
323343
import './styles.css';
@@ -331,7 +351,7 @@ module.exports = {
331351
module: {
332352
rules: [
333353
{
334-
test: /\.css$/i,
354+
test: /\.link\.css$/i,
335355
use: [
336356
{ loader: 'style-loader', options: { injectType: 'linkTag' } },
337357
{ loader: 'file-loader' },
@@ -354,7 +374,7 @@ The loader generate this:
354374
Type: `Object`
355375
Default: `{}`
356376

357-
If defined, style-loader will attach given attributes with their values on `<style>` / `<link>` element.
377+
If defined, the `style-loader` will attach given attributes with their values on `<style>` / `<link>` element.
358378

359379
**component.js**
360380

@@ -396,7 +416,7 @@ If you target an [iframe](https://developer.mozilla.org/en-US/docs/Web/API/HTMLI
396416

397417
#### `String`
398418

399-
Allows to setup custom [query selector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) where styles inject into DOM.
419+
Allows to setup custom [query selector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) where styles inject into the DOM.
400420

401421
**webpack.config.js**
402422

0 commit comments

Comments
 (0)