You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

466 lines
12 KiB

4 years ago
  1. [![npm][npm]][npm-url]
  2. [![node][node]][node-url]
  3. [![deps][deps]][deps-url]
  4. [![chat][chat]][chat-url]
  5. <div align="center">
  6. <a href="https://github.com/webpack/webpack">
  7. <img width="200" height="200"
  8. src="https://webpack.js.org/assets/icon-square-big.svg">
  9. </a>
  10. <h1>Style Loader</h1>
  11. <p>Adds CSS to the DOM by injecting a <code>&lt;style&gt;</code> tag</p>
  12. </div>
  13. <h2 align="center">Install</h2>
  14. ```bash
  15. npm install style-loader --save-dev
  16. ```
  17. <h2 align="center">Usage</h2>
  18. It's recommended to combine `style-loader` with the [`css-loader`](https://github.com/webpack/css-loader)
  19. **component.js**
  20. ```js
  21. import style from './file.css'
  22. ```
  23. **webpack.config.js**
  24. ```js
  25. {
  26. module: {
  27. rules: [
  28. {
  29. test: /\.css$/,
  30. use: [
  31. { loader: "style-loader" },
  32. { loader: "css-loader" }
  33. ]
  34. }
  35. ]
  36. }
  37. }
  38. ```
  39. #### `Locals (CSS Modules)`
  40. When using [local scoped CSS](https://github.com/webpack/css-loader#css-scope) the module exports the generated identifiers (locals).
  41. **component.js**
  42. ```js
  43. import style from './file.css'
  44. style.className === "z849f98ca812"
  45. ```
  46. ### `Url`
  47. It'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.
  48. ```js
  49. import url from 'file.css'
  50. ```
  51. **webpack.config.js**
  52. ```js
  53. {
  54. module: {
  55. rules: [
  56. {
  57. test: /\.css$/,
  58. use: [
  59. { loader: "style-loader/url" },
  60. { loader: "file-loader" }
  61. ]
  62. }
  63. ]
  64. }
  65. }
  66. ```
  67. ```html
  68. <link rel="stylesheet" href="path/to/file.css">
  69. ```
  70. > ℹ️ 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.
  71. ### `Useable`
  72. The `style-loader` injects the styles lazily making them useable on-demand via `style.use()` / `style.unuse()`
  73. By 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`).
  74. **webpack.config.js**
  75. ```js
  76. {
  77. module: {
  78. rules: [
  79. {
  80. test: /\.css$/,
  81. use: [
  82. { loader: "style-loader" },
  83. { loader: "css-loader" },
  84. ],
  85. },
  86. {
  87. test: /\.useable\.css$/,
  88. use: [
  89. {
  90. loader: "style-loader/useable"
  91. },
  92. { loader: "css-loader" },
  93. ],
  94. },
  95. ],
  96. },
  97. }
  98. ```
  99. #### `Reference Counter API`
  100. **component.js**
  101. ```js
  102. import style from './file.css'
  103. style.use(); // = style.ref();
  104. style.unuse(); // = style.unref();
  105. ```
  106. Styles 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`.
  107. > ⚠️ Behavior is undefined when `unuse`/`unref` is called more often than `use`/`ref`. Don't do that.
  108. <h2 align="center">Options</h2>
  109. |Name|Type|Default|Description|
  110. |:--:|:--:|:-----:|:----------|
  111. |**`hmr`**|`{Boolean}`|`true`|Enable/disable Hot Module Replacement (HMR), if disabled no HMR Code will be added (good for non local development/production)|
  112. |**`base`** |`{Number}`|`true`|Set module ID base (DLLPlugin)|
  113. |**`attrs`**|`{Object}`|`{}`|Add custom attrs to `<style></style>`|
  114. |**`transform`** |`{Function}`|`false`|Transform/Conditionally load CSS by passing a transform/condition function|
  115. |**`insertAt`**|`{String\|Object}`|`bottom`|Inserts `<style></style>` at the given position|
  116. |**`insertInto`**|`{String\|Function}`|`<head>`|Inserts `<style></style>` into the given position|
  117. |**`singleton`**|`{Boolean}`|`undefined`|Reuses a single `<style></style>` element, instead of adding/removing individual elements for each required module.|
  118. |**`sourceMap`**|`{Boolean}`|`false`|Enable/Disable Sourcemaps|
  119. |**`convertToAbsoluteUrls`**|`{Boolean}`|`false`|Converts relative URLs to absolute urls, when source maps are enabled|
  120. ### `hmr`
  121. Enable/disable Hot Module Replacement (HMR), if disabled no HMR Code will be added.
  122. This could be used for non local development and production.
  123. **webpack.config.js**
  124. ```js
  125. {
  126. loader: 'style-loader',
  127. options: {
  128. hmr: false
  129. }
  130. }
  131. ```
  132. ### `base`
  133. 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.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.:
  134. **webpack.dll1.config.js**
  135. ```js
  136. {
  137. test: /\.css$/,
  138. use: [
  139. 'style-loader',
  140. 'css-loader'
  141. ]
  142. }
  143. ```
  144. **webpack.dll2.config.js**
  145. ```js
  146. {
  147. test: /\.css$/,
  148. use: [
  149. { loader: 'style-loader', options: { base: 1000 } },
  150. 'css-loader'
  151. ]
  152. }
  153. ```
  154. **webpack.app.config.js**
  155. ```
  156. {
  157. test: /\.css$/,
  158. use: [
  159. { loader: 'style-loader', options: { base: 2000 } },
  160. 'css-loader'
  161. ]
  162. }
  163. ```
  164. ### `attrs`
  165. If defined, style-loader will attach given attributes with their values on `<style>` / `<link>` element.
  166. **component.js**
  167. ```js
  168. import style from './file.css'
  169. ```
  170. **webpack.config.js**
  171. ```js
  172. {
  173. test: /\.css$/,
  174. use: [
  175. { loader: 'style-loader', options: { attrs: { id: 'id' } } }
  176. { loader: 'css-loader' }
  177. ]
  178. }
  179. ```
  180. ```html
  181. <style id="id"></style>
  182. ```
  183. #### `Url`
  184. **component.js**
  185. ```js
  186. import link from './file.css'
  187. ```
  188. **webpack.config.js**
  189. ```js
  190. {
  191. test: /\.css$/,
  192. use: [
  193. { loader: 'style-loader/url', options: { attrs: { id: 'id' } } }
  194. { loader: 'file-loader' }
  195. ]
  196. }
  197. ```
  198. ### `transform`
  199. A `transform` is a function that can modify the css just before it is loaded into the page by the style-loader.
  200. This 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.
  201. If the return value of the `transform` function is falsy, the css will not be loaded into the page at all.
  202. > ⚠️ In case you are using ES Module syntax in `tranform.js` then, you **need to transpile** it or otherwise it will throw an `{Error}`.
  203. **webpack.config.js**
  204. ```js
  205. {
  206. loader: 'style-loader',
  207. options: {
  208. transform: 'path/to/transform.js'
  209. }
  210. }
  211. ```
  212. **transform.js**
  213. ```js
  214. module.exports = function (css) {
  215. // Here we can change the original css
  216. const transformed = css.replace('.classNameA', '.classNameB')
  217. return transformed
  218. }
  219. ```
  220. #### `Conditional`
  221. **webpack.config.js**
  222. ```js
  223. {
  224. loader: 'style-loader',
  225. options: {
  226. transform: 'path/to/conditional.js'
  227. }
  228. }
  229. ```
  230. **conditional.js**
  231. ```js
  232. module.exports = function (css) {
  233. // If the condition is matched load [and transform] the CSS
  234. if (css.includes('something I want to check')) {
  235. return css;
  236. }
  237. // If a falsy value is returned, the CSS won't be loaded
  238. return false
  239. }
  240. ```
  241. ### `insertAt`
  242. By 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
  243. **webpack.config.js**
  244. ```js
  245. {
  246. loader: 'style-loader',
  247. options: {
  248. insertAt: 'top'
  249. }
  250. }
  251. ```
  252. A new `<style>` element can be inserted before a specific element by passing an object, e.g.
  253. **webpack.config.js**
  254. ```js
  255. {
  256. loader: 'style-loader',
  257. options: {
  258. insertAt: {
  259. before: '#id'
  260. }
  261. }
  262. }
  263. ```
  264. ### `insertInto`
  265. By 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.
  266. You can also pass function to override default behavior and insert styles in your container, e.g
  267. **webpack.config.js**
  268. ```js
  269. {
  270. loader: 'style-loader',
  271. options: {
  272. insertInto: () => document.querySelector("#root"),
  273. }
  274. }
  275. ```
  276. Using function you can insert the styles into a [ShadowRoot](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot), e.g
  277. **webpack.config.js**
  278. ```js
  279. {
  280. loader: 'style-loader',
  281. options: {
  282. insertInto: () => document.querySelector("#root").shadowRoot,
  283. }
  284. }
  285. ```
  286. ### `singleton`
  287. If defined, the style-loader will reuse a single `<style></style>` element, instead of adding/removing individual elements for each required module.
  288. > ℹ️ 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.
  289. **webpack.config.js**
  290. ```js
  291. {
  292. loader: 'style-loader',
  293. options: {
  294. singleton: true
  295. }
  296. }
  297. ```
  298. ### `sourceMap`
  299. Enable/Disable source map loading
  300. **webpack.config.js**
  301. ```js
  302. {
  303. loader: 'style-loader',
  304. options: {
  305. sourceMap: true
  306. }
  307. }
  308. ```
  309. ### `convertToAbsoluteUrls`
  310. 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. You can enable it with the convertToAbsoluteUrls option.
  311. **webpack.config.js**
  312. ```js
  313. {
  314. loader: 'style-loader',
  315. options: {
  316. sourceMap: true,
  317. convertToAbsoluteUrls: true
  318. }
  319. }
  320. ```
  321. <h2 align="center">Maintainers</h2>
  322. <table>
  323. <tbody>
  324. <tr>
  325. <td align="center">
  326. <a href="https://github.com/bebraw">
  327. <img width="150" height="150" src="https://github.com/bebraw.png?v=3&s=150">
  328. </br>
  329. Juho Vepsäläinen
  330. </a>
  331. </td>
  332. <td align="center">
  333. <a href="https://github.com/d3viant0ne">
  334. <img width="150" height="150" src="https://github.com/d3viant0ne.png?v=3&s=150">
  335. </br>
  336. Joshua Wiens
  337. </a>
  338. </td>
  339. <td align="center">
  340. <a href="https://github.com/sapegin">
  341. <img width="150" height="150" src="https://github.com/sapegin.png?v=3&s=150">
  342. </br>
  343. Artem Sapegin
  344. </a>
  345. </td>
  346. <td align="center">
  347. <a href="https://github.com/michael-ciniawsky">
  348. <img width="150" height="150" src="https://github.com/michael-ciniawsky.png?v=3&s=150">
  349. </br>
  350. Michael Ciniawsky
  351. </a>
  352. </td>
  353. <td align="center">
  354. <a href="https://github.com/evilebottnawi">
  355. <img width="150" height="150" src="https://github.com/evilebottnawi.png?v=3&s=150">
  356. </br>
  357. Alexander Krasnoyarov
  358. </a>
  359. </td>
  360. </tr>
  361. <tr>
  362. <td align="center">
  363. <a href="https://github.com/sokra">
  364. <img width="150" height="150" src="https://github.com/sokra.png?v=3&s=150">
  365. </br>
  366. Tobias Koppers
  367. </a>
  368. </td>
  369. <td align="center">
  370. <a href="https://github.com/SpaceK33z">
  371. <img width="150" height="150" src="https://github.com/SpaceK33z.png?v=3&s=150">
  372. </br>
  373. Kees Kluskens
  374. </a>
  375. </td>
  376. <tr>
  377. <tbody>
  378. </table>
  379. [npm]: https://img.shields.io/npm/v/style-loader.svg
  380. [npm-url]: https://npmjs.com/package/style-loader
  381. [node]: https://img.shields.io/node/v/style-loader.svg
  382. [node-url]: https://nodejs.org
  383. [deps]: https://david-dm.org/webpack/style-loader.svg
  384. [deps-url]: https://david-dm.org/webpack/file-loader
  385. [chat]: https://badges.gitter.im/webpack/webpack.svg
  386. [chat-url]: https://gitter.im/webpack/webpack