2b8310faa282408385ff78cbd32654d28622fbf7f5078eeafd80c0881182b04930cd8bb5c1d2f2a11052ab83b8dfe105e77fa33b1d95255ab412d3694af1ff 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. <div align="center">
  2. <img width="180" height="180" vspace="20"
  3. src="https://cdn.worldvectorlogo.com/logos/css-3.svg">
  4. <a href="https://github.com/webpack/webpack">
  5. <img width="200" height="200"
  6. src="https://webpack.js.org/assets/icon-square-big.svg">
  7. </a>
  8. </div>
  9. [![npm][npm]][npm-url]
  10. [![node][node]][node-url]
  11. [![deps][deps]][deps-url]
  12. [![tests][tests]][tests-url]
  13. [![coverage][cover]][cover-url]
  14. [![chat][chat]][chat-url]
  15. [![size][size]][size-url]
  16. # css-loader
  17. The `css-loader` interprets `@import` and `url()` like `import/require()` and will resolve them.
  18. ## Getting Started
  19. To begin, you'll need to install `css-loader`:
  20. ```console
  21. npm install --save-dev css-loader
  22. ```
  23. Then add the plugin to your `webpack` config. For example:
  24. **file.js**
  25. ```js
  26. import css from 'file.css';
  27. ```
  28. **webpack.config.js**
  29. ```js
  30. module.exports = {
  31. module: {
  32. rules: [
  33. {
  34. test: /\.css$/i,
  35. use: ['style-loader', 'css-loader'],
  36. },
  37. ],
  38. },
  39. };
  40. ```
  41. Good loaders for requiring your assets are the [file-loader](https://github.com/webpack/file-loader) and the [url-loader](https://github.com/webpack/url-loader) which you should specify in your config (see [below](https://github.com/webpack-contrib/css-loader#assets)).
  42. And run `webpack` via your preferred method.
  43. ### `toString`
  44. You can also use the css-loader results directly as a string, such as in Angular's component style.
  45. **webpack.config.js**
  46. ```js
  47. module.exports = {
  48. module: {
  49. rules: [
  50. {
  51. test: /\.css$/i,
  52. use: ['to-string-loader', 'css-loader'],
  53. },
  54. ],
  55. },
  56. };
  57. ```
  58. or
  59. ```js
  60. const css = require('./test.css').toString();
  61. console.log(css); // {String}
  62. ```
  63. If there are SourceMaps, they will also be included in the result string.
  64. If, for one reason or another, you need to extract CSS as a
  65. plain string resource (i.e. not wrapped in a JS module) you
  66. might want to check out the [extract-loader](https://github.com/peerigon/extract-loader).
  67. It's useful when you, for instance, need to post process the CSS as a string.
  68. **webpack.config.js**
  69. ```js
  70. module.exports = {
  71. module: {
  72. rules: [
  73. {
  74. test: /\.css$/i,
  75. use: [
  76. 'handlebars-loader', // handlebars loader expects raw resource string
  77. 'extract-loader',
  78. 'css-loader',
  79. ],
  80. },
  81. ],
  82. },
  83. };
  84. ```
  85. ## Options
  86. | Name | Type | Default | Description |
  87. | :-----------------------------------------: | :-------------------------: | :------: | :--------------------------------------------------------------------- |
  88. | **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enables/Disables `url`/`image-set` functions handling |
  89. | **[`import`](#import)** | `{Boolean\|Function}` | `true` | Enables/Disables `@import` at-rules handling |
  90. | **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `false` | Enables/Disables CSS Modules and their configuration |
  91. | **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enables/Disables generation of source maps |
  92. | **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Enables/Disables or setups number of loaders applied before CSS loader |
  93. | **[`localsConvention`](#localsconvention)** | `{String}` | `'asIs'` | Style of exported classnames |
  94. | **[`onlyLocals`](#onlylocals)** | `{Boolean}` | `false` | Export only locals |
  95. | **[`esModule`](#esmodule)** | `{Boolean}` | `false` | Use ES modules syntax |
  96. ### `url`
  97. Type: `Boolean|Function`
  98. Default: `true`
  99. Enables/Disables `url`/`image-set` functions handling.
  100. Control `url()` resolving. Absolute URLs and root-relative URLs are not resolving.
  101. Examples resolutions:
  102. ```
  103. url(image.png) => require('./image.png')
  104. url('image.png') => require('./image.png')
  105. url(./image.png) => require('./image.png')
  106. url('./image.png') => require('./image.png')
  107. url('http://dontwritehorriblecode.com/2112.png') => require('http://dontwritehorriblecode.com/2112.png')
  108. image-set(url('image2x.png') 1x, url('image1x.png') 2x) => require('./image1x.png') and require('./image2x.png')
  109. ```
  110. To import assets from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:
  111. ```
  112. url(~module/image.png) => require('module/image.png')
  113. url('~module/image.png') => require('module/image.png')
  114. url(~aliasDirectory/image.png) => require('otherDirectory/image.png')
  115. ```
  116. #### `Boolean`
  117. Enable/disable `url()` resolving.
  118. **webpack.config.js**
  119. ```js
  120. module.exports = {
  121. module: {
  122. rules: [
  123. {
  124. test: /\.css$/i,
  125. loader: 'css-loader',
  126. options: {
  127. url: true,
  128. },
  129. },
  130. ],
  131. },
  132. };
  133. ```
  134. #### `Function`
  135. Allow to filter `url()`. All filtered `url()` will not be resolved (left in the code as they were written).
  136. **webpack.config.js**
  137. ```js
  138. module.exports = {
  139. module: {
  140. rules: [
  141. {
  142. test: /\.css$/i,
  143. loader: 'css-loader',
  144. options: {
  145. url: (url, resourcePath) => {
  146. // resourcePath - path to css file
  147. // Don't handle `img.png` urls
  148. if (url.includes('img.png')) {
  149. return false;
  150. }
  151. return true;
  152. },
  153. },
  154. },
  155. ],
  156. },
  157. };
  158. ```
  159. ### `import`
  160. Type: `Boolean|Function`
  161. Default: `true`
  162. Enables/Disables `@import` at-rules handling.
  163. Control `@import` resolving. Absolute urls in `@import` will be moved in runtime code.
  164. Examples resolutions:
  165. ```
  166. @import 'style.css' => require('./style.css')
  167. @import url(style.css) => require('./style.css')
  168. @import url('style.css') => require('./style.css')
  169. @import './style.css' => require('./style.css')
  170. @import url(./style.css) => require('./style.css')
  171. @import url('./style.css') => require('./style.css')
  172. @import url('http://dontwritehorriblecode.com/style.css') => @import url('http://dontwritehorriblecode.com/style.css') in runtime
  173. ```
  174. To import styles from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:
  175. ```
  176. @import url(~module/style.css) => require('module/style.css')
  177. @import url('~module/style.css') => require('module/style.css')
  178. @import url(~aliasDirectory/style.css) => require('otherDirectory/style.css')
  179. ```
  180. #### `Boolean`
  181. Enable/disable `@import` resolving.
  182. **webpack.config.js**
  183. ```js
  184. module.exports = {
  185. module: {
  186. rules: [
  187. {
  188. test: /\.css$/i,
  189. loader: 'css-loader',
  190. options: {
  191. import: true,
  192. },
  193. },
  194. ],
  195. },
  196. };
  197. ```
  198. #### `Function`
  199. Allow to filter `@import`. All filtered `@import` will not be resolved (left in the code as they were written).
  200. **webpack.config.js**
  201. ```js
  202. module.exports = {
  203. module: {
  204. rules: [
  205. {
  206. test: /\.css$/i,
  207. loader: 'css-loader',
  208. options: {
  209. import: (parsedImport, resourcePath) => {
  210. // parsedImport.url - url of `@import`
  211. // parsedImport.media - media query of `@import`
  212. // resourcePath - path to css file
  213. // Don't handle `style.css` import
  214. if (parsedImport.url.includes('style.css')) {
  215. return false;
  216. }
  217. return true;
  218. },
  219. },
  220. },
  221. ],
  222. },
  223. };
  224. ```
  225. ### `modules`
  226. Type: `Boolean|String|Object`
  227. Default: `false`
  228. Enables/Disables CSS Modules and their configuration.
  229. The `modules` option enables/disables the **[CSS Modules](https://github.com/css-modules/css-modules)** specification and setup basic behaviour.
  230. Using `false` value increase performance because we avoid parsing **CSS Modules** features, it will be useful for developers who use vanilla css or use other technologies.
  231. **webpack.config.js**
  232. ```js
  233. module.exports = {
  234. module: {
  235. rules: [
  236. {
  237. test: /\.css$/i,
  238. loader: 'css-loader',
  239. options: {
  240. modules: true,
  241. },
  242. },
  243. ],
  244. },
  245. };
  246. ```
  247. #### `Features`
  248. ##### `Scope`
  249. Using `local` value requires you to specify `:global` classes.
  250. Using `global` value requires you to specify `:local` classes.
  251. Using `pure` value requires selectors must contain at least one local class or id.
  252. You can find more information [here](https://github.com/css-modules/css-modules).
  253. Styles can be locally scoped to avoid globally scoping styles.
  254. The syntax `:local(.className)` can be used to declare `className` in the local scope. The local identifiers are exported by the module.
  255. With `:local` (without brackets) local mode can be switched on for this selector.
  256. The `:global(.className)` notation can be used to declare an explicit global selector.
  257. With `:global` (without brackets) global mode can be switched on for this selector.
  258. The loader replaces local selectors with unique identifiers. The chosen unique identifiers are exported by the module.
  259. ```css
  260. :local(.className) {
  261. background: red;
  262. }
  263. :local .className {
  264. color: green;
  265. }
  266. :local(.className .subClass) {
  267. color: green;
  268. }
  269. :local .className .subClass :global(.global-class-name) {
  270. color: blue;
  271. }
  272. ```
  273. ```css
  274. ._23_aKvs-b8bW2Vg3fwHozO {
  275. background: red;
  276. }
  277. ._23_aKvs-b8bW2Vg3fwHozO {
  278. color: green;
  279. }
  280. ._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 {
  281. color: green;
  282. }
  283. ._23_aKvs-b8bW2Vg3fwHozO ._13LGdX8RMStbBE9w-t0gZ1 .global-class-name {
  284. color: blue;
  285. }
  286. ```
  287. > ℹ️ Identifiers are exported
  288. ```js
  289. exports.locals = {
  290. className: '_23_aKvs-b8bW2Vg3fwHozO',
  291. subClass: '_13LGdX8RMStbBE9w-t0gZ1',
  292. };
  293. ```
  294. CamelCase is recommended for local selectors. They are easier to use within the imported JS module.
  295. You can use `:local(#someId)`, but this is not recommended. Use classes instead of ids.
  296. ##### `Composing`
  297. When declaring a local classname you can compose a local class from another local classname.
  298. ```css
  299. :local(.className) {
  300. background: red;
  301. color: yellow;
  302. }
  303. :local(.subClass) {
  304. composes: className;
  305. background: blue;
  306. }
  307. ```
  308. This doesn't result in any change to the CSS itself but exports multiple classnames.
  309. ```js
  310. exports.locals = {
  311. className: '_23_aKvs-b8bW2Vg3fwHozO',
  312. subClass: '_13LGdX8RMStbBE9w-t0gZ1 _23_aKvs-b8bW2Vg3fwHozO',
  313. };
  314. ```
  315. ```css
  316. ._23_aKvs-b8bW2Vg3fwHozO {
  317. background: red;
  318. color: yellow;
  319. }
  320. ._13LGdX8RMStbBE9w-t0gZ1 {
  321. background: blue;
  322. }
  323. ```
  324. ##### `Importing`
  325. To import a local classname from another module.
  326. ```css
  327. :local(.continueButton) {
  328. composes: button from 'library/button.css';
  329. background: red;
  330. }
  331. ```
  332. ```css
  333. :local(.nameEdit) {
  334. composes: edit highlight from './edit.css';
  335. background: red;
  336. }
  337. ```
  338. To import from multiple modules use multiple `composes:` rules.
  339. ```css
  340. :local(.className) {
  341. composes: edit hightlight from './edit.css';
  342. composes: button from 'module/button.css';
  343. composes: classFromThisModule;
  344. background: red;
  345. }
  346. ```
  347. ##### `Values`
  348. You can use `@value` to specific values to be reused throughout a document.
  349. We recommend use prefix `v-` for values, `s-` for selectors and `m-` for media at-rules.
  350. ```css
  351. @value v-primary: #BF4040;
  352. @value s-black: black-selector;
  353. @value m-large: (min-width: 960px);
  354. .header {
  355. color: v-primary;
  356. padding: 0 10px;
  357. }
  358. .s-black {
  359. color: black;
  360. }
  361. @media m-large {
  362. .header {
  363. padding: 0 20px;
  364. }
  365. }
  366. ```
  367. #### `Boolean`
  368. Enable **CSS Modules** features.
  369. **webpack.config.js**
  370. ```js
  371. module.exports = {
  372. module: {
  373. rules: [
  374. {
  375. test: /\.css$/i,
  376. loader: 'css-loader',
  377. options: {
  378. modules: true,
  379. },
  380. },
  381. ],
  382. },
  383. };
  384. ```
  385. #### `String`
  386. Enable **CSS Modules** features and setup `mode`.
  387. **webpack.config.js**
  388. ```js
  389. module.exports = {
  390. module: {
  391. rules: [
  392. {
  393. test: /\.css$/i,
  394. loader: 'css-loader',
  395. options: {
  396. // Using `local` value has same effect like using `modules: true`
  397. modules: 'global',
  398. },
  399. },
  400. ],
  401. },
  402. };
  403. ```
  404. #### `Object`
  405. Enable **CSS Modules** features and setup options for them.
  406. **webpack.config.js**
  407. ```js
  408. module.exports = {
  409. module: {
  410. rules: [
  411. {
  412. test: /\.css$/i,
  413. loader: 'css-loader',
  414. options: {
  415. modules: {
  416. mode: 'local',
  417. exportGlobals: true,
  418. localIdentName: '[path][name]__[local]--[hash:base64:5]',
  419. context: path.resolve(__dirname, 'src'),
  420. hashPrefix: 'my-custom-hash',
  421. },
  422. },
  423. },
  424. ],
  425. },
  426. };
  427. ```
  428. ##### `auto`
  429. Type: `Boolean|RegExp|Function`
  430. Default: `'undefined'`
  431. Allows auto enable css modules based on filename.
  432. ###### `Boolean`
  433. Possible values:
  434. - `true` - enable css modules for all files for which `/\.module\.\w+$/i.test(filename)` return true
  435. - `false` - disable css modules
  436. **webpack.config.js**
  437. ```js
  438. module.exports = {
  439. module: {
  440. rules: [
  441. {
  442. test: /\.css$/i,
  443. loader: 'css-loader',
  444. options: {
  445. modules: {
  446. auto: true,
  447. },
  448. },
  449. },
  450. ],
  451. },
  452. };
  453. ```
  454. ###### `RegExp`
  455. Enable css modules for files based on the filename satisfying your regex check.
  456. **webpack.config.js**
  457. ```js
  458. module.exports = {
  459. module: {
  460. rules: [
  461. {
  462. test: /\.css$/i,
  463. loader: 'css-loader',
  464. options: {
  465. modules: {
  466. auto: /\.custom-module\.\w+$/i,
  467. },
  468. },
  469. },
  470. ],
  471. },
  472. };
  473. ```
  474. ###### `Function`
  475. Enable css modules for files based on the filename satisfying your filter function check.
  476. **webpack.config.js**
  477. ```js
  478. module.exports = {
  479. module: {
  480. rules: [
  481. {
  482. test: /\.css$/i,
  483. loader: 'css-loader',
  484. options: {
  485. modules: {
  486. auto: (resourcePath) => resourcePath.endsWith('.custom-module.css'),
  487. },
  488. },
  489. },
  490. ],
  491. },
  492. };
  493. ```
  494. ##### `mode`
  495. Type: `String|Function`
  496. Default: `'local'`
  497. Setup `mode` option. You can omit the value when you want `local` mode.
  498. ###### `String`
  499. Possible values - `local`, `global`, and `pure`.
  500. **webpack.config.js**
  501. ```js
  502. module.exports = {
  503. module: {
  504. rules: [
  505. {
  506. test: /\.css$/i,
  507. loader: 'css-loader',
  508. options: {
  509. modules: {
  510. mode: 'global',
  511. },
  512. },
  513. },
  514. ],
  515. },
  516. };
  517. ```
  518. ###### `Function`
  519. Allows set different values for the `mode` option based on a filename
  520. Possible return values - `local`, `global`, and `pure`.
  521. **webpack.config.js**
  522. ```js
  523. module.exports = {
  524. module: {
  525. rules: [
  526. {
  527. test: /\.css$/i,
  528. loader: 'css-loader',
  529. options: {
  530. modules: {
  531. // Callback must return "local", "global", or "pure" values
  532. mode: (resourcePath) => {
  533. if (/pure.css$/i.test(resourcePath)) {
  534. return 'pure';
  535. }
  536. if (/global.css$/i.test(resourcePath)) {
  537. return 'global';
  538. }
  539. return 'local';
  540. },
  541. },
  542. },
  543. },
  544. ],
  545. },
  546. };
  547. ```
  548. ##### `exportGlobals`
  549. Type: `Boolean`
  550. Default: `false`
  551. Allow `css-loader` to export names from global class or id, so you can use that as local name.
  552. **webpack.config.js**
  553. ```js
  554. module.exports = {
  555. module: {
  556. rules: [
  557. {
  558. test: /\.css$/i,
  559. loader: 'css-loader',
  560. options: {
  561. modules: {
  562. exportGlobals: true,
  563. },
  564. },
  565. },
  566. ],
  567. },
  568. };
  569. ```
  570. ##### `localIdentName`
  571. Type: `String`
  572. Default: `'[hash:base64]'`
  573. You can configure the generated ident with the `localIdentName` query parameter.
  574. See [loader-utils's documentation](https://github.com/webpack/loader-utils#interpolatename) for more information on options.
  575. Recommendations:
  576. - use `'[path][name]__[local]'` for development
  577. - use `'[hash:base64]'` for production
  578. The `[local]` placeholder contains original class.
  579. **Note:** all reserved (`<>:"/\|?*`) and control filesystem characters (excluding characters in the `[local]` placeholder) will be converted to `-`.
  580. **webpack.config.js**
  581. ```js
  582. module.exports = {
  583. module: {
  584. rules: [
  585. {
  586. test: /\.css$/i,
  587. loader: 'css-loader',
  588. options: {
  589. modules: {
  590. localIdentName: '[path][name]__[local]--[hash:base64:5]',
  591. },
  592. },
  593. },
  594. ],
  595. },
  596. };
  597. ```
  598. ##### `context`
  599. Type: `String`
  600. Default: `undefined`
  601. Allow to redefine basic loader context for local ident name.
  602. By default we use `rootContext` of loader.
  603. **webpack.config.js**
  604. ```js
  605. module.exports = {
  606. module: {
  607. rules: [
  608. {
  609. test: /\.css$/i,
  610. loader: 'css-loader',
  611. options: {
  612. modules: {
  613. context: path.resolve(__dirname, 'context'),
  614. },
  615. },
  616. },
  617. ],
  618. },
  619. };
  620. ```
  621. ##### `hashPrefix`
  622. Type: `String`
  623. Default: `undefined`
  624. Allow to add custom hash to generate more unique classes.
  625. **webpack.config.js**
  626. ```js
  627. module.exports = {
  628. module: {
  629. rules: [
  630. {
  631. test: /\.css$/i,
  632. loader: 'css-loader',
  633. options: {
  634. modules: {
  635. hashPrefix: 'hash',
  636. },
  637. },
  638. },
  639. ],
  640. },
  641. };
  642. ```
  643. ##### `getLocalIdent`
  644. Type: `Function`
  645. Default: `undefined`
  646. You can also specify the absolute path to your custom `getLocalIdent` function to generate classname based on a different schema.
  647. By default we use built-in function to generate a classname.
  648. **webpack.config.js**
  649. ```js
  650. module.exports = {
  651. module: {
  652. rules: [
  653. {
  654. test: /\.css$/i,
  655. loader: 'css-loader',
  656. options: {
  657. modules: {
  658. getLocalIdent: (context, localIdentName, localName, options) => {
  659. return 'whatever_random_class_name';
  660. },
  661. },
  662. },
  663. },
  664. ],
  665. },
  666. };
  667. ```
  668. ##### `localIdentRegExp`
  669. Type: `String|RegExp`
  670. Default: `undefined`
  671. **webpack.config.js**
  672. ```js
  673. module.exports = {
  674. module: {
  675. rules: [
  676. {
  677. test: /\.css$/i,
  678. loader: 'css-loader',
  679. options: {
  680. modules: {
  681. localIdentRegExp: /page-(.*)\.css/i,
  682. },
  683. },
  684. },
  685. ],
  686. },
  687. };
  688. ```
  689. ### `sourceMap`
  690. Type: `Boolean`
  691. Default: `false`
  692. Enables/Disables generation of source maps.
  693. To include source maps set the `sourceMap` option.
  694. They are not enabled by default because they expose a runtime overhead and increase in bundle size (JS source maps do not).
  695. **webpack.config.js**
  696. ```js
  697. module.exports = {
  698. module: {
  699. rules: [
  700. {
  701. test: /\.css$/i,
  702. loader: 'css-loader',
  703. options: {
  704. sourceMap: true,
  705. },
  706. },
  707. ],
  708. },
  709. };
  710. ```
  711. ### `importLoaders`
  712. Type: `Number`
  713. Default: `0`
  714. Enables/Disables or setups number of loaders applied before CSS loader.
  715. The option `importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources.
  716. **webpack.config.js**
  717. ```js
  718. module.exports = {
  719. module: {
  720. rules: [
  721. {
  722. test: /\.css$/i,
  723. use: [
  724. 'style-loader',
  725. {
  726. loader: 'css-loader',
  727. options: {
  728. importLoaders: 2,
  729. // 0 => no loaders (default);
  730. // 1 => postcss-loader;
  731. // 2 => postcss-loader, sass-loader
  732. },
  733. },
  734. 'postcss-loader',
  735. 'sass-loader',
  736. ],
  737. },
  738. ],
  739. },
  740. };
  741. ```
  742. This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
  743. ### `localsConvention`
  744. Type: `String`
  745. Default: `'asIs'`
  746. Style of exported classnames.
  747. By default, the exported JSON keys mirror the class names (i.e `asIs` value).
  748. | Name | Type | Description |
  749. | :-------------------: | :--------: | :----------------------------------------------------------------------------------------------- |
  750. | **`'asIs'`** | `{String}` | Class names will be exported as is. |
  751. | **`'camelCase'`** | `{String}` | Class names will be camelized, the original class name will not to be removed from the locals |
  752. | **`'camelCaseOnly'`** | `{String}` | Class names will be camelized, the original class name will be removed from the locals |
  753. | **`'dashes'`** | `{String}` | Only dashes in class names will be camelized |
  754. | **`'dashesOnly'`** | `{String}` | Dashes in class names will be camelized, the original class name will be removed from the locals |
  755. **file.css**
  756. ```css
  757. .class-name {
  758. }
  759. ```
  760. **file.js**
  761. ```js
  762. import { className } from 'file.css';
  763. ```
  764. **webpack.config.js**
  765. ```js
  766. module.exports = {
  767. module: {
  768. rules: [
  769. {
  770. test: /\.css$/i,
  771. loader: 'css-loader',
  772. options: {
  773. localsConvention: 'camelCase',
  774. },
  775. },
  776. ],
  777. },
  778. };
  779. ```
  780. ### `onlyLocals`
  781. Type: `Boolean`
  782. Default: `false`
  783. Export only locals.
  784. **Useful** when you use **css modules** for pre-rendering (for example SSR).
  785. For pre-rendering with `mini-css-extract-plugin` you should use this option instead of `style-loader!css-loader` **in the pre-rendering bundle**.
  786. It doesn't embed CSS but only exports the identifier mappings.
  787. **webpack.config.js**
  788. ```js
  789. module.exports = {
  790. module: {
  791. rules: [
  792. {
  793. test: /\.css$/i,
  794. loader: 'css-loader',
  795. options: {
  796. onlyLocals: true,
  797. },
  798. },
  799. ],
  800. },
  801. };
  802. ```
  803. ### `esModule`
  804. Type: `Boolean`
  805. Default: `false`
  806. By default, `css-loader` generates JS modules that use the CommonJS modules syntax.
  807. 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/).
  808. You can enable a ES module syntax using:
  809. **webpack.config.js**
  810. ```js
  811. module.exports = {
  812. module: {
  813. rules: [
  814. {
  815. test: /\.css$/i,
  816. loader: 'css-loader',
  817. options: {
  818. esModule: true,
  819. },
  820. },
  821. ],
  822. },
  823. };
  824. ```
  825. ## Examples
  826. ### Assets
  827. The following `webpack.config.js` can load CSS files, embed small PNG/JPG/GIF/SVG images as well as fonts as [Data URLs](https://tools.ietf.org/html/rfc2397) and copy larger files to the output directory.
  828. **webpack.config.js**
  829. ```js
  830. module.exports = {
  831. module: {
  832. rules: [
  833. {
  834. test: /\.css$/i,
  835. use: ['style-loader', 'css-loader'],
  836. },
  837. {
  838. test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
  839. loader: 'url-loader',
  840. options: {
  841. limit: 8192,
  842. },
  843. },
  844. ],
  845. },
  846. };
  847. ```
  848. ### Extract
  849. 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.
  850. - This can be achieved by using the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) to extract the CSS when running in production mode.
  851. - As an alternative, if seeking better development performance and css outputs that mimic production. [extract-css-chunks-webpack-plugin](https://github.com/faceyspacey/extract-css-chunks-webpack-plugin) offers a hot module reload friendly, extended version of mini-css-extract-plugin. HMR real CSS files in dev, works like mini-css in non-dev
  852. ### Pure CSS, CSS modules and PostCSS
  853. When you have pure CSS (without CSS modules), CSS modules and PostCSS in your project you can use this setup:
  854. **webpack.config.js**
  855. ```js
  856. module.exports = {
  857. module: {
  858. rules: [
  859. {
  860. // For CSS modules
  861. // For pure CSS - /\.css$/i,
  862. // For Sass/SCSS - /\.((c|sa|sc)ss)$/i,
  863. // For Less - /\.((c|le)ss)$/i,
  864. test: /\.((c|sa|sc)ss)$/i,
  865. use: [
  866. 'style-loader',
  867. {
  868. loader: 'css-loader',
  869. options: {
  870. // Run `postcss-loader` on each CSS `@import`, do not forget that `sass-loader` compile non CSS `@import`'s into a single file
  871. // If you need run `sass-loader` and `postcss-loader` on each CSS `@import` please set it to `2`
  872. importLoaders: 1,
  873. // Automatically enable css modules for files satisfying `/\.module\.\w+$/i` RegExp.
  874. modules: { auto: true },
  875. },
  876. },
  877. {
  878. loader: 'postcss-loader',
  879. options: { plugins: () => [postcssPresetEnv({ stage: 0 })] },
  880. },
  881. // Can be `less-loader`
  882. // The `test` property should be `\.less/i`
  883. {
  884. test: /\.s[ac]ss$/i,
  885. loader: 'sass-loader',
  886. },
  887. ],
  888. },
  889. {
  890. test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/i,
  891. loader: 'url-loader',
  892. options: {
  893. limit: 8192,
  894. },
  895. },
  896. ],
  897. },
  898. };
  899. ```
  900. ## Contributing
  901. Please take a moment to read our contributing guidelines if you haven't yet done so.
  902. [CONTRIBUTING](./.github/CONTRIBUTING.md)
  903. ## License
  904. [MIT](./LICENSE)
  905. [npm]: https://img.shields.io/npm/v/css-loader.svg
  906. [npm-url]: https://npmjs.com/package/css-loader
  907. [node]: https://img.shields.io/node/v/css-loader.svg
  908. [node-url]: https://nodejs.org
  909. [deps]: https://david-dm.org/webpack-contrib/css-loader.svg
  910. [deps-url]: https://david-dm.org/webpack-contrib/css-loader
  911. [tests]: https://github.com/webpack-contrib/css-loader/workflows/css-loader/badge.svg
  912. [tests-url]: https://github.com/webpack-contrib/css-loader/actions
  913. [cover]: https://codecov.io/gh/webpack-contrib/css-loader/branch/master/graph/badge.svg
  914. [cover-url]: https://codecov.io/gh/webpack-contrib/css-loader
  915. [chat]: https://badges.gitter.im/webpack/webpack.svg
  916. [chat-url]: https://gitter.im/webpack/webpack
  917. [size]: https://packagephobia.now.sh/badge?p=css-loader
  918. [size-url]: https://packagephobia.now.sh/result?p=css-loader