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.

1101 lines
24 KiB

3 months ago
  1. <div align="center">
  2. <img
  3. width="180"
  4. height="180"
  5. hspace="10"
  6. alt="PostCSS Logo"
  7. src="https://api.postcss.org/logo.svg">
  8. <a href="https://github.com/webpack/webpack">
  9. <img
  10. width="200"
  11. height="200"
  12. hspace="10"
  13. src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg">
  14. </a>
  15. <div align="center">
  16. <a href="https://evilmartians.com/?utm_source=postcss">
  17. <img
  18. src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg"
  19. alt="Sponsored by Evil Martians"
  20. width="236"
  21. height="54"
  22. vspace="10">
  23. </a>
  24. </div>
  25. </div>
  26. [![npm][npm]][npm-url]
  27. [![node][node]][node-url]
  28. [![deps][deps]][deps-url]
  29. [![tests][tests]][tests-url]
  30. [![coverage][cover]][cover-url]
  31. [![size][size]][size-url]
  32. Webpack chat: [![chat][chat]][chat-url]
  33. PostCSS chat: [![chat-postcss][chat-postcss]][chat-postcss-url]
  34. # postcss-loader
  35. Loader to process CSS with [`PostCSS`](https://github.com/postcss/postcss).
  36. ## Getting Started
  37. You need webpack v5 to use the latest version. For Webpack v4, you have to install postcss-loader v4.
  38. To begin, you'll need to install `postcss-loader` and `postcss`:
  39. ```console
  40. npm install --save-dev postcss-loader postcss
  41. ```
  42. Then add the plugin to your `webpack` config. For example:
  43. > In the following configuration the plugin [`postcss-preset-env`](https://github.com/csstools/postcss-preset-env) is used, which is not installed by default.
  44. **file.js**
  45. ```js
  46. import css from "file.css";
  47. ```
  48. **webpack.config.js**
  49. ```js
  50. module.exports = {
  51. module: {
  52. rules: [
  53. {
  54. test: /\.css$/i,
  55. use: [
  56. "style-loader",
  57. "css-loader",
  58. {
  59. loader: "postcss-loader",
  60. options: {
  61. postcssOptions: {
  62. plugins: [
  63. [
  64. "postcss-preset-env",
  65. {
  66. // Options
  67. },
  68. ],
  69. ],
  70. },
  71. },
  72. },
  73. ],
  74. },
  75. ],
  76. },
  77. };
  78. ```
  79. Alternative use with [config files](#config):
  80. **postcss.config.js**
  81. ```js
  82. module.exports = {
  83. plugins: [
  84. [
  85. "postcss-preset-env",
  86. {
  87. // Options
  88. },
  89. ],
  90. ],
  91. };
  92. ```
  93. The loader **automatically** searches for configuration files.
  94. **webpack.config.js**
  95. ```js
  96. module.exports = {
  97. module: {
  98. rules: [
  99. {
  100. test: /\.css$/i,
  101. use: ["style-loader", "css-loader", "postcss-loader"],
  102. },
  103. ],
  104. },
  105. };
  106. ```
  107. And run `webpack` via your preferred method.
  108. ## Options
  109. | Name | Type | Default | Description |
  110. | :---------------------------------: | :------------------: | :-----------------------------------: | :------------------------------------------- |
  111. | [`execute`](#execute) | `{Boolean}` | `undefined` | Enable PostCSS Parser support in `CSS-in-JS` |
  112. | [`postcssOptions`](#postcssOptions) | `{Object\|Function}` | `defaults values for Postcss.process` | Set `PostCSS` options and plugins |
  113. | [`sourceMap`](#sourcemap) | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
  114. | [`implementation`](#implementation) | `{Function\|String}` | `postcss` | Setup PostCSS implementation to use |
  115. ### `execute`
  116. Type: `Boolean`
  117. Default: `undefined`
  118. If you use JS styles the [`postcss-js`](https://github.com/postcss/postcss-js) parser, add the `execute` option.
  119. **webpack.config.js**
  120. ```js
  121. module.exports = {
  122. module: {
  123. rules: [
  124. {
  125. test: /\.style.js$/,
  126. use: [
  127. "style-loader",
  128. {
  129. loader: "css-loader",
  130. },
  131. {
  132. loader: "postcss-loader",
  133. options: {
  134. postcssOptions: {
  135. parser: "postcss-js",
  136. },
  137. execute: true,
  138. },
  139. },
  140. ],
  141. },
  142. ],
  143. },
  144. };
  145. ```
  146. ### `postcssOptions`
  147. Type: `Object|Function`
  148. Default: `undefined`
  149. Allows to set [`PostCSS options`](https://postcss.org/api/#processoptions) and plugins.
  150. All `PostCSS` options are supported.
  151. There is the special `config` option for config files. How it works and how it can be configured is described below.
  152. We recommend do not specify `from`, `to` and `map` options, because this can lead to wrong path in source maps.
  153. If you need source maps please use the [`sourcemap`](#sourcemap) option.
  154. #### `Object`
  155. Setup `plugins`:
  156. **webpack.config.js** (**recommended**)
  157. ```js
  158. const myOtherPostcssPlugin = require("postcss-my-plugin");
  159. module.exports = {
  160. module: {
  161. rules: [
  162. {
  163. test: /\.sss$/i,
  164. loader: "postcss-loader",
  165. options: {
  166. postcssOptions: {
  167. plugins: [
  168. "postcss-import",
  169. ["postcss-short", { prefix: "x" }],
  170. require.resolve("my-postcss-plugin"),
  171. myOtherPostcssPlugin({ myOption: true }),
  172. // Deprecated and will be removed in the next major release
  173. { "postcss-nested": { preserveEmpty: true } },
  174. ],
  175. },
  176. },
  177. },
  178. ],
  179. },
  180. };
  181. ```
  182. **webpack.config.js** (**deprecated**, will be removed in the next major release)
  183. ```js
  184. module.exports = {
  185. module: {
  186. rules: [
  187. {
  188. test: /\.sss$/i,
  189. loader: "postcss-loader",
  190. options: {
  191. postcssOptions: {
  192. plugins: {
  193. "postcss-import": {},
  194. "postcss-short": { prefix: "x" },
  195. },
  196. },
  197. },
  198. },
  199. ],
  200. },
  201. };
  202. ```
  203. Setup `syntax`:
  204. **webpack.config.js**
  205. ```js
  206. module.exports = {
  207. module: {
  208. rules: [
  209. {
  210. test: /\.sss$/i,
  211. loader: "postcss-loader",
  212. options: {
  213. postcssOptions: {
  214. // Can be `String`
  215. syntax: "sugarss",
  216. // Can be `Object`
  217. syntax: require("sugarss"),
  218. },
  219. },
  220. },
  221. ],
  222. },
  223. };
  224. ```
  225. Setup `parser`:
  226. **webpack.config.js**
  227. ```js
  228. module.exports = {
  229. module: {
  230. rules: [
  231. {
  232. test: /\.sss$/i,
  233. loader: "postcss-loader",
  234. options: {
  235. postcssOptions: {
  236. // Can be `String`
  237. parser: "sugarss",
  238. // Can be `Object`
  239. parser: require("sugarss"),
  240. // Can be `Function`
  241. parser: require("sugarss").parse,
  242. },
  243. },
  244. },
  245. ],
  246. },
  247. };
  248. ```
  249. Setup `stringifier`:
  250. **webpack.config.js**
  251. ```js
  252. const Midas = require("midas");
  253. const midas = new Midas();
  254. module.exports = {
  255. module: {
  256. rules: [
  257. {
  258. test: /\.sss$/i,
  259. loader: "postcss-loader",
  260. options: {
  261. postcssOptions: {
  262. // Can be `String`
  263. stringifier: "sugarss",
  264. // Can be `Object`
  265. stringifier: require("sugarss"),
  266. // Can be `Function`
  267. stringifier: midas.stringifier,
  268. },
  269. },
  270. },
  271. ],
  272. },
  273. };
  274. ```
  275. #### `Function`
  276. **webpack.config.js**
  277. ```js
  278. module.exports = {
  279. module: {
  280. rules: [
  281. {
  282. test: /\.(css|sss)$/i,
  283. loader: "postcss-loader",
  284. options: {
  285. postcssOptions: (loaderContext) => {
  286. if (/\.sss$/.test(loaderContext.resourcePath)) {
  287. return {
  288. parser: "sugarss",
  289. plugins: [
  290. ["postcss-short", { prefix: "x" }],
  291. "postcss-preset-env",
  292. ],
  293. };
  294. }
  295. return {
  296. plugins: [
  297. ["postcss-short", { prefix: "x" }],
  298. "postcss-preset-env",
  299. ],
  300. };
  301. },
  302. },
  303. },
  304. ],
  305. },
  306. };
  307. ```
  308. #### `config`
  309. Type: `Boolean|String`
  310. Default: `undefined`
  311. Allows to set options using config files.
  312. Options specified in the config file are combined with options passed to the loader, the loader options overwrite options from config.
  313. ##### Config Files
  314. The loader will search up the directory tree for configuration in the following places:
  315. - a `postcss` property in `package.json`
  316. - a `.postcssrc` file in JSON or YAML format
  317. - a `.postcssrc.json`, `.postcssrc.yaml`, `.postcssrc.yml`, `.postcssrc.js`, or `.postcssrc.cjs` file
  318. - a `postcss.config.js` or `postcss.config.cjs` CommonJS module exporting an object (**recommended**)
  319. ##### Examples of Config Files
  320. Using `Object` notation:
  321. **postcss.config.js** (**recommend**)
  322. ```js
  323. module.exports = {
  324. // You can specify any options from https://postcss.org/api/#processoptions here
  325. // parser: 'sugarss',
  326. plugins: [
  327. // Plugins for PostCSS
  328. ["postcss-short", { prefix: "x" }],
  329. "postcss-preset-env",
  330. ],
  331. };
  332. ```
  333. Using `Function` notation:
  334. **postcss.config.js** (**recommend**)
  335. ```js
  336. module.exports = (api) => {
  337. // `api.file` - path to the file
  338. // `api.mode` - `mode` value of webpack, please read https://webpack.js.org/configuration/mode/
  339. // `api.webpackLoaderContext` - loader context for complex use cases
  340. // `api.env` - alias `api.mode` for compatibility with `postcss-cli`
  341. // `api.options` - the `postcssOptions` options
  342. if (/\.sss$/.test(api.file)) {
  343. return {
  344. // You can specify any options from https://postcss.org/api/#processoptions here
  345. parser: "sugarss",
  346. plugins: [
  347. // Plugins for PostCSS
  348. ["postcss-short", { prefix: "x" }],
  349. "postcss-preset-env",
  350. ],
  351. };
  352. }
  353. return {
  354. // You can specify any options from https://postcss.org/api/#processoptions here
  355. plugins: [
  356. // Plugins for PostCSS
  357. ["postcss-short", { prefix: "x" }],
  358. "postcss-preset-env",
  359. ],
  360. };
  361. };
  362. ```
  363. **postcss.config.js** (**deprecated**, will be removed in the next major release)
  364. ```js
  365. module.exports = {
  366. // You can specify any options from https://postcss.org/api/#processoptions here
  367. // parser: 'sugarss',
  368. plugins: {
  369. // Plugins for PostCSS
  370. "postcss-short": { prefix: "x" },
  371. "postcss-preset-env": {},
  372. },
  373. };
  374. ```
  375. ##### Config Cascade
  376. You can use different `postcss.config.js` files in different directories.
  377. Config lookup starts from `path.dirname(file)` and walks the file tree upwards until a config file is found.
  378. ```
  379. |– components
  380. | |– component
  381. | | |– index.js
  382. | | |– index.png
  383. | | |– style.css (1)
  384. | | |– postcss.config.js (1)
  385. | |– component
  386. | | |– index.js
  387. | | |– image.png
  388. | | |– style.css (2)
  389. |
  390. |– postcss.config.js (1 && 2 (recommended))
  391. |– webpack.config.js
  392. |
  393. |– package.json
  394. ```
  395. After setting up your `postcss.config.js`, add `postcss-loader` to your `webpack.config.js`.
  396. You can use it standalone or in conjunction with `css-loader` (recommended).
  397. Use it **before** `css-loader` and `style-loader`, but **after** other preprocessor loaders like e.g `sass|less|stylus-loader`, if you use any (since [webpack loaders evaluate right to left/bottom to top](https://webpack.js.org/concepts/loaders/#configuration)).
  398. **webpack.config.js** (**recommended**)
  399. ```js
  400. module.exports = {
  401. module: {
  402. rules: [
  403. {
  404. test: /\.css$/,
  405. use: [
  406. "style-loader",
  407. {
  408. loader: "css-loader",
  409. options: {
  410. importLoaders: 1,
  411. },
  412. },
  413. "postcss-loader",
  414. ],
  415. },
  416. ],
  417. },
  418. };
  419. ```
  420. #### Boolean
  421. Enables/Disables autoloading config.
  422. **webpack.config.js**
  423. ```js
  424. module.exports = {
  425. module: {
  426. rules: [
  427. {
  428. test: /\.css$/i,
  429. loader: "postcss-loader",
  430. options: {
  431. postcssOptions: {
  432. config: false,
  433. },
  434. },
  435. },
  436. ],
  437. },
  438. };
  439. ```
  440. #### String
  441. Allows to specify the path to the config file.
  442. **webpack.config.js**
  443. ```js
  444. const path = require("path");
  445. module.exports = {
  446. module: {
  447. rules: [
  448. {
  449. test: /\.css$/i,
  450. loader: "postcss-loader",
  451. options: {
  452. postcssOptions: {
  453. config: path.resolve(__dirname, "custom.config.js"),
  454. },
  455. },
  456. },
  457. ],
  458. },
  459. };
  460. ```
  461. ### `sourceMap`
  462. Type: `Boolean`
  463. Default: depends on the `compiler.devtool` value
  464. By default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option.
  465. All values enable source map generation except `eval` and `false` value.
  466. **webpack.config.js**
  467. ```js
  468. module.exports = {
  469. module: {
  470. rules: [
  471. {
  472. test: /\.css$/i,
  473. use: [
  474. { loader: "style-loader" },
  475. { loader: "css-loader", options: { sourceMap: true } },
  476. { loader: "postcss-loader", options: { sourceMap: true } },
  477. { loader: "sass-loader", options: { sourceMap: true } },
  478. ],
  479. },
  480. ],
  481. },
  482. };
  483. ```
  484. Alternative setup:
  485. **webpack.config.js**
  486. ```js
  487. module.exports = {
  488. devtool: "source-map",
  489. module: {
  490. rules: [
  491. {
  492. test: /\.css$/i,
  493. use: [
  494. { loader: "style-loader" },
  495. { loader: "css-loader" },
  496. { loader: "postcss-loader" },
  497. { loader: "sass-loader" },
  498. ],
  499. },
  500. ],
  501. },
  502. };
  503. ```
  504. ### `implementation`
  505. Type: `Function | String`
  506. Default: `postcss`
  507. The special `implementation` option determines which implementation of PostCSS to use. Overrides the locally installed `peerDependency` version of `postcss`.
  508. **This option is only really useful for downstream tooling authors to ease the PostCSS 7-to-8 transition.**
  509. #### Function
  510. **webpack.config.js**
  511. ```js
  512. module.exports = {
  513. module: {
  514. rules: [
  515. {
  516. test: /\.css$/i,
  517. use: [
  518. { loader: "style-loader" },
  519. { loader: "css-loader" },
  520. {
  521. loader: "postcss-loader",
  522. options: { implementation: require("postcss") },
  523. },
  524. { loader: "sass-loader" },
  525. ],
  526. },
  527. ],
  528. },
  529. };
  530. ```
  531. #### String
  532. **webpack.config.js**
  533. ```js
  534. module.exports = {
  535. module: {
  536. rules: [
  537. {
  538. test: /\.css$/i,
  539. use: [
  540. { loader: "style-loader" },
  541. { loader: "css-loader" },
  542. {
  543. loader: "postcss-loader",
  544. options: { implementation: require.resolve("postcss") },
  545. },
  546. { loader: "sass-loader" },
  547. ],
  548. },
  549. ],
  550. },
  551. };
  552. ```
  553. ## Examples
  554. ### SugarSS
  555. You'll need to install `sugarss`:
  556. ```console
  557. npm install --save-dev sugarss
  558. ```
  559. Using [`SugarSS`](https://github.com/postcss/sugarss) syntax.
  560. **webpack.config.js**
  561. ```js
  562. module.exports = {
  563. module: {
  564. rules: [
  565. {
  566. test: /\.sss$/i,
  567. use: [
  568. "style-loader",
  569. {
  570. loader: "css-loader",
  571. options: { importLoaders: 1 },
  572. },
  573. {
  574. loader: "postcss-loader",
  575. options: {
  576. postcssOptions: {
  577. parser: "sugarss",
  578. },
  579. },
  580. },
  581. ],
  582. },
  583. ],
  584. },
  585. };
  586. ```
  587. ### Autoprefixer
  588. You'll need to install `autoprefixer`:
  589. ```console
  590. npm install --save-dev autoprefixer
  591. ```
  592. Add vendor prefixes to CSS rules using [`autoprefixer`](https://github.com/postcss/autoprefixer).
  593. **webpack.config.js**
  594. ```js
  595. module.exports = {
  596. module: {
  597. rules: [
  598. {
  599. test: /\.css$/i,
  600. use: [
  601. "style-loader",
  602. {
  603. loader: "css-loader",
  604. options: { importLoaders: 1 },
  605. },
  606. {
  607. loader: "postcss-loader",
  608. options: {
  609. postcssOptions: {
  610. plugins: [
  611. [
  612. "autoprefixer",
  613. {
  614. // Options
  615. },
  616. ],
  617. ],
  618. },
  619. },
  620. },
  621. ],
  622. },
  623. ],
  624. },
  625. };
  626. ```
  627. > :warning: [`postcss-preset-env`](https://github.com/csstools/postcss-preset-env) includes [`autoprefixer`](https://github.com/postcss/autoprefixer), so adding it separately is not necessary if you already use the preset. More [information](https://github.com/csstools/postcss-preset-env#autoprefixer)
  628. ### PostCSS Preset Env
  629. You'll need to install `postcss-preset-env`:
  630. ```console
  631. npm install --save-dev postcss-preset-env
  632. ```
  633. **webpack.config.js**
  634. ```js
  635. module.exports = {
  636. module: {
  637. rules: [
  638. {
  639. test: /\.css$/i,
  640. use: [
  641. "style-loader",
  642. {
  643. loader: "css-loader",
  644. options: { importLoaders: 1 },
  645. },
  646. {
  647. loader: "postcss-loader",
  648. options: {
  649. postcssOptions: {
  650. plugins: [
  651. [
  652. "postcss-preset-env",
  653. {
  654. // Options
  655. },
  656. ],
  657. ],
  658. },
  659. },
  660. },
  661. ],
  662. },
  663. ],
  664. },
  665. };
  666. ```
  667. ### CSS Modules
  668. What is `CSS Modules`? Please [read](https://github.com/webpack-contrib/css-loader#modules).
  669. No additional options required on the `postcss-loader` side.
  670. To make them work properly, either add the `css-loader`’s `importLoaders` option.
  671. **webpack.config.js**
  672. ```js
  673. module.exports = {
  674. module: {
  675. rules: [
  676. {
  677. test: /\.css$/i,
  678. use: [
  679. "style-loader",
  680. {
  681. loader: "css-loader",
  682. options: {
  683. modules: true,
  684. importLoaders: 1,
  685. },
  686. },
  687. "postcss-loader",
  688. ],
  689. },
  690. ],
  691. },
  692. };
  693. ```
  694. ### CSS-in-JS and [`postcss-js`](https://github.com/postcss/postcss-js)
  695. You'll need to install `postcss-js`:
  696. ```console
  697. npm install --save-dev postcss-js
  698. ```
  699. If you want to process styles written in JavaScript, use the [`postcss-js`](https://github.com/postcss/postcss-js) parser.
  700. **webpack.config.js**
  701. ```js
  702. module.exports = {
  703. module: {
  704. rules: [
  705. {
  706. test: /\.style.js$/,
  707. use: [
  708. "style-loader",
  709. {
  710. loader: "css-loader",
  711. options: {
  712. importLoaders: 2,
  713. },
  714. },
  715. {
  716. loader: "postcss-loader",
  717. options: {
  718. postcssOptions: {
  719. parser: "postcss-js",
  720. },
  721. execute: true,
  722. },
  723. },
  724. "babel-loader",
  725. ],
  726. },
  727. ],
  728. },
  729. };
  730. ```
  731. As result you will be able to write styles in the following way
  732. ```js
  733. import colors from "./styles/colors";
  734. export default {
  735. ".menu": {
  736. color: colors.main,
  737. height: 25,
  738. "&_link": {
  739. color: "white",
  740. },
  741. },
  742. };
  743. ```
  744. > :warning: If you are using Babel you need to do the following in order for the setup to work
  745. > 1. Add [`babel-plugin-add-module-exports`](https://github.com/59naga/babel-plugin-add-module-exports) to your configuration.
  746. > 2. You need to have only one **default** export per style module.
  747. ### Extract CSS
  748. Using [`mini-css-extract-plugin`](https://github.com/webpack-contrib/mini-css-extract-plugin).
  749. **webpack.config.js**
  750. ```js
  751. const isProductionMode = process.env.NODE_ENV === "production";
  752. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  753. module.exports = {
  754. mode: isProductionMode ? "production" : "development",
  755. module: {
  756. rules: [
  757. {
  758. test: /\.css$/,
  759. use: [
  760. isProductionMode ? MiniCssExtractPlugin.loader : "style-loader",
  761. "css-loader",
  762. "postcss-loader",
  763. ],
  764. },
  765. ],
  766. },
  767. plugins: [
  768. new MiniCssExtractPlugin({
  769. filename: isProductionMode ? "[name].[contenthash].css" : "[name].css",
  770. }),
  771. ],
  772. };
  773. ```
  774. ### Emit assets
  775. To write a asset from PostCSS plugin to the webpack, need to add a message in `result.messages`.
  776. The message should contain the following fields:
  777. - `type` = `asset` - Message type (require, should be equal `asset`)
  778. - `file` - file name (require)
  779. - `content` - file content (require)
  780. - `sourceMap` - sourceMap
  781. - `info` - asset info
  782. **webpack.config.js**
  783. ```js
  784. const customPlugin = () => (css, result) => {
  785. result.messages.push({
  786. type: "asset",
  787. file: "sprite.svg",
  788. content: "<svg>...</svg>",
  789. });
  790. };
  791. const postcssPlugin = postcss.plugin("postcss-assets", customPlugin);
  792. module.exports = {
  793. module: {
  794. rules: [
  795. {
  796. test: /\.css$/i,
  797. use: [
  798. "style-loader",
  799. "css-loader",
  800. {
  801. loader: "postcss-loader",
  802. options: {
  803. postcssOptions: {
  804. plugins: [postcssPlugin()],
  805. },
  806. },
  807. },
  808. ],
  809. },
  810. ],
  811. },
  812. };
  813. ```
  814. ### Add dependencies, contextDependencies, buildDependencies, missingDependencies
  815. The dependencies are necessary for webpack to understand when it needs to run recompilation on the changed files.
  816. There are two way to add dependencies:
  817. 1. (Recommended). The plugin may emit messages in `result.messages`.
  818. The message should contain the following fields:
  819. - `type` = `dependency` - Message type (require, should be equal `dependency`, `context-dependency`, `build-dependency` or `missing-dependency`)
  820. - `file` - absolute file path (require)
  821. **webpack.config.js**
  822. ```js
  823. const path = require("path");
  824. const customPlugin = () => (css, result) => {
  825. result.messages.push({
  826. type: "dependency",
  827. file: path.resolve(__dirname, "path", "to", "file"),
  828. });
  829. };
  830. const postcssPlugin = postcss.plugin("postcss-assets", customPlugin);
  831. module.exports = {
  832. module: {
  833. rules: [
  834. {
  835. test: /\.css$/i,
  836. use: [
  837. "style-loader",
  838. "css-loader",
  839. {
  840. loader: "postcss-loader",
  841. options: {
  842. postcssOptions: {
  843. plugins: [postcssPlugin()],
  844. },
  845. },
  846. },
  847. ],
  848. },
  849. ],
  850. },
  851. };
  852. ```
  853. 2. Pass `loaderContext` in plugin.
  854. **webpack.config.js**
  855. ```js
  856. const path = require("path");
  857. module.exports = {
  858. module: {
  859. rules: [
  860. {
  861. test: /\.css$/i,
  862. use: [
  863. "style-loader",
  864. "css-loader",
  865. {
  866. loader: "postcss-loader",
  867. options: {
  868. postcssOptions: {
  869. config: path.resolve(__dirname, "path/to/postcss.config.js"),
  870. },
  871. },
  872. },
  873. ],
  874. },
  875. ],
  876. },
  877. };
  878. ```
  879. **postcss.config.js**
  880. ```js
  881. module.exports = (api) => ({
  882. plugins: [
  883. require("path/to/customPlugin")({
  884. loaderContext: api.webpackLoaderContext,
  885. }),
  886. ],
  887. });
  888. ```
  889. **customPlugin.js**
  890. ```js
  891. const path = require("path");
  892. const customPlugin = (loaderContext) => (css, result) => {
  893. loaderContext.webpack.addDependency(
  894. path.resolve(__dirname, "path", "to", "file")
  895. );
  896. };
  897. module.exports = postcss.plugin("postcss-assets", customPlugin);
  898. ```
  899. ## Contributing
  900. Please take a moment to read our contributing guidelines if you haven't yet done so.
  901. [CONTRIBUTING](./.github/CONTRIBUTING.md)
  902. ## License
  903. [MIT](./LICENSE)
  904. [npm]: https://img.shields.io/npm/v/postcss-loader.svg
  905. [npm-url]: https://npmjs.com/package/postcss-loader
  906. [node]: https://img.shields.io/node/v/postcss-loader.svg
  907. [node-url]: https://nodejs.org
  908. [deps]: https://david-dm.org/webpack-contrib/postcss-loader.svg
  909. [deps-url]: https://david-dm.org/webpack-contrib/postcss-loader
  910. [tests]: https://github.com/webpack-contrib/postcss-loader/workflows/postcss-loader/badge.svg
  911. [tests-url]: https://github.com/webpack-contrib/postcss-loader/actions
  912. [cover]: https://codecov.io/gh/webpack-contrib/postcss-loader/branch/master/graph/badge.svg
  913. [cover-url]: https://codecov.io/gh/webpack-contrib/postcss-loader
  914. [chat]: https://badges.gitter.im/webpack/webpack.svg
  915. [chat-url]: https://gitter.im/webpack/webpack
  916. [chat-postcss]: https://badges.gitter.im/postcss/postcss.svg
  917. [chat-postcss-url]: https://gitter.im/postcss/postcss
  918. [size]: https://packagephobia.now.sh/badge?p=postcss-loader
  919. [size-url]: https://packagephobia.now.sh/result?p=postcss-loader