source: frontend/node_modules/postcss-preset-env/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 20.0 KB
Line 
1# PostCSS Preset Env [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][postcss]
2
3[<img alt="npm version" src="https://img.shields.io/npm/v/postcss-preset-env.svg" height="20">][npm-url]
4[<img alt="build status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url]
5[![install size][package-phobia-badge]][package-phobia]
6[<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
7
8[PostCSS Preset Env] lets you convert modern CSS into something most browsers
9can understand, determining the polyfills you need based on your targeted
10browsers or runtime environments.
11
12## Quick start
13
14[PostCSS Preset Env] is a [PostCSS] plugin.<br>
15If you are already using [PostCSS] to build your CSS, you can simply add [PostCSS Preset Env] to your configuration.
16
17- Install `postcss-preset-env` from npm.
18- Add `postcss-preset-env` to your configuration:
19
20```js
21const postcssPresetEnv = require('postcss-preset-env');
22
23const yourConfig = {
24 plugins: [
25 postcssPresetEnv(/* pluginOptions */)
26 ]
27}
28```
29
30_[Read more on how to use and install PostCSS Preset Env.](#usage)_
31
32## How does it work?
33
34[PostCSS Preset Env] is a Plugin Pack for [PostCSS]. It leverages the list of the features we keep an eye from [CSSDB][cssdb] and applies plugins, so you can use those new features without having to worry about browser support.
35
36CSSDB exposes the browser support that each feature has which can come from [Can I Use](https://caniuse.com/css-all) or from [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/all) (through [mdn/browser-compat-data](https://github.com/mdn/browser-compat-data)).
37
38By providing a list of browser targets for your project, plugins that aren't needed will be skipped. Over time your targets might change and by updating the settings your CSS bundle will only ever contain the needed fallbacks.
39
40What [PostCSS Preset Env] does is to take the support data that comes from MDN and Can I Use and determine from a [browserlist](https://github.com/browserslist/browserslist) whether those transformations are needed. It also packs [Autoprefixer](https://github.com/postcss/autoprefixer) within and shares the list with it, so prefixes are only applied when you're going to need them given your browser support list.
41
42### Glossary:
43
44* **Browser list option**: [Browserlist](https://github.com/browserslist/browserslist) is a package that gives you a list of browsers for a given query. For example, `chrome < 42` will give you a list of every Chrome version that has been released up to, but not including, 42.
45* **Browser support stats**: Features get introduced on browsers at certain versions. They're often visible on [MDN](https://developer.mozilla.org/en-US/) and [Can I Use](https://caniuse.com/). Comparing these stats with the needed _support_ for your project tells you if it's safe to use a feature or not.
46* **CSS Feature**: A CSS feature is often part of some spec that enables a specific feature. For example, `hwb` functional notation lets you express a given color according to its hue, whiteness, and blackness. This is part of the CSS Color 4 Spec.
47* **CSS Spec**: A Spec is a document that collects new features, what problems are they trying to solve and how it's intended to be solved (generally). This is usually an evolving document that goes over lengthy discussions between several people from different companies.
48* **Plugin**: A plugin is package that's intended (usually) to enable a new CSS Feature by leveraging PostCSS. This doesn't need to be part of any spec. An example of the latter is [PostCSS Mixins](https://github.com/postcss/postcss-mixins), a concept that existed on Less or Sass, but it's not part of any spec. This plugin plack **only** packs plugins that enable features acknowledged by the World Wide Web Consortium (W3C) which will then be implemented by browsers later.
49* **Polyfill**: A polyfill is a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it. A polyfill _should_ be indistinguishable from the native behaviour.
50
51Here's a quick example of the syntax you can leverage by using [PostCSS Preset Env].
52
53```pcss
54@custom-media --viewport-medium (width <= 50rem);
55@custom-selector :--heading h1, h2, h3, h4, h5, h6;
56
57:root {
58 --mainColor: #12345678;
59}
60
61body {
62 color: var(--mainColor);
63 font-family: system-ui;
64 overflow-wrap: break-word;
65}
66
67:--heading {
68 background-image: image-set(url(img/heading.png) 1x, url(img/heading@2x.png) 2x);
69
70 @media (--viewport-medium) {
71 margin-block: 0;
72 }
73}
74
75a {
76 color: rgb(0 0 100% / 90%);
77
78 &:hover {
79 color: rebeccapurple;
80 }
81}
82
83/* becomes */
84
85:root {
86 --mainColor: rgba(18, 52, 86, 0.47059);
87}
88
89body {
90 color: rgba(18, 52, 86, 0.47059);
91 color: var(--mainColor);
92 font-family: system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Droid Sans, Helvetica Neue;
93 word-wrap: break-word;
94}
95
96h1, h2, h3, h4, h5, h6 {
97 background-image: url(img/heading.png);
98}
99
100@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
101 h1, h2, h3, h4, h5, h6 {
102 background-image: url(img/heading@2x.png)
103 }
104}
105
106@media (max-width: 50rem) {
107 h1, h2, h3, h4, h5, h6 {
108 margin-top: 0;
109 margin-bottom: 0;
110 }
111}
112
113a {
114 color: rgba(0, 0, 255, 0.9)
115}
116
117a:hover {
118 color: #639;
119}
120```
121
122Without any configuration options, [PostCSS Preset Env] enables **Stage 2**
123features and supports **all** browsers.
124
125[![Transform with Preset Env][readme-transform-with-preset-env-img]][readme-transform-with-preset-env-url]
126[![Style with Preset Env][readme-style-with-preset-env-img]][readme-style-with-preset-env-url]
127
128⚠️ Please note that some features need a companion library that makes
129the feature work. While we try to avoid this requirement, there are instances
130in which this isn't possible to polyfill a new behaviour with _just_ CSS.
131
132[See the list below](#plugins-that-need-client-library).
133
134## Usage
135
136Add [PostCSS Preset Env] to your project:
137
138```bash
139npm install postcss-preset-env --save-dev
140```
141
142Use [PostCSS Preset Env] as a [PostCSS] plugin:
143
144```js
145const postcss = require('postcss');
146const postcssPresetEnv = require('postcss-preset-env');
147
148postcss([
149 postcssPresetEnv(/* pluginOptions */)
150]).process(YOUR_CSS /*, processOptions */);
151```
152
153[PostCSS Preset Env] runs in all Node environments, with special instructions for:
154
155| [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) | [Rollup](INSTALL.md#rollup) |
156| --- | --- | --- | --- | --- | --- | --- |
157
158## Options
159
160### stage
161
162The `stage` option determines which CSS features to polyfill, based upon their
163stability in the process of becoming implemented web standards.
164
165```js
166postcssPresetEnv({ stage: 0 })
167```
168
169The `stage` can be `0` (experimental) through `4` (stable), or `false`. Setting
170`stage` to `false` will disable every polyfill. Doing this would only be useful
171if you intended to exclusively use the [`features`](#features) option.
172
173Default: `2`
174
175### minimumVendorImplementations
176
177The `minimumVendorImplementations` option determines which CSS features to polyfill, based their implementation status.
178This can be used to enable plugins that are available in browsers regardless of the [spec status](#stage).
179
180```js
181postcssPresetEnv({ minimumVendorImplementations: 2 })
182```
183
184`minimumVendorImplementations` can be `0` (no vendor has implemented it) through `3` (all major vendors).<br>
185
186Default: `0`
187
188**Note:**
189
190When a feature has not yet been implemented by any vendor it can be considered experimental.<br>
191Even with a single implementation it might still change in the future.<br>
192Sometimes issues with a feature/specification are only discovered after it becomes available.
193
194A value of `2` is recommended when you want to use only those features that should be [stable](#stability-and-portability).
195
196Having 2 independent implementations is [a critical step in proposals becoming standards](https://www.w3.org/2021/Process-20211102/#implementation-experience) and a good indicator of a feature's stability.
197
198### features
199
200The `features` option enables or disables specific polyfills by ID. Passing
201`true` to a specific feature ID will enable its polyfill, while passing `false`
202will disable it. [List of Features](https://github.com/csstools/postcss-plugins/blob/main/plugin-packs/postcss-preset-env/FEATURES.md)
203
204```js
205postcssPresetEnv({
206 /* use stage 3 features + css nesting rules */
207 stage: 3,
208 features: {
209 'nesting-rules': true
210 }
211})
212```
213
214Passing an object to a specific feature ID will both enable and configure it.
215
216```js
217postcssPresetEnv({
218 /* use stage 3 features + css color-mod (warning on unresolved) */
219 stage: 3,
220 features: {
221 'color-mod-function': { unresolved: 'warn' }
222 }
223})
224```
225
226Any polyfills not explicitly enabled or disabled through `features` are
227determined by the [`stage`](#stage) option.
228
229### browsers
230
231The `browsers` option determines which polyfills are required based upon the
232browsers you are supporting.
233
234[PostCSS Preset Env] supports any standard [browserslist] configuration, which
235can be a `.browserslistrc` file, a `browserslist` key in `package.json`, or
236`browserslist` environment variables.
237
238The `browsers` option should only be used when a standard browserslist
239configuration is not available.
240
241```js
242postcssPresetEnv({ browsers: 'last 2 versions' })
243```
244
245If no valid browserslist configuration is specified, the
246[default browserslist query](https://github.com/browserslist/browserslist#queries)
247will be used.
248
249### insertBefore / insertAfter
250
251The `insertBefore` and `insertAfter` keys allow you to insert other PostCSS
252plugins into the chain. This is only useful if you are also using sugary
253PostCSS plugins that must execute before or after certain polyfills.
254Both `insertBefore` and `insertAfter` support chaining one or multiple plugins.
255
256```js
257import postcssSimpleVars from 'postcss-simple-vars';
258
259postcssPresetEnv({
260 insertBefore: {
261 'all-property': postcssSimpleVars
262 }
263})
264```
265
266### autoprefixer
267
268[PostCSS Preset Env] includes [autoprefixer] and [`browsers`](#browsers) option
269will be passed to it automatically.
270
271Specifying the `autoprefixer` option enables passing
272[additional options](https://github.com/postcss/autoprefixer#options)
273into [autoprefixer].
274
275```js
276postcssPresetEnv({
277 autoprefixer: { grid: true }
278})
279```
280
281Passing `autoprefixer: false` disables autoprefixer.
282
283⚠️ [autoprefixer] has [complex logic to fix CSS Grid in IE and older Edge](https://github.com/postcss/autoprefixer#grid-autoplacement-support-in-ie).
284
285This can have unexpected results with certain features and when [`preserve: true`](#preserve) is used. (defaults to `true`)
286
287```pcss
288:root {
289 --grid-gap: 15px;
290}
291
292.test-grid {
293 grid-gap: var(--grid-gap);
294 grid-template-columns: repeat(2, 1fr);
295}
296```
297
298Becomes :
299
300```
301.test-grid {
302 grid-gap: 15px;
303 grid-gap: var(--grid-gap);
304 -ms-grid-columns: 1fr var(--grid-gap) 1fr;
305 grid-template-columns: repeat(2, 1fr);
306}
307```
308
309The prefixed `-ms-grid-columns` still has a custom property: `1fr var(--grid-gap) 1fr;` which won't work.<br />
310This example shows issues with custom properties but other transforms might have similar issues.
311
312If you target IE or older Edge it is best to avoid using other modern features in grid property values.<br />
313As a last resort you can set [`preserve: false`](#preserve), we do not advice this as doing so purely to fix issues with CSS grid.
314
315_older Edge is any version before chromium (<79)_
316
317### preserve
318
319The `preserve` option determines whether all plugins should receive a
320`preserve` option, which may preserve or remove otherwise-polyfilled CSS. By
321default, this option is not configured.
322
323```js
324postcssPresetEnv({
325 preserve: false // instruct all plugins to omit pre-polyfilled CSS
326});
327```
328
329### importFrom
330
331The `importFrom` option specifies sources where variables like Custom Media,
332Custom Properties, Custom Selectors, and Environment Variables can be imported
333from, which might be CSS, JS, and JSON files, functions, and directly passed
334objects.
335
336```js
337postcssPresetEnv({
338 /*
339 @custom-media --small-viewport (max-width: 30em);
340 @custom-selector :--heading h1, h2, h3;
341 :root { --color: red; }
342 */
343 importFrom: 'path/to/file.css'
344});
345```
346
347Multiple sources can be passed into this option, and they will be parsed in the
348order they are received. JavaScript files, JSON files, functions, and objects
349will use different namespaces to import different kinds of variables.
350
351```js
352postcssPresetEnv({
353 importFrom: [
354 /*
355 @custom-media --small-viewport (max-width: 30em);
356 @custom-selector :--heading h1, h2, h3;
357 :root { --color: red; }
358 */
359 'path/to/file.css',
360
361 /* module.exports = {
362 customMedia: { '--small-viewport': '(max-width: 30em)' },
363 customProperties: { '--color': 'red' },
364 customSelectors: { ':--heading': 'h1, h2, h3' },
365 environmentVariables: { '--branding-padding': '20px' }
366 } */
367 'and/then/this.js',
368
369 /* {
370 "custom-media": { "--small-viewport": "(max-width: 30em)" }
371 "custom-properties": { "--color": "red" },
372 "custom-selectors": { ":--heading": "h1, h2, h3" },
373 "environment-variables": { "--branding-padding": "20px" }
374 } */
375 'and/then/that.json',
376
377 {
378 customMedia: { '--small-viewport': '(max-width: 30em)' },
379 customProperties: { '--color': 'red' },
380 customSelectors: { ':--heading': 'h1, h2, h3' },
381 environmentVariables: { '--branding-padding': '20px' }
382 },
383 () => {
384 const customMedia = { '--small-viewport': '(max-width: 30em)' };
385 const customProperties = { '--color': 'red' };
386 const customSelectors = { ':--heading': 'h1, h2, h3' };
387 const environmentVariables = { '--branding-padding': '20px' };
388
389 return { customMedia, customProperties, customSelectors, environmentVariables };
390 }
391 ]
392});
393```
394
395### exportTo
396
397The `exportTo` option specifies destinations where variables like Custom Media,
398Custom Properties, Custom Selectors, and Environment Variables can be exported
399to, which might be CSS, JS, and JSON files, functions, and directly passed
400objects.
401
402```js
403postcssPresetEnv({
404 /*
405 @custom-media --small-viewport (max-width: 30em);
406 @custom-selector :--heading h1, h2, h3;
407 :root { --color: red; }
408 */
409 exportTo: 'path/to/file.css'
410});
411```
412
413Multiple destinations can be passed into this option as well, and they will be
414parsed in the order they are received. JavaScript files, JSON files, and
415objects will use different namespaces to import different kinds of variables.
416
417```js
418const cachedObject = {};
419
420postcssPresetEnv({
421 exportTo: [
422 /*
423 @custom-media --small-viewport (max-width: 30em);
424 @custom-selector :--heading h1, h2, h3;
425 :root { --color: red; }
426 */
427 'path/to/file.css',
428
429 /* module.exports = {
430 customMedia: { '--small-viewport': '(max-width: 30em)' },
431 customProperties: { '--color': 'red' },
432 customSelectors: { ':--heading': 'h1, h2, h3' },
433 environmentVariables: { '--branding-padding': '20px' }
434 } */
435 'and/then/this.js',
436
437 /* {
438 "custom-media": { "--small-viewport": "(max-width: 30em)" }
439 "custom-properties": { "--color": "red" },
440 "custom-selectors": { ":--heading": "h1, h2, h3" },
441 "environment-variables": { "--branding-padding": "20px" }
442 } */
443 'and/then/that.json',
444
445 cachedObject,
446 variables => {
447 if ('customProperties' in variables) {
448 // do something special with customProperties
449 }
450
451 Object.assign(cachedObject, variables);
452 }
453 ]
454});
455```
456
457### debug
458
459The `debug` option enables debugging messages to stdout which should be useful to help you debug which features have been enabled/disabled and why.
460
461### enableClientSidePolyfills
462
463The `enableClientSidePolyfills` enables any feature that would need an extra browser library to be loaded into the page for it to work. Defaults to `true`.
464
465Note that manually enabling/disabling features via the "feature" option overrides this flag.
466
467## Stability and Portability
468
469[PostCSS Preset Env] will often include very modern CSS features that are not fully ready yet.
470This gives users the chance to play around with these features and provide feedback.
471
472If the specification changes or is abandoned a new major version of the plugin will be released.
473This will require you to update your source code so that everything works as expected.
474
475To have more stability between updates of [PostCSS Preset Env] you may set `stage: 3` and/or `minimumVendorImplementations: 2`.
476
477A side effect of staying close to the standard is that you can more easily migrate your project to other tooling all together.
478
479## Plugins list
480
481### Plugins that need client library
482
483This is the current list of features that need a client library with a link
484to the polyfill's library.
485
486* `blank-pseudo-class`: [Plugin](https://github.com/csstools/postcss-plugins/blob/main/plugins/css-blank-pseudo) / [Polyfill](https://github.com/csstools/postcss-plugins/blob/main/plugins/css-blank-pseudo/README-BROWSER.md)
487* `focus-visible-pseudo-class`: [Plugin](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-focus-visible) / [Polyfill](https://github.com/WICG/focus-visible)
488* `focus-within-pseudo-class`: [Plugin](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-focus-within) / [Library](https://github.com/jsxtools/focus-within) / [Polyfill](https://github.com/jsxtools/focus-within/blob/master/README-BROWSER.md)
489* `has-pseudo-class`: [Plugin](https://github.com/csstools/postcss-plugins/blob/main/plugins/css-has-pseudo) / [Polyfill](https://github.com/csstools/postcss-plugins/blob/main/plugins/css-has-pseudo/README-BROWSER.md)
490* `prefers-color-scheme-query`: [Plugin](https://github.com/csstools/postcss-plugins/blob/main/plugins/css-prefers-color-scheme) / [Polyfill](https://github.com/csstools/postcss-plugins/blob/main/plugins/css-prefers-color-scheme/README-BROWSER.md)
491
492If you want to disable these types of features, please check the [`enableClientSidePolyfills` option](#enableclientsidepolyfills).
493
494### Plugins not affected by Browser Support
495
496Given they have no support they will always be enabled if they match by Stage:
497
498* `blank-pseudo-class`: [Plugin](https://github.com/csstools/postcss-plugins/blob/main/plugins/css-blank-pseudo) / [Polyfill](https://github.com/csstools/postcss-plugins/blob/main/plugins/css-blank-pseudo/README-BROWSER.md)
499* `custom-media-queries`: [Plugin](https://github.com/postcss/postcss-custom-media)
500* `has-pseudo-class`: [Plugin](https://github.com/csstools/postcss-plugins/blob/main/plugins/css-has-pseudo) / [Polyfill](https://github.com/csstools/postcss-plugins/blob/main/plugins/css-has-pseudo/README-BROWSER.md)
501* `image-set-function`: [Plugin](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-image-set-function)
502* `media-query-ranges`: [Plugin](https://github.com/postcss/postcss-media-minmax)
503* `nesting-rules`: [Plugin](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-nesting)
504
505[cli-img]: https://github.com/csstools/postcss-plugins/workflows/test/badge.svg
506[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
507[discord]: https://discord.gg/bUadyRwkJS
508[npm-img]: https://img.shields.io/npm/v/postcss-preset-env.svg
509[npm-url]: https://www.npmjs.com/package/postcss-preset-env
510[package-phobia-badge]: https://packagephobia.com/badge?p=postcss-preset-env
511[package-phobia]: https://packagephobia.com/result?p=postcss-preset-env
512
513[autoprefixer]: https://github.com/postcss/autoprefixer
514[browserslist]: https://github.com/browserslist/browserslist#readme
515[caniuse]: https://caniuse.com/
516[cssdb]: https://cssdb.org/
517[PostCSS]: https://github.com/postcss/postcss
518[PostCSS Preset Env]: https://github.com/csstools/postcss-plugins/tree/main/plugin-packs/postcss-preset-env
519[readme-style-with-preset-env-img]: https://csstools.github.io/postcss-preset-env/readme-style-with-preset-env.svg
520[readme-style-with-preset-env-url]: https://codepen.io/pen?template=OZRovK
521[readme-transform-with-preset-env-img]: https://csstools.github.io/postcss-preset-env/readme-transform-with-preset-env.svg
522[readme-transform-with-preset-env-url]: https://csstools.github.io/postcss-preset-env/
Note: See TracBrowser for help on using the repository browser.