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