| 1 | <div align="center">
|
|---|
| 2 | <a href="https://github.com/webpack/webpack">
|
|---|
| 3 | <img width="200" height="200"
|
|---|
| 4 | src="https://webpack.js.org/assets/icon-square-big.svg">
|
|---|
| 5 | </a>
|
|---|
| 6 | <h1>Style Loader</h1>
|
|---|
| 7 | </div>
|
|---|
| 8 |
|
|---|
| 9 | [![npm][npm]][npm-url]
|
|---|
| 10 | [![node][node]][node-url]
|
|---|
| 11 | [![tests][tests]][tests-url]
|
|---|
| 12 | [![coverage][cover]][cover-url]
|
|---|
| 13 | [![discussion][discussion]][discussion-url]
|
|---|
| 14 | [![size][size]][size-url]
|
|---|
| 15 |
|
|---|
| 16 | # style-loader
|
|---|
| 17 |
|
|---|
| 18 | Inject CSS into the DOM.
|
|---|
| 19 |
|
|---|
| 20 | ## Getting Started
|
|---|
| 21 |
|
|---|
| 22 | To begin, you'll need to install `style-loader`:
|
|---|
| 23 |
|
|---|
| 24 | ```console
|
|---|
| 25 | npm install --save-dev style-loader
|
|---|
| 26 | ```
|
|---|
| 27 |
|
|---|
| 28 | or
|
|---|
| 29 |
|
|---|
| 30 | ```console
|
|---|
| 31 | yarn add -D style-loader
|
|---|
| 32 | ```
|
|---|
| 33 |
|
|---|
| 34 | or
|
|---|
| 35 |
|
|---|
| 36 | ```console
|
|---|
| 37 | pnpm add -D style-loader
|
|---|
| 38 | ```
|
|---|
| 39 |
|
|---|
| 40 | It's recommended to combine `style-loader` with the [`css-loader`](https://github.com/webpack-contrib/css-loader)
|
|---|
| 41 |
|
|---|
| 42 | Then add the loader to your `webpack` config. For example:
|
|---|
| 43 |
|
|---|
| 44 | **style.css**
|
|---|
| 45 |
|
|---|
| 46 | ```css
|
|---|
| 47 | body {
|
|---|
| 48 | background: green;
|
|---|
| 49 | }
|
|---|
| 50 | ```
|
|---|
| 51 |
|
|---|
| 52 | **component.js**
|
|---|
| 53 |
|
|---|
| 54 | ```js
|
|---|
| 55 | import "./style.css";
|
|---|
| 56 | ```
|
|---|
| 57 |
|
|---|
| 58 | **webpack.config.js**
|
|---|
| 59 |
|
|---|
| 60 | ```js
|
|---|
| 61 | module.exports = {
|
|---|
| 62 | module: {
|
|---|
| 63 | rules: [
|
|---|
| 64 | {
|
|---|
| 65 | test: /\.css$/i,
|
|---|
| 66 | use: ["style-loader", "css-loader"],
|
|---|
| 67 | },
|
|---|
| 68 | ],
|
|---|
| 69 | },
|
|---|
| 70 | };
|
|---|
| 71 | ```
|
|---|
| 72 |
|
|---|
| 73 | ## Security Warning
|
|---|
| 74 |
|
|---|
| 75 | This loader is primarily meant for development. The default settings are not safe for production environments. See the [recommended example configuration](#recommended) and the section on [nonces](#nonce) for details.
|
|---|
| 76 |
|
|---|
| 77 | ## Options
|
|---|
| 78 |
|
|---|
| 79 | - [**`injectType`**](#injecttype)
|
|---|
| 80 | - [**`attributes`**](#attributes)
|
|---|
| 81 | - [**`insert`**](#insert)
|
|---|
| 82 | - [**`styleTagTransform`**](#styleTagTransform)
|
|---|
| 83 | - [**`base`**](#base)
|
|---|
| 84 | - [**`esModule`**](#esmodule)
|
|---|
| 85 |
|
|---|
| 86 | ### `injectType`
|
|---|
| 87 |
|
|---|
| 88 | Type:
|
|---|
| 89 |
|
|---|
| 90 | ```ts
|
|---|
| 91 | type injectType =
|
|---|
| 92 | | "styleTag"
|
|---|
| 93 | | "singletonStyleTag"
|
|---|
| 94 | | "autoStyleTag"
|
|---|
| 95 | | "lazyStyleTag"
|
|---|
| 96 | | "lazySingletonStyleTag"
|
|---|
| 97 | | "lazyAutoStyleTag"
|
|---|
| 98 | | "linkTag";
|
|---|
| 99 | ```
|
|---|
| 100 |
|
|---|
| 101 | Default: `styleTag`
|
|---|
| 102 |
|
|---|
| 103 | Allows to setup how styles will be injected into the DOM.
|
|---|
| 104 |
|
|---|
| 105 | Possible values:
|
|---|
| 106 |
|
|---|
| 107 | #### `styleTag`
|
|---|
| 108 |
|
|---|
| 109 | Automatically injects styles into the DOM using multiple `<style></style>`. It is **default** behaviour.
|
|---|
| 110 |
|
|---|
| 111 | **component.js**
|
|---|
| 112 |
|
|---|
| 113 | ```js
|
|---|
| 114 | import "./styles.css";
|
|---|
| 115 | ```
|
|---|
| 116 |
|
|---|
| 117 | Example with Locals (CSS Modules):
|
|---|
| 118 |
|
|---|
| 119 | **component-with-css-modules.js**
|
|---|
| 120 |
|
|---|
| 121 | ```js
|
|---|
| 122 | import styles from "./styles.css";
|
|---|
| 123 |
|
|---|
| 124 | const divElement = document.createElement("div");
|
|---|
| 125 | divElement.className = styles["my-class"];
|
|---|
| 126 | ```
|
|---|
| 127 |
|
|---|
| 128 | All locals (class names) stored in imported object.
|
|---|
| 129 |
|
|---|
| 130 | **webpack.config.js**
|
|---|
| 131 |
|
|---|
| 132 | ```js
|
|---|
| 133 | module.exports = {
|
|---|
| 134 | module: {
|
|---|
| 135 | rules: [
|
|---|
| 136 | {
|
|---|
| 137 | test: /\.css$/i,
|
|---|
| 138 | use: [
|
|---|
| 139 | // The `injectType` option can be avoided because it is default behaviour
|
|---|
| 140 | { loader: "style-loader", options: { injectType: "styleTag" } },
|
|---|
| 141 | "css-loader",
|
|---|
| 142 | ],
|
|---|
| 143 | },
|
|---|
| 144 | ],
|
|---|
| 145 | },
|
|---|
| 146 | };
|
|---|
| 147 | ```
|
|---|
| 148 |
|
|---|
| 149 | The loader inject styles like:
|
|---|
| 150 |
|
|---|
| 151 | ```html
|
|---|
| 152 | <style>
|
|---|
| 153 | .foo {
|
|---|
| 154 | color: red;
|
|---|
| 155 | }
|
|---|
| 156 | </style>
|
|---|
| 157 | <style>
|
|---|
| 158 | .bar {
|
|---|
| 159 | color: blue;
|
|---|
| 160 | }
|
|---|
| 161 | </style>
|
|---|
| 162 | ```
|
|---|
| 163 |
|
|---|
| 164 | #### `singletonStyleTag`
|
|---|
| 165 |
|
|---|
| 166 | Automatically injects styles into the DOM using one `<style></style>`.
|
|---|
| 167 |
|
|---|
| 168 | > **Warning**
|
|---|
| 169 | >
|
|---|
| 170 | > Source maps do not work.
|
|---|
| 171 |
|
|---|
| 172 | **component.js**
|
|---|
| 173 |
|
|---|
| 174 | ```js
|
|---|
| 175 | import "./styles.css";
|
|---|
| 176 | ```
|
|---|
| 177 |
|
|---|
| 178 | **component-with-css-modules.js**
|
|---|
| 179 |
|
|---|
| 180 | ```js
|
|---|
| 181 | import styles from "./styles.css";
|
|---|
| 182 |
|
|---|
| 183 | const divElement = document.createElement("div");
|
|---|
| 184 | divElement.className = styles["my-class"];
|
|---|
| 185 | ```
|
|---|
| 186 |
|
|---|
| 187 | All locals (class names) stored in imported object.
|
|---|
| 188 |
|
|---|
| 189 | **webpack.config.js**
|
|---|
| 190 |
|
|---|
| 191 | ```js
|
|---|
| 192 | module.exports = {
|
|---|
| 193 | module: {
|
|---|
| 194 | rules: [
|
|---|
| 195 | {
|
|---|
| 196 | test: /\.css$/i,
|
|---|
| 197 | use: [
|
|---|
| 198 | {
|
|---|
| 199 | loader: "style-loader",
|
|---|
| 200 | options: { injectType: "singletonStyleTag" },
|
|---|
| 201 | },
|
|---|
| 202 | "css-loader",
|
|---|
| 203 | ],
|
|---|
| 204 | },
|
|---|
| 205 | ],
|
|---|
| 206 | },
|
|---|
| 207 | };
|
|---|
| 208 | ```
|
|---|
| 209 |
|
|---|
| 210 | The loader inject styles like:
|
|---|
| 211 |
|
|---|
| 212 | ```html
|
|---|
| 213 | <style>
|
|---|
| 214 | .foo {
|
|---|
| 215 | color: red;
|
|---|
| 216 | }
|
|---|
| 217 | .bar {
|
|---|
| 218 | color: blue;
|
|---|
| 219 | }
|
|---|
| 220 | </style>
|
|---|
| 221 | ```
|
|---|
| 222 |
|
|---|
| 223 | #### `autoStyleTag`
|
|---|
| 224 |
|
|---|
| 225 | Works the same as a [`styleTag`](#styleTag), but if the code is executed in IE6-9, turns on the [`singletonStyleTag`](#singletonStyleTag) mode.
|
|---|
| 226 |
|
|---|
| 227 | #### `lazyStyleTag`
|
|---|
| 228 |
|
|---|
| 229 | Injects styles into the DOM using multiple `<style></style>` on demand.
|
|---|
| 230 | 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`).
|
|---|
| 231 | When you `lazyStyleTag` value the `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`.
|
|---|
| 232 |
|
|---|
| 233 | > ⚠️ Behavior is undefined when `unuse` is called more often than `use`. Don't do that.
|
|---|
| 234 |
|
|---|
| 235 | **component.js**
|
|---|
| 236 |
|
|---|
| 237 | ```js
|
|---|
| 238 | import styles from "./styles.lazy.css";
|
|---|
| 239 |
|
|---|
| 240 | styles.use();
|
|---|
| 241 | // For removing styles you can use
|
|---|
| 242 | // styles.unuse();
|
|---|
| 243 | ```
|
|---|
| 244 |
|
|---|
| 245 | **component-with-css-modules.js**
|
|---|
| 246 |
|
|---|
| 247 | ```js
|
|---|
| 248 | import styles from "./styles.lazy.css";
|
|---|
| 249 |
|
|---|
| 250 | styles.use();
|
|---|
| 251 |
|
|---|
| 252 | const divElement = document.createElement("div");
|
|---|
| 253 | divElement.className = styles.locals["my-class"];
|
|---|
| 254 | ```
|
|---|
| 255 |
|
|---|
| 256 | All locals (class names) stored in `locals` property of imported object.
|
|---|
| 257 |
|
|---|
| 258 | **webpack.config.js**
|
|---|
| 259 |
|
|---|
| 260 | ```js
|
|---|
| 261 | module.exports = {
|
|---|
| 262 | module: {
|
|---|
| 263 | rules: [
|
|---|
| 264 | {
|
|---|
| 265 | test: /\.css$/i,
|
|---|
| 266 | exclude: /\.lazy\.css$/i,
|
|---|
| 267 | use: ["style-loader", "css-loader"],
|
|---|
| 268 | },
|
|---|
| 269 | {
|
|---|
| 270 | test: /\.lazy\.css$/i,
|
|---|
| 271 | use: [
|
|---|
| 272 | { loader: "style-loader", options: { injectType: "lazyStyleTag" } },
|
|---|
| 273 | "css-loader",
|
|---|
| 274 | ],
|
|---|
| 275 | },
|
|---|
| 276 | ],
|
|---|
| 277 | },
|
|---|
| 278 | };
|
|---|
| 279 | ```
|
|---|
| 280 |
|
|---|
| 281 | The loader inject styles like:
|
|---|
| 282 |
|
|---|
| 283 | ```html
|
|---|
| 284 | <style>
|
|---|
| 285 | .foo {
|
|---|
| 286 | color: red;
|
|---|
| 287 | }
|
|---|
| 288 | </style>
|
|---|
| 289 | <style>
|
|---|
| 290 | .bar {
|
|---|
| 291 | color: blue;
|
|---|
| 292 | }
|
|---|
| 293 | </style>
|
|---|
| 294 | ```
|
|---|
| 295 |
|
|---|
| 296 | #### `lazySingletonStyleTag`
|
|---|
| 297 |
|
|---|
| 298 | Injects styles into the DOM using one `<style></style>` on demand.
|
|---|
| 299 | 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`).
|
|---|
| 300 | When you `lazySingletonStyleTag` value the `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`.
|
|---|
| 301 |
|
|---|
| 302 | > ⚠️ Source maps do not work.
|
|---|
| 303 |
|
|---|
| 304 | > ⚠️ Behavior is undefined when `unuse` is called more often than `use`. Don't do that.
|
|---|
| 305 |
|
|---|
| 306 | **component.js**
|
|---|
| 307 |
|
|---|
| 308 | ```js
|
|---|
| 309 | import styles from "./styles.css";
|
|---|
| 310 |
|
|---|
| 311 | styles.use();
|
|---|
| 312 | // For removing styles you can use
|
|---|
| 313 | // styles.unuse();
|
|---|
| 314 | ```
|
|---|
| 315 |
|
|---|
| 316 | **component-with-css-modules.js**
|
|---|
| 317 |
|
|---|
| 318 | ```js
|
|---|
| 319 | import styles from "./styles.lazy.css";
|
|---|
| 320 |
|
|---|
| 321 | styles.use();
|
|---|
| 322 |
|
|---|
| 323 | const divElement = document.createElement("div");
|
|---|
| 324 | divElement.className = styles.locals["my-class"];
|
|---|
| 325 | ```
|
|---|
| 326 |
|
|---|
| 327 | All locals (class names) stored in `locals` property of imported object.
|
|---|
| 328 |
|
|---|
| 329 | **webpack.config.js**
|
|---|
| 330 |
|
|---|
| 331 | ```js
|
|---|
| 332 | module.exports = {
|
|---|
| 333 | module: {
|
|---|
| 334 | rules: [
|
|---|
| 335 | {
|
|---|
| 336 | test: /\.css$/i,
|
|---|
| 337 | exclude: /\.lazy\.css$/i,
|
|---|
| 338 | use: ["style-loader", "css-loader"],
|
|---|
| 339 | },
|
|---|
| 340 | {
|
|---|
| 341 | test: /\.lazy\.css$/i,
|
|---|
| 342 | use: [
|
|---|
| 343 | {
|
|---|
| 344 | loader: "style-loader",
|
|---|
| 345 | options: { injectType: "lazySingletonStyleTag" },
|
|---|
| 346 | },
|
|---|
| 347 | "css-loader",
|
|---|
| 348 | ],
|
|---|
| 349 | },
|
|---|
| 350 | ],
|
|---|
| 351 | },
|
|---|
| 352 | };
|
|---|
| 353 | ```
|
|---|
| 354 |
|
|---|
| 355 | The loader generate this:
|
|---|
| 356 |
|
|---|
| 357 | ```html
|
|---|
| 358 | <style>
|
|---|
| 359 | .foo {
|
|---|
| 360 | color: red;
|
|---|
| 361 | }
|
|---|
| 362 | .bar {
|
|---|
| 363 | color: blue;
|
|---|
| 364 | }
|
|---|
| 365 | </style>
|
|---|
| 366 | ```
|
|---|
| 367 |
|
|---|
| 368 | #### `lazyAutoStyleTag`
|
|---|
| 369 |
|
|---|
| 370 | Works the same as a [`lazyStyleTag`](#lazyStyleTag), but if the code is executed in IE6-9, turns on the [`lazySingletonStyleTag`](#lazySingletonStyleTag) mode.
|
|---|
| 371 |
|
|---|
| 372 | #### `linkTag`
|
|---|
| 373 |
|
|---|
| 374 | Injects styles into the DOM using multiple `<link rel="stylesheet" href="path/to/file.css">` .
|
|---|
| 375 |
|
|---|
| 376 | > ℹ️ The loader will dynamically insert the `<link href="path/to/file.css" rel="stylesheet">` tag at runtime via JavaScript. You should use [MiniCssExtractPlugin](https://webpack.js.org/plugins/mini-css-extract-plugin/) if you want to include a static `<link href="path/to/file.css" rel="stylesheet">`.
|
|---|
| 377 |
|
|---|
| 378 | ```js
|
|---|
| 379 | import "./styles.css";
|
|---|
| 380 | import "./other-styles.css";
|
|---|
| 381 | ```
|
|---|
| 382 |
|
|---|
| 383 | **webpack.config.js**
|
|---|
| 384 |
|
|---|
| 385 | ```js
|
|---|
| 386 | module.exports = {
|
|---|
| 387 | module: {
|
|---|
| 388 | rules: [
|
|---|
| 389 | {
|
|---|
| 390 | test: /\.link\.css$/i,
|
|---|
| 391 | use: [
|
|---|
| 392 | { loader: "style-loader", options: { injectType: "linkTag" } },
|
|---|
| 393 | { loader: "file-loader" },
|
|---|
| 394 | ],
|
|---|
| 395 | },
|
|---|
| 396 | ],
|
|---|
| 397 | },
|
|---|
| 398 | };
|
|---|
| 399 | ```
|
|---|
| 400 |
|
|---|
| 401 | The loader generate this:
|
|---|
| 402 |
|
|---|
| 403 | ```html
|
|---|
| 404 | <link rel="stylesheet" href="path/to/style.css" />
|
|---|
| 405 | <link rel="stylesheet" href="path/to/other-styles.css" />
|
|---|
| 406 | ```
|
|---|
| 407 |
|
|---|
| 408 | ### `attributes`
|
|---|
| 409 |
|
|---|
| 410 | Type:
|
|---|
| 411 |
|
|---|
| 412 | ```ts
|
|---|
| 413 | type attributes = HTMLAttributes;
|
|---|
| 414 | ```
|
|---|
| 415 |
|
|---|
| 416 | Default: `{}`
|
|---|
| 417 |
|
|---|
| 418 | If defined, the `style-loader` will attach given attributes with their values on `<style>` / `<link>` element.
|
|---|
| 419 |
|
|---|
| 420 | **component.js**
|
|---|
| 421 |
|
|---|
| 422 | ```js
|
|---|
| 423 | import style from "./file.css";
|
|---|
| 424 | ```
|
|---|
| 425 |
|
|---|
| 426 | **webpack.config.js**
|
|---|
| 427 |
|
|---|
| 428 | ```js
|
|---|
| 429 | module.exports = {
|
|---|
| 430 | module: {
|
|---|
| 431 | rules: [
|
|---|
| 432 | {
|
|---|
| 433 | test: /\.css$/i,
|
|---|
| 434 | use: [
|
|---|
| 435 | { loader: "style-loader", options: { attributes: { id: "id" } } },
|
|---|
| 436 | { loader: "css-loader" },
|
|---|
| 437 | ],
|
|---|
| 438 | },
|
|---|
| 439 | ],
|
|---|
| 440 | },
|
|---|
| 441 | };
|
|---|
| 442 | ```
|
|---|
| 443 |
|
|---|
| 444 | ```html
|
|---|
| 445 | <style id="id"></style>
|
|---|
| 446 | ```
|
|---|
| 447 |
|
|---|
| 448 | ### `insert`
|
|---|
| 449 |
|
|---|
| 450 | Type:
|
|---|
| 451 |
|
|---|
| 452 | ```ts
|
|---|
| 453 | type insert =
|
|---|
| 454 | | string
|
|---|
| 455 | | ((htmlElement: HTMLElement, options: Record<string, any>) => void);
|
|---|
| 456 | ```
|
|---|
| 457 |
|
|---|
| 458 | Default: `head`
|
|---|
| 459 |
|
|---|
| 460 | By default, the `style-loader` appends `<style>`/`<link>` elements to the end of the style target, which is the `<head>` tag of the page unless specified by `insert`.
|
|---|
| 461 | This will cause CSS created by the loader to take priority over CSS already present in the target.
|
|---|
| 462 | You can use other values if the standard behavior is not suitable for you, but we do not recommend doing this.
|
|---|
| 463 | 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.
|
|---|
| 464 |
|
|---|
| 465 | #### `string`
|
|---|
| 466 |
|
|---|
| 467 | ##### `Selector`
|
|---|
| 468 |
|
|---|
| 469 | Allows to setup custom [query selector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) where styles inject into the DOM.
|
|---|
| 470 |
|
|---|
| 471 | **webpack.config.js**
|
|---|
| 472 |
|
|---|
| 473 | ```js
|
|---|
| 474 | module.exports = {
|
|---|
| 475 | module: {
|
|---|
| 476 | rules: [
|
|---|
| 477 | {
|
|---|
| 478 | test: /\.css$/i,
|
|---|
| 479 | use: [
|
|---|
| 480 | {
|
|---|
| 481 | loader: "style-loader",
|
|---|
| 482 | options: {
|
|---|
| 483 | insert: "body",
|
|---|
| 484 | },
|
|---|
| 485 | },
|
|---|
| 486 | "css-loader",
|
|---|
| 487 | ],
|
|---|
| 488 | },
|
|---|
| 489 | ],
|
|---|
| 490 | },
|
|---|
| 491 | };
|
|---|
| 492 | ```
|
|---|
| 493 |
|
|---|
| 494 | ##### `Absolute path to function`
|
|---|
| 495 |
|
|---|
| 496 | Allows to setup absolute path to custom function that allows to override default behavior and insert styles at any position.
|
|---|
| 497 |
|
|---|
| 498 | > **Warning**
|
|---|
| 499 | >
|
|---|
| 500 | > Do not forget that this code will be used in the browser and not all browsers support latest ECMA features like `let`, `const`, `arrow function expression` and etc. We recommend using [`babel-loader`](https://webpack.js.org/loaders/babel-loader/) for support latest ECMA features.
|
|---|
| 501 |
|
|---|
| 502 | > **Warning**
|
|---|
| 503 | >
|
|---|
| 504 | > Do not forget that some DOM methods may not be available in older browsers, we recommended use only [DOM core level 2 properties](https://caniuse.com/#search=DOM%20Core), but it is depends what browsers you want to support
|
|---|
| 505 |
|
|---|
| 506 | **webpack.config.js**
|
|---|
| 507 |
|
|---|
| 508 | ```js
|
|---|
| 509 | module.exports = {
|
|---|
| 510 | module: {
|
|---|
| 511 | rules: [
|
|---|
| 512 | {
|
|---|
| 513 | test: /\.css$/i,
|
|---|
| 514 | use: [
|
|---|
| 515 | {
|
|---|
| 516 | loader: "style-loader",
|
|---|
| 517 | options: {
|
|---|
| 518 | insert: require.resolve("modulePath"),
|
|---|
| 519 | },
|
|---|
| 520 | },
|
|---|
| 521 | "css-loader",
|
|---|
| 522 | ],
|
|---|
| 523 | },
|
|---|
| 524 | ],
|
|---|
| 525 | },
|
|---|
| 526 | };
|
|---|
| 527 | ```
|
|---|
| 528 |
|
|---|
| 529 | A new `<style>`/`<link>` elements will be inserted into at bottom of `body` tag.
|
|---|
| 530 |
|
|---|
| 531 | #### `function`
|
|---|
| 532 |
|
|---|
| 533 | Allows to override default behavior and insert styles at any position.
|
|---|
| 534 |
|
|---|
| 535 | > **Warning**
|
|---|
| 536 | >
|
|---|
| 537 | > Do not forget that this code will be used in the browser and not all browsers support latest ECMA features like `let`, `const`, `arrow function expression` and etc, we recommend use only ECMA 5 features, but it is depends what browsers you want to support
|
|---|
| 538 |
|
|---|
| 539 | > **Warning**
|
|---|
| 540 | >
|
|---|
| 541 | > Do not forget that some DOM methods may not be available in older browsers, we recommended use only [DOM core level 2 properties](https://caniuse.com/#search=DOM%20Core), but it is depends what browsers you want to support
|
|---|
| 542 |
|
|---|
| 543 | **webpack.config.js**
|
|---|
| 544 |
|
|---|
| 545 | ```js
|
|---|
| 546 | module.exports = {
|
|---|
| 547 | module: {
|
|---|
| 548 | rules: [
|
|---|
| 549 | {
|
|---|
| 550 | test: /\.css$/i,
|
|---|
| 551 | use: [
|
|---|
| 552 | {
|
|---|
| 553 | loader: "style-loader",
|
|---|
| 554 | options: {
|
|---|
| 555 | insert: function insertAtTop(element) {
|
|---|
| 556 | var parent = document.querySelector("head");
|
|---|
| 557 | // eslint-disable-next-line no-underscore-dangle
|
|---|
| 558 | var lastInsertedElement =
|
|---|
| 559 | window._lastElementInsertedByStyleLoader;
|
|---|
| 560 |
|
|---|
| 561 | if (!lastInsertedElement) {
|
|---|
| 562 | parent.insertBefore(element, parent.firstChild);
|
|---|
| 563 | } else if (lastInsertedElement.nextSibling) {
|
|---|
| 564 | parent.insertBefore(element, lastInsertedElement.nextSibling);
|
|---|
| 565 | } else {
|
|---|
| 566 | parent.appendChild(element);
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | // eslint-disable-next-line no-underscore-dangle
|
|---|
| 570 | window._lastElementInsertedByStyleLoader = element;
|
|---|
| 571 | },
|
|---|
| 572 | },
|
|---|
| 573 | },
|
|---|
| 574 | "css-loader",
|
|---|
| 575 | ],
|
|---|
| 576 | },
|
|---|
| 577 | ],
|
|---|
| 578 | },
|
|---|
| 579 | };
|
|---|
| 580 | ```
|
|---|
| 581 |
|
|---|
| 582 | Insert styles at top of `head` tag.
|
|---|
| 583 |
|
|---|
| 584 | You can pass any parameters to `style.use(options)` and this value will be passed to `insert` and `styleTagTransform` functions.
|
|---|
| 585 |
|
|---|
| 586 | **webpack.config.js**
|
|---|
| 587 |
|
|---|
| 588 | ```js
|
|---|
| 589 | module.exports = {
|
|---|
| 590 | module: {
|
|---|
| 591 | rules: [
|
|---|
| 592 | {
|
|---|
| 593 | test: /\.css$/i,
|
|---|
| 594 | use: [
|
|---|
| 595 | {
|
|---|
| 596 | loader: "style-loader",
|
|---|
| 597 | options: {
|
|---|
| 598 | injectType: "lazyStyleTag",
|
|---|
| 599 | // Do not forget that this code will be used in the browser and
|
|---|
| 600 | // not all browsers support latest ECMA features like `let`, `const`, `arrow function expression` and etc,
|
|---|
| 601 | // we recommend use only ECMA 5 features,
|
|---|
| 602 | // but it is depends what browsers you want to support
|
|---|
| 603 | insert: function insertIntoTarget(element, options) {
|
|---|
| 604 | var parent = options.target || document.head;
|
|---|
| 605 |
|
|---|
| 606 | parent.appendChild(element);
|
|---|
| 607 | },
|
|---|
| 608 | },
|
|---|
| 609 | },
|
|---|
| 610 | "css-loader",
|
|---|
| 611 | ],
|
|---|
| 612 | },
|
|---|
| 613 | ],
|
|---|
| 614 | },
|
|---|
| 615 | };
|
|---|
| 616 | ```
|
|---|
| 617 |
|
|---|
| 618 | Insert styles to the provided element or to the `head` tag if target isn't provided. Now you can inject styles into Shadow DOM (or any other element).
|
|---|
| 619 |
|
|---|
| 620 | **custom-square.css**
|
|---|
| 621 |
|
|---|
| 622 | ```css
|
|---|
| 623 | div {
|
|---|
| 624 | width: 50px;
|
|---|
| 625 | height: 50px;
|
|---|
| 626 | background-color: red;
|
|---|
| 627 | }
|
|---|
| 628 | ```
|
|---|
| 629 |
|
|---|
| 630 | **custom-square.js**
|
|---|
| 631 |
|
|---|
| 632 | ```js
|
|---|
| 633 | import customSquareStyles from "./custom-square.css";
|
|---|
| 634 |
|
|---|
| 635 | class CustomSquare extends HTMLElement {
|
|---|
| 636 | constructor() {
|
|---|
| 637 | super();
|
|---|
| 638 |
|
|---|
| 639 | this.attachShadow({ mode: "open" });
|
|---|
| 640 |
|
|---|
| 641 | const divElement = document.createElement("div");
|
|---|
| 642 |
|
|---|
| 643 | divElement.textContent = "Text content.";
|
|---|
| 644 |
|
|---|
| 645 | this.shadowRoot.appendChild(divElement);
|
|---|
| 646 |
|
|---|
| 647 | customSquareStyles.use({ target: this.shadowRoot });
|
|---|
| 648 |
|
|---|
| 649 | // You can override injected styles
|
|---|
| 650 | const bgPurple = new CSSStyleSheet();
|
|---|
| 651 | const width = this.getAttribute("w");
|
|---|
| 652 | const height = this.getAttribute("h");
|
|---|
| 653 |
|
|---|
| 654 | bgPurple.replace(`div { width: ${width}px; height: ${height}px; }`);
|
|---|
| 655 |
|
|---|
| 656 | this.shadowRoot.adoptedStyleSheets = [bgPurple];
|
|---|
| 657 |
|
|---|
| 658 | // `divElement` will have `100px` width, `100px` height and `red` background color
|
|---|
| 659 | }
|
|---|
| 660 | }
|
|---|
| 661 |
|
|---|
| 662 | customElements.define("custom-square", CustomSquare);
|
|---|
| 663 |
|
|---|
| 664 | export default CustomSquare;
|
|---|
| 665 | ```
|
|---|
| 666 |
|
|---|
| 667 | ### `styleTagTransform`
|
|---|
| 668 |
|
|---|
| 669 | Type:
|
|---|
| 670 |
|
|---|
| 671 | ```ts
|
|---|
| 672 | type styleTagTransform =
|
|---|
| 673 | | string
|
|---|
| 674 | | ((
|
|---|
| 675 | css: string,
|
|---|
| 676 | styleElement: HTMLStyleElement,
|
|---|
| 677 | options: Record<string, any>
|
|---|
| 678 | ) => void);
|
|---|
| 679 | ```
|
|---|
| 680 |
|
|---|
| 681 | Default: `undefined`
|
|---|
| 682 |
|
|---|
| 683 | #### `string`
|
|---|
| 684 |
|
|---|
| 685 | Allows to setup absolute path to custom function that allows to override default behavior styleTagTransform.
|
|---|
| 686 |
|
|---|
| 687 | > **Warning**
|
|---|
| 688 | >
|
|---|
| 689 | > Do not forget that this code will be used in the browser and not all browsers support latest ECMA features like `let`, `const`, `arrow function expression` and etc, we recommend use only ECMA 5 features, but it is depends what browsers you want to support
|
|---|
| 690 |
|
|---|
| 691 | **webpack.config.js**
|
|---|
| 692 |
|
|---|
| 693 | ```js
|
|---|
| 694 | module.exports = {
|
|---|
| 695 | module: {
|
|---|
| 696 | rules: [
|
|---|
| 697 | {
|
|---|
| 698 | test: /\.css$/i,
|
|---|
| 699 | use: [
|
|---|
| 700 | {
|
|---|
| 701 | loader: "style-loader",
|
|---|
| 702 | options: {
|
|---|
| 703 | injectType: "styleTag",
|
|---|
| 704 | styleTagTransform: require.resolve("module-path"),
|
|---|
| 705 | },
|
|---|
| 706 | },
|
|---|
| 707 | "css-loader",
|
|---|
| 708 | ],
|
|---|
| 709 | },
|
|---|
| 710 | ],
|
|---|
| 711 | },
|
|---|
| 712 | };
|
|---|
| 713 | ```
|
|---|
| 714 |
|
|---|
| 715 | #### `function`
|
|---|
| 716 |
|
|---|
| 717 | Transform tag and css when insert 'style' tag into the DOM.
|
|---|
| 718 |
|
|---|
| 719 | > **Warning**
|
|---|
| 720 | >
|
|---|
| 721 | > Do not forget that this code will be used in the browser and not all browsers support latest ECMA features like `let`, `const`, `arrow function expression` and etc, we recommend use only ECMA 5 features, but it is depends what browsers you want to support
|
|---|
| 722 |
|
|---|
| 723 | > **Warning**
|
|---|
| 724 | >
|
|---|
| 725 | > Do not forget that some DOM methods may not be available in older browsers, we recommended use only [DOM core level 2 properties](https://caniuse.com/#search=DOM%20Core), but it is depends what browsers you want to support
|
|---|
| 726 |
|
|---|
| 727 | **webpack.config.js**
|
|---|
| 728 |
|
|---|
| 729 | ```js
|
|---|
| 730 | module.exports = {
|
|---|
| 731 | module: {
|
|---|
| 732 | rules: [
|
|---|
| 733 | {
|
|---|
| 734 | test: /\.css$/i,
|
|---|
| 735 | use: [
|
|---|
| 736 | {
|
|---|
| 737 | loader: "style-loader",
|
|---|
| 738 | options: {
|
|---|
| 739 | injectType: "styleTag",
|
|---|
| 740 | styleTagTransform: function (css, style) {
|
|---|
| 741 | // Do something ...
|
|---|
| 742 | style.innerHTML = `${css}.modify{}\n`;
|
|---|
| 743 |
|
|---|
| 744 | document.head.appendChild(style);
|
|---|
| 745 | },
|
|---|
| 746 | },
|
|---|
| 747 | },
|
|---|
| 748 | "css-loader",
|
|---|
| 749 | ],
|
|---|
| 750 | },
|
|---|
| 751 | ],
|
|---|
| 752 | },
|
|---|
| 753 | };
|
|---|
| 754 | ```
|
|---|
| 755 |
|
|---|
| 756 | ### `base`
|
|---|
| 757 |
|
|---|
| 758 | ```ts
|
|---|
| 759 | type base = number;
|
|---|
| 760 | ```
|
|---|
| 761 |
|
|---|
| 762 | This 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.me.uk/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.:
|
|---|
| 763 |
|
|---|
| 764 | **webpack.dll1.config.js**
|
|---|
| 765 |
|
|---|
| 766 | ```js
|
|---|
| 767 | module.exports = {
|
|---|
| 768 | module: {
|
|---|
| 769 | rules: [
|
|---|
| 770 | {
|
|---|
| 771 | test: /\.css$/i,
|
|---|
| 772 | use: ["style-loader", "css-loader"],
|
|---|
| 773 | },
|
|---|
| 774 | ],
|
|---|
| 775 | },
|
|---|
| 776 | };
|
|---|
| 777 | ```
|
|---|
| 778 |
|
|---|
| 779 | **webpack.dll2.config.js**
|
|---|
| 780 |
|
|---|
| 781 | ```js
|
|---|
| 782 | module.exports = {
|
|---|
| 783 | module: {
|
|---|
| 784 | rules: [
|
|---|
| 785 | {
|
|---|
| 786 | test: /\.css$/i,
|
|---|
| 787 | use: [
|
|---|
| 788 | { loader: "style-loader", options: { base: 1000 } },
|
|---|
| 789 | "css-loader",
|
|---|
| 790 | ],
|
|---|
| 791 | },
|
|---|
| 792 | ],
|
|---|
| 793 | },
|
|---|
| 794 | };
|
|---|
| 795 | ```
|
|---|
| 796 |
|
|---|
| 797 | **webpack.app.config.js**
|
|---|
| 798 |
|
|---|
| 799 | ```js
|
|---|
| 800 | module.exports = {
|
|---|
| 801 | module: {
|
|---|
| 802 | rules: [
|
|---|
| 803 | {
|
|---|
| 804 | test: /\.css$/i,
|
|---|
| 805 | use: [
|
|---|
| 806 | { loader: "style-loader", options: { base: 2000 } },
|
|---|
| 807 | "css-loader",
|
|---|
| 808 | ],
|
|---|
| 809 | },
|
|---|
| 810 | ],
|
|---|
| 811 | },
|
|---|
| 812 | };
|
|---|
| 813 | ```
|
|---|
| 814 |
|
|---|
| 815 | ### `esModule`
|
|---|
| 816 |
|
|---|
| 817 | Type:
|
|---|
| 818 |
|
|---|
| 819 | ```ts
|
|---|
| 820 | type esModule = boolean;
|
|---|
| 821 | ```
|
|---|
| 822 |
|
|---|
| 823 | Default: `true`
|
|---|
| 824 |
|
|---|
| 825 | By default, `style-loader` generates JS modules that use the ES modules syntax.
|
|---|
| 826 | There are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
|
|---|
| 827 |
|
|---|
| 828 | You can enable a CommonJS modules syntax using:
|
|---|
| 829 |
|
|---|
| 830 | **webpack.config.js**
|
|---|
| 831 |
|
|---|
| 832 | ```js
|
|---|
| 833 | module.exports = {
|
|---|
| 834 | module: {
|
|---|
| 835 | rules: [
|
|---|
| 836 | {
|
|---|
| 837 | test: /\.css$/i,
|
|---|
| 838 | loader: "style-loader",
|
|---|
| 839 | options: {
|
|---|
| 840 | esModule: false,
|
|---|
| 841 | },
|
|---|
| 842 | },
|
|---|
| 843 | ],
|
|---|
| 844 | },
|
|---|
| 845 | };
|
|---|
| 846 | ```
|
|---|
| 847 |
|
|---|
| 848 | ## Examples
|
|---|
| 849 |
|
|---|
| 850 | ### Recommend
|
|---|
| 851 |
|
|---|
| 852 | For `production` builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.
|
|---|
| 853 | This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin), because it creates separate css files.
|
|---|
| 854 | For `development` mode (including `webpack-dev-server`) you can use `style-loader`, because it injects CSS into the DOM using multiple `<style></style>` and works faster.
|
|---|
| 855 |
|
|---|
| 856 | > **Warning**
|
|---|
| 857 | >
|
|---|
| 858 | > Do not use together `style-loader` and `mini-css-extract-plugin`.
|
|---|
| 859 |
|
|---|
| 860 | **webpack.config.js**
|
|---|
| 861 |
|
|---|
| 862 | ```js
|
|---|
| 863 | const MiniCssExtractPlugin = require("mini-css-extract-plugin");
|
|---|
| 864 | const devMode = process.env.NODE_ENV !== "production";
|
|---|
| 865 |
|
|---|
| 866 | module.exports = {
|
|---|
| 867 | module: {
|
|---|
| 868 | rules: [
|
|---|
| 869 | {
|
|---|
| 870 | test: /\.(sa|sc|c)ss$/,
|
|---|
| 871 | use: [
|
|---|
| 872 | devMode ? "style-loader" : MiniCssExtractPlugin.loader,
|
|---|
| 873 | "css-loader",
|
|---|
| 874 | "postcss-loader",
|
|---|
| 875 | "sass-loader",
|
|---|
| 876 | ],
|
|---|
| 877 | },
|
|---|
| 878 | ],
|
|---|
| 879 | },
|
|---|
| 880 | plugins: [].concat(devMode ? [] : [new MiniCssExtractPlugin()]),
|
|---|
| 881 | };
|
|---|
| 882 | ```
|
|---|
| 883 |
|
|---|
| 884 | ### Named export for CSS Modules
|
|---|
| 885 |
|
|---|
| 886 | > **Warning**
|
|---|
| 887 | >
|
|---|
| 888 | > Names of locals are converted to `camelCase`.
|
|---|
| 889 |
|
|---|
| 890 | > **Warning**
|
|---|
| 891 | >
|
|---|
| 892 | > It is not allowed to use JavaScript reserved words in css class names.
|
|---|
| 893 |
|
|---|
| 894 | > **Warning**
|
|---|
| 895 | >
|
|---|
| 896 | > Options `esModule` and `modules.namedExport` in `css-loader` should be enabled.
|
|---|
| 897 |
|
|---|
| 898 | **styles.css**
|
|---|
| 899 |
|
|---|
| 900 | ```css
|
|---|
| 901 | .foo-baz {
|
|---|
| 902 | color: red;
|
|---|
| 903 | }
|
|---|
| 904 | .bar {
|
|---|
| 905 | color: blue;
|
|---|
| 906 | }
|
|---|
| 907 | ```
|
|---|
| 908 |
|
|---|
| 909 | **index.js**
|
|---|
| 910 |
|
|---|
| 911 | ```js
|
|---|
| 912 | import { fooBaz, bar } from "./styles.css";
|
|---|
| 913 |
|
|---|
| 914 | console.log(fooBaz, bar);
|
|---|
| 915 | ```
|
|---|
| 916 |
|
|---|
| 917 | You can enable a ES module named export using:
|
|---|
| 918 |
|
|---|
| 919 | **webpack.config.js**
|
|---|
| 920 |
|
|---|
| 921 | ```js
|
|---|
| 922 | module.exports = {
|
|---|
| 923 | module: {
|
|---|
| 924 | rules: [
|
|---|
| 925 | {
|
|---|
| 926 | test: /\.css$/,
|
|---|
| 927 | use: [
|
|---|
| 928 | {
|
|---|
| 929 | loader: "style-loader",
|
|---|
| 930 | },
|
|---|
| 931 | {
|
|---|
| 932 | loader: "css-loader",
|
|---|
| 933 | options: {
|
|---|
| 934 | modules: {
|
|---|
| 935 | namedExport: true,
|
|---|
| 936 | },
|
|---|
| 937 | },
|
|---|
| 938 | },
|
|---|
| 939 | ],
|
|---|
| 940 | },
|
|---|
| 941 | ],
|
|---|
| 942 | },
|
|---|
| 943 | };
|
|---|
| 944 | ```
|
|---|
| 945 |
|
|---|
| 946 | ### Source maps
|
|---|
| 947 |
|
|---|
| 948 | The loader automatically inject source maps when previous loader emit them.
|
|---|
| 949 | Therefore, to generate source maps, set the `sourceMap` option to `true` for the previous loader.
|
|---|
| 950 |
|
|---|
| 951 | **webpack.config.js**
|
|---|
| 952 |
|
|---|
| 953 | ```js
|
|---|
| 954 | module.exports = {
|
|---|
| 955 | module: {
|
|---|
| 956 | rules: [
|
|---|
| 957 | {
|
|---|
| 958 | test: /\.css$/i,
|
|---|
| 959 | use: [
|
|---|
| 960 | "style-loader",
|
|---|
| 961 | { loader: "css-loader", options: { sourceMap: true } },
|
|---|
| 962 | ],
|
|---|
| 963 | },
|
|---|
| 964 | ],
|
|---|
| 965 | },
|
|---|
| 966 | };
|
|---|
| 967 | ```
|
|---|
| 968 |
|
|---|
| 969 | ### Nonce
|
|---|
| 970 |
|
|---|
| 971 | If you are using a [Content Security Policy](https://www.w3.org/TR/CSP3/) (CSP), the injected code will usually be blocked. A workaround is to use a nonce. Note, however, that using a nonce significantly reduces the protection provided by the CSP. You can read more about the security impact in [the specification](https://www.w3.org/TR/CSP3/#security-considerations). The better solution is not to use this loader in production.
|
|---|
| 972 |
|
|---|
| 973 | There are two ways to work with `nonce`:
|
|---|
| 974 |
|
|---|
| 975 | - using the `attributes` option
|
|---|
| 976 | - using the `__webpack_nonce__` variable
|
|---|
| 977 |
|
|---|
| 978 | > **Warning**
|
|---|
| 979 | >
|
|---|
| 980 | > the `attributes` option takes precedence over the `__webpack_nonce__` variable
|
|---|
| 981 |
|
|---|
| 982 | #### `attributes`
|
|---|
| 983 |
|
|---|
| 984 | **component.js**
|
|---|
| 985 |
|
|---|
| 986 | ```js
|
|---|
| 987 | import "./style.css";
|
|---|
| 988 | ```
|
|---|
| 989 |
|
|---|
| 990 | **webpack.config.js**
|
|---|
| 991 |
|
|---|
| 992 | ```js
|
|---|
| 993 | module.exports = {
|
|---|
| 994 | module: {
|
|---|
| 995 | rules: [
|
|---|
| 996 | {
|
|---|
| 997 | test: /\.css$/i,
|
|---|
| 998 | use: [
|
|---|
| 999 | {
|
|---|
| 1000 | loader: "style-loader",
|
|---|
| 1001 | options: {
|
|---|
| 1002 | attributes: {
|
|---|
| 1003 | nonce: "12345678",
|
|---|
| 1004 | },
|
|---|
| 1005 | },
|
|---|
| 1006 | },
|
|---|
| 1007 | "css-loader",
|
|---|
| 1008 | ],
|
|---|
| 1009 | },
|
|---|
| 1010 | ],
|
|---|
| 1011 | },
|
|---|
| 1012 | };
|
|---|
| 1013 | ```
|
|---|
| 1014 |
|
|---|
| 1015 | The loader generate:
|
|---|
| 1016 |
|
|---|
| 1017 | ```html
|
|---|
| 1018 | <style nonce="12345678">
|
|---|
| 1019 | .foo {
|
|---|
| 1020 | color: red;
|
|---|
| 1021 | }
|
|---|
| 1022 | </style>
|
|---|
| 1023 | ```
|
|---|
| 1024 |
|
|---|
| 1025 | #### `__webpack_nonce__`
|
|---|
| 1026 |
|
|---|
| 1027 | **create-nonce.js**
|
|---|
| 1028 |
|
|---|
| 1029 | ```js
|
|---|
| 1030 | __webpack_nonce__ = "12345678";
|
|---|
| 1031 | ```
|
|---|
| 1032 |
|
|---|
| 1033 | **component.js**
|
|---|
| 1034 |
|
|---|
| 1035 | ```js
|
|---|
| 1036 | import "./create-nonce.js";
|
|---|
| 1037 | import "./style.css";
|
|---|
| 1038 | ```
|
|---|
| 1039 |
|
|---|
| 1040 | Alternative example for `require`:
|
|---|
| 1041 |
|
|---|
| 1042 | **component.js**
|
|---|
| 1043 |
|
|---|
| 1044 | ```js
|
|---|
| 1045 | __webpack_nonce__ = "12345678";
|
|---|
| 1046 |
|
|---|
| 1047 | require("./style.css");
|
|---|
| 1048 | ```
|
|---|
| 1049 |
|
|---|
| 1050 | **webpack.config.js**
|
|---|
| 1051 |
|
|---|
| 1052 | ```js
|
|---|
| 1053 | module.exports = {
|
|---|
| 1054 | module: {
|
|---|
| 1055 | rules: [
|
|---|
| 1056 | {
|
|---|
| 1057 | test: /\.css$/i,
|
|---|
| 1058 | use: ["style-loader", "css-loader"],
|
|---|
| 1059 | },
|
|---|
| 1060 | ],
|
|---|
| 1061 | },
|
|---|
| 1062 | };
|
|---|
| 1063 | ```
|
|---|
| 1064 |
|
|---|
| 1065 | The loader generate:
|
|---|
| 1066 |
|
|---|
| 1067 | ```html
|
|---|
| 1068 | <style nonce="12345678">
|
|---|
| 1069 | .foo {
|
|---|
| 1070 | color: red;
|
|---|
| 1071 | }
|
|---|
| 1072 | </style>
|
|---|
| 1073 | ```
|
|---|
| 1074 |
|
|---|
| 1075 | #### Insert styles at top
|
|---|
| 1076 |
|
|---|
| 1077 | Inserts styles at top of `head` tag.
|
|---|
| 1078 |
|
|---|
| 1079 | **webpack.config.js**
|
|---|
| 1080 |
|
|---|
| 1081 | ```js
|
|---|
| 1082 | module.exports = {
|
|---|
| 1083 | module: {
|
|---|
| 1084 | rules: [
|
|---|
| 1085 | {
|
|---|
| 1086 | test: /\.css$/i,
|
|---|
| 1087 | use: [
|
|---|
| 1088 | {
|
|---|
| 1089 | loader: "style-loader",
|
|---|
| 1090 | options: {
|
|---|
| 1091 | insert: function insertAtTop(element) {
|
|---|
| 1092 | var parent = document.querySelector("head");
|
|---|
| 1093 | var lastInsertedElement =
|
|---|
| 1094 | window._lastElementInsertedByStyleLoader;
|
|---|
| 1095 |
|
|---|
| 1096 | if (!lastInsertedElement) {
|
|---|
| 1097 | parent.insertBefore(element, parent.firstChild);
|
|---|
| 1098 | } else if (lastInsertedElement.nextSibling) {
|
|---|
| 1099 | parent.insertBefore(element, lastInsertedElement.nextSibling);
|
|---|
| 1100 | } else {
|
|---|
| 1101 | parent.appendChild(element);
|
|---|
| 1102 | }
|
|---|
| 1103 |
|
|---|
| 1104 | window._lastElementInsertedByStyleLoader = element;
|
|---|
| 1105 | },
|
|---|
| 1106 | },
|
|---|
| 1107 | },
|
|---|
| 1108 | "css-loader",
|
|---|
| 1109 | ],
|
|---|
| 1110 | },
|
|---|
| 1111 | ],
|
|---|
| 1112 | },
|
|---|
| 1113 | };
|
|---|
| 1114 | ```
|
|---|
| 1115 |
|
|---|
| 1116 | #### Insert styles before target element
|
|---|
| 1117 |
|
|---|
| 1118 | Inserts styles before `#id` element.
|
|---|
| 1119 |
|
|---|
| 1120 | **webpack.config.js**
|
|---|
| 1121 |
|
|---|
| 1122 | ```js
|
|---|
| 1123 | module.exports = {
|
|---|
| 1124 | module: {
|
|---|
| 1125 | rules: [
|
|---|
| 1126 | {
|
|---|
| 1127 | test: /\.css$/i,
|
|---|
| 1128 | use: [
|
|---|
| 1129 | {
|
|---|
| 1130 | loader: "style-loader",
|
|---|
| 1131 | options: {
|
|---|
| 1132 | insert: function insertBeforeAt(element) {
|
|---|
| 1133 | const parent = document.querySelector("head");
|
|---|
| 1134 | const target = document.querySelector("#id");
|
|---|
| 1135 |
|
|---|
| 1136 | const lastInsertedElement =
|
|---|
| 1137 | window._lastElementInsertedByStyleLoader;
|
|---|
| 1138 |
|
|---|
| 1139 | if (!lastInsertedElement) {
|
|---|
| 1140 | parent.insertBefore(element, target);
|
|---|
| 1141 | } else if (lastInsertedElement.nextSibling) {
|
|---|
| 1142 | parent.insertBefore(element, lastInsertedElement.nextSibling);
|
|---|
| 1143 | } else {
|
|---|
| 1144 | parent.appendChild(element);
|
|---|
| 1145 | }
|
|---|
| 1146 |
|
|---|
| 1147 | window._lastElementInsertedByStyleLoader = element;
|
|---|
| 1148 | },
|
|---|
| 1149 | },
|
|---|
| 1150 | },
|
|---|
| 1151 | "css-loader",
|
|---|
| 1152 | ],
|
|---|
| 1153 | },
|
|---|
| 1154 | ],
|
|---|
| 1155 | },
|
|---|
| 1156 | };
|
|---|
| 1157 | ```
|
|---|
| 1158 |
|
|---|
| 1159 | #### Custom Elements (Shadow DOM)
|
|---|
| 1160 |
|
|---|
| 1161 | You can define custom target for your styles for the `lazyStyleTag` type.
|
|---|
| 1162 |
|
|---|
| 1163 | **webpack.config.js**
|
|---|
| 1164 |
|
|---|
| 1165 | ```js
|
|---|
| 1166 | module.exports = {
|
|---|
| 1167 | module: {
|
|---|
| 1168 | rules: [
|
|---|
| 1169 | {
|
|---|
| 1170 | test: /\.css$/i,
|
|---|
| 1171 | use: [
|
|---|
| 1172 | {
|
|---|
| 1173 | loader: "style-loader",
|
|---|
| 1174 | options: {
|
|---|
| 1175 | injectType: "lazyStyleTag",
|
|---|
| 1176 | // Do not forget that this code will be used in the browser and
|
|---|
| 1177 | // not all browsers support latest ECMA features like `let`, `const`, `arrow function expression` and etc,
|
|---|
| 1178 | // we recommend use only ECMA 5 features,
|
|---|
| 1179 | // but it is depends what browsers you want to support
|
|---|
| 1180 | insert: function insertIntoTarget(element, options) {
|
|---|
| 1181 | var parent = options.target || document.head;
|
|---|
| 1182 |
|
|---|
| 1183 | parent.appendChild(element);
|
|---|
| 1184 | },
|
|---|
| 1185 | },
|
|---|
| 1186 | },
|
|---|
| 1187 | "css-loader",
|
|---|
| 1188 | ],
|
|---|
| 1189 | },
|
|---|
| 1190 | ],
|
|---|
| 1191 | },
|
|---|
| 1192 | };
|
|---|
| 1193 | ```
|
|---|
| 1194 |
|
|---|
| 1195 | Insert styles to the provided element or to the `head` tag if target isn't provided.
|
|---|
| 1196 |
|
|---|
| 1197 | **custom-square.css**
|
|---|
| 1198 |
|
|---|
| 1199 | ```css
|
|---|
| 1200 | div {
|
|---|
| 1201 | width: 50px;
|
|---|
| 1202 | height: 50px;
|
|---|
| 1203 | background-color: red;
|
|---|
| 1204 | }
|
|---|
| 1205 | ```
|
|---|
| 1206 |
|
|---|
| 1207 | **custom-square.js**
|
|---|
| 1208 |
|
|---|
| 1209 | ```js
|
|---|
| 1210 | import customSquareStyles from "./custom-square.css";
|
|---|
| 1211 |
|
|---|
| 1212 | class CustomSquare extends HTMLElement {
|
|---|
| 1213 | constructor() {
|
|---|
| 1214 | super();
|
|---|
| 1215 |
|
|---|
| 1216 | this.attachShadow({ mode: "open" });
|
|---|
| 1217 |
|
|---|
| 1218 | const divElement = document.createElement("div");
|
|---|
| 1219 |
|
|---|
| 1220 | divElement.textContent = "Text content.";
|
|---|
| 1221 |
|
|---|
| 1222 | this.shadowRoot.appendChild(divElement);
|
|---|
| 1223 |
|
|---|
| 1224 | customSquareStyles.use({ target: this.shadowRoot });
|
|---|
| 1225 |
|
|---|
| 1226 | // You can override injected styles
|
|---|
| 1227 | const bgPurple = new CSSStyleSheet();
|
|---|
| 1228 | const width = this.getAttribute("w");
|
|---|
| 1229 | const height = this.getAttribute("h");
|
|---|
| 1230 |
|
|---|
| 1231 | bgPurple.replace(`div { width: ${width}px; height: ${height}px; }`);
|
|---|
| 1232 |
|
|---|
| 1233 | this.shadowRoot.adoptedStyleSheets = [bgPurple];
|
|---|
| 1234 |
|
|---|
| 1235 | // `divElement` will have `100px` width, `100px` height and `red` background color
|
|---|
| 1236 | }
|
|---|
| 1237 | }
|
|---|
| 1238 |
|
|---|
| 1239 | customElements.define("custom-square", CustomSquare);
|
|---|
| 1240 |
|
|---|
| 1241 | export default CustomSquare;
|
|---|
| 1242 | ```
|
|---|
| 1243 |
|
|---|
| 1244 | ## Contributing
|
|---|
| 1245 |
|
|---|
| 1246 | Please take a moment to read our contributing guidelines if you haven't yet done so.
|
|---|
| 1247 |
|
|---|
| 1248 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
|
|---|
| 1249 |
|
|---|
| 1250 | ## License
|
|---|
| 1251 |
|
|---|
| 1252 | [MIT](./LICENSE)
|
|---|
| 1253 |
|
|---|
| 1254 | [npm]: https://img.shields.io/npm/v/style-loader.svg
|
|---|
| 1255 | [npm-url]: https://npmjs.com/package/style-loader
|
|---|
| 1256 | [node]: https://img.shields.io/node/v/style-loader.svg
|
|---|
| 1257 | [node-url]: https://nodejs.org
|
|---|
| 1258 | [tests]: https://github.com/webpack-contrib/style-loader/workflows/style-loader/badge.svg
|
|---|
| 1259 | [tests-url]: https://github.com/webpack-contrib/style-loader/actions
|
|---|
| 1260 | [cover]: https://codecov.io/gh/webpack-contrib/style-loader/branch/master/graph/badge.svg
|
|---|
| 1261 | [cover-url]: https://codecov.io/gh/webpack-contrib/style-loader
|
|---|
| 1262 | [discussion]: https://img.shields.io/github/discussions/webpack/webpack
|
|---|
| 1263 | [discussion-url]: https://github.com/webpack/webpack/discussions
|
|---|
| 1264 | [size]: https://packagephobia.now.sh/badge?p=style-loader
|
|---|
| 1265 | [size-url]: https://packagephobia.now.sh/result?p=style-loader
|
|---|