Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

3 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. # PostCSS Preset Env [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][postcss]
  2. [![NPM Version][npm-img]][npm-url]
  3. [![Build Status][cli-img]][cli-url]
  4. [![Windows Build Status][win-img]][win-url]
  5. [![Support Chat][git-img]][git-url]
  6. [PostCSS Preset Env] lets you convert modern CSS into something most browsers
  7. can understand, determining the polyfills you need based on your targeted
  8. browsers or runtime environments.
  9. ```bash
  10. npm install postcss-preset-env
  11. ```
  12. ```pcss
  13. @custom-media --viewport-medium (width <= 50rem);
  14. @custom-selector :--heading h1, h2, h3, h4, h5, h6;
  15. :root {
  16. --mainColor: #12345678;
  17. }
  18. body {
  19. color: var(--mainColor);
  20. font-family: system-ui;
  21. overflow-wrap: break-word;
  22. }
  23. :--heading {
  24. background-image: image-set(url(img/heading.png) 1x, url(img/heading@2x.png) 2x);
  25. @media (--viewport-medium) {
  26. margin-block: 0;
  27. }
  28. }
  29. a {
  30. color: rgb(0 0 100% / 90%);
  31. &:hover {
  32. color: rebeccapurple;
  33. }
  34. }
  35. /* becomes */
  36. :root {
  37. --mainColor: rgba(18, 52, 86, 0.47059);
  38. }
  39. body {
  40. color: rgba(18, 52, 86, 0.47059);
  41. color: var(--mainColor);
  42. font-family: system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Droid Sans, Helvetica Neue;
  43. word-wrap: break-word;
  44. }
  45. h1, h2, h3, h4, h5, h6 {
  46. background-image: url(img/heading.png);
  47. }
  48. @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  49. h1, h2, h3, h4, h5, h6 {
  50. background-image: url(img/heading@2x.png)
  51. }
  52. }
  53. @media (max-width: 50rem) {
  54. h1, h2, h3, h4, h5, h6 {
  55. margin-top: 0;
  56. margin-bottom: 0;
  57. }
  58. }
  59. a {
  60. color: rgba(0, 0, 255, 0.9)
  61. }
  62. a:hover {
  63. color: #639;
  64. }
  65. ```
  66. Without any configuration options, [PostCSS Preset Env] enables **Stage 2**
  67. features and supports **all** browsers.
  68. [![Transform with Preset Env][readme-transform-with-preset-env-img]][readme-transform-with-preset-env-url]
  69. [![Style with Preset Env][readme-style-with-preset-env-img]][readme-style-with-preset-env-url]
  70. ## Usage
  71. Add [PostCSS Preset Env] to your project:
  72. ```bash
  73. npm install postcss-preset-env --save-dev
  74. ```
  75. Use [PostCSS Preset Env] to process your CSS:
  76. ```js
  77. const postcssPresetEnv = require('postcss-preset-env');
  78. postcssPresetEnv.process(YOUR_CSS /*, processOptions, pluginOptions */);
  79. ```
  80. Or use it as a [PostCSS] plugin:
  81. ```js
  82. const postcss = require('postcss');
  83. const postcssPresetEnv = require('postcss-preset-env');
  84. postcss([
  85. postcssPresetEnv(/* pluginOptions */)
  86. ]).process(YOUR_CSS /*, processOptions */);
  87. ```
  88. [PostCSS Preset Env] runs in all Node environments, with special instructions for:
  89. | [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) |
  90. | --- | --- | --- | --- | --- | --- |
  91. ## Options
  92. ### stage
  93. The `stage` option determines which CSS features to polyfill, based upon their
  94. stability in the process of becoming implemented web standards.
  95. ```js
  96. postcssPresetEnv({ stage: 0 })
  97. ```
  98. The `stage` can be `0` (experimental) through `4` (stable), or `false`. Setting
  99. `stage` to `false` will disable every polyfill. Doing this would only be useful
  100. if you intended to exclusively use the [`features`](#features) option.
  101. Without any configuration options, [PostCSS Preset Env] enables **Stage 2**
  102. features.
  103. ### features
  104. The `features` option enables or disables specific polyfills by ID. Passing
  105. `true` to a specific feature ID will enable its polyfill, while passing `false`
  106. will disable it.
  107. ```js
  108. postcssPresetEnv({
  109. /* use stage 3 features + css nesting rules */
  110. stage: 3,
  111. features: {
  112. 'nesting-rules': true
  113. }
  114. })
  115. ```
  116. Passing an object to a specific feature ID will both enable and configure it.
  117. ```js
  118. postcssPresetEnv({
  119. /* use stage 3 features + css color-mod (warning on unresolved) */
  120. stage: 3,
  121. features: {
  122. 'color-mod-function': { unresolved: 'warn' }
  123. }
  124. })
  125. ```
  126. Any polyfills not explicitly enabled or disabled through `features` are
  127. determined by the [`stage`](#stage) option.
  128. ### browsers
  129. The `browsers` option determines which polyfills are required based upon the
  130. browsers you are supporting.
  131. [PostCSS Preset Env] supports any standard [browserslist] configuration, which
  132. can be a `.browserslistrc` file, a `browserslist` key in `package.json`, or
  133. `browserslist` environment variables.
  134. The `browsers` option should only be used when a standard browserslist
  135. configuration is not available.
  136. ```js
  137. postcssPresetEnv({ browsers: 'last 2 versions' })
  138. ```
  139. If not valid browserslist configuration is specified, the
  140. [default browserslist query](https://github.com/browserslist/browserslist#queries)
  141. will be used.
  142. ### insertBefore / insertAfter
  143. The `insertBefore` and `insertAfter` keys allow you to insert other PostCSS
  144. plugins into the chain. This is only useful if you are also using sugary
  145. PostCSS plugins that must execute before or after certain polyfills.
  146. Both `insertBefore` and `insertAfter` support chaining one or multiple plugins.
  147. ```js
  148. import postcssSimpleVars from 'postcss-simple-vars';
  149. postcssPresetEnv({
  150. insertBefore: {
  151. 'all-property': postcssSimpleVars
  152. }
  153. })
  154. ```
  155. ### autoprefixer
  156. [PostCSS Preset Env] includes [autoprefixer] and [`browsers`](#browsers) option
  157. will be passed to it automatically.
  158. Specifying `autoprefixer` option enables passing
  159. [additional options](https://github.com/postcss/autoprefixer#options)
  160. into [autoprefixer].
  161. ```js
  162. postcssPresetEnv({
  163. autoprefixer: { grid: true }
  164. })
  165. ```
  166. ### preserve
  167. The `preserve` option determines whether all plugins should receive a
  168. `preserve` option, which may preserve or remove otherwise-polyfilled CSS. By
  169. default, this option is not configured.
  170. ```js
  171. postcssPresetEnv({
  172. preserve: false // instruct all plugins to omit pre-polyfilled CSS
  173. });
  174. ```
  175. ### importFrom
  176. The `importFrom` option specifies sources where variables like Custom Media,
  177. Custom Properties, Custom Selectors, and Environment Variables can be imported
  178. from, which might be CSS, JS, and JSON files, functions, and directly passed
  179. objects.
  180. ```js
  181. postcssPresetEnv({
  182. /*
  183. @custom-media --small-viewport (max-width: 30em);
  184. @custom-selector :--heading h1, h2, h3;
  185. :root { --color: red; }
  186. */
  187. importFrom: 'path/to/file.css'
  188. });
  189. ```
  190. Multiple sources can be passed into this option, and they will be parsed in the
  191. order they are received. JavaScript files, JSON files, functions, and objects
  192. will use different namespaces to import different kinds of variables.
  193. ```js
  194. postcssPresetEnv({
  195. importFrom: [
  196. /*
  197. @custom-media --small-viewport (max-width: 30em);
  198. @custom-selector :--heading h1, h2, h3;
  199. :root { --color: red; }
  200. */
  201. 'path/to/file.css',
  202. /* module.exports = {
  203. customMedia: { '--small-viewport': '(max-width: 30em)' },
  204. customProperties: { '--color': 'red' },
  205. customSelectors: { ':--heading': 'h1, h2, h3' },
  206. environmentVariables: { '--branding-padding': '20px' }
  207. } */
  208. 'and/then/this.js',
  209. /* {
  210. "custom-media": { "--small-viewport": "(max-width: 30em)" }
  211. "custom-properties": { "--color": "red" },
  212. "custom-selectors": { ":--heading": "h1, h2, h3" },
  213. "environment-variables": { "--branding-padding": "20px" }
  214. } */
  215. 'and/then/that.json',
  216. {
  217. customMedia: { '--small-viewport': '(max-width: 30em)' },
  218. customProperties: { '--color': 'red' },
  219. customSelectors: { ':--heading': 'h1, h2, h3' },
  220. environmentVariables: { '--branding-padding': '20px' }
  221. },
  222. () => {
  223. const customMedia = { '--small-viewport': '(max-width: 30em)' };
  224. const customProperties = { '--color': 'red' };
  225. const customSelectors = { ':--heading': 'h1, h2, h3' };
  226. const environmentVariables = { '--branding-padding': '20px' };
  227. return { customMedia, customProperties, customSelectors, environmentVariables };
  228. }
  229. ]
  230. });
  231. ```
  232. ### exportTo
  233. The `exportTo` option specifies destinations where variables like Custom Media,
  234. Custom Properties, Custom Selectors, and Environment Variables can be exported
  235. to, which might be CSS, JS, and JSON files, functions, and directly passed
  236. objects.
  237. ```js
  238. postcssPresetEnv({
  239. /*
  240. @custom-media --small-viewport (max-width: 30em);
  241. @custom-selector :--heading h1, h2, h3;
  242. :root { --color: red; }
  243. */
  244. exportTo: 'path/to/file.css'
  245. });
  246. ```
  247. Multiple destinations can be passed into this option as well, and they will be
  248. parsed in the order they are received. JavaScript files, JSON files, and
  249. objects will use different namespaces to import different kinds of variables.
  250. ```js
  251. const cachedObject = {};
  252. postcssPresetEnv({
  253. exportTo: [
  254. /*
  255. @custom-media --small-viewport (max-width: 30em);
  256. @custom-selector :--heading h1, h2, h3;
  257. :root { --color: red; }
  258. */
  259. 'path/to/file.css',
  260. /* module.exports = {
  261. customMedia: { '--small-viewport': '(max-width: 30em)' },
  262. customProperties: { '--color': 'red' },
  263. customSelectors: { ':--heading': 'h1, h2, h3' },
  264. environmentVariables: { '--branding-padding': '20px' }
  265. } */
  266. 'and/then/this.js',
  267. /* {
  268. "custom-media": { "--small-viewport": "(max-width: 30em)" }
  269. "custom-properties": { "--color": "red" },
  270. "custom-selectors": { ":--heading": "h1, h2, h3" },
  271. "environment-variables": { "--branding-padding": "20px" }
  272. } */
  273. 'and/then/that.json',
  274. cachedObject,
  275. variables => {
  276. if ('customProperties' in variables) {
  277. // do something special with customProperties
  278. }
  279. Object.assign(cachedObject, variables);
  280. }
  281. ]
  282. });
  283. ```
  284. [cli-img]: https://img.shields.io/travis/csstools/postcss-preset-env.svg
  285. [cli-url]: https://travis-ci.org/csstools/postcss-preset-env
  286. [git-img]: https://img.shields.io/badge/support-chat-blue.svg
  287. [git-url]: https://gitter.im/postcss/postcss
  288. [npm-img]: https://img.shields.io/npm/v/postcss-preset-env.svg
  289. [npm-url]: https://www.npmjs.com/package/postcss-preset-env
  290. [win-img]: https://img.shields.io/appveyor/ci/jonathantneal/postcss-preset-env.svg
  291. [win-url]: https://ci.appveyor.com/project/jonathantneal/postcss-preset-env
  292. [autoprefixer]: https://github.com/postcss/autoprefixer
  293. [browserslist]: https://github.com/browserslist/browserslist#readme
  294. [caniuse]: https://caniuse.com/
  295. [cssdb]: https://cssdb.org/
  296. [PostCSS]: https://github.com/postcss/postcss
  297. [PostCSS Preset Env]: https://github.com/csstools/postcss-preset-env
  298. [readme-style-with-preset-env-img]: https://csstools.github.io/postcss-preset-env/readme-style-with-preset-env.svg
  299. [readme-style-with-preset-env-url]: https://codepen.io/pen?template=OZRovK
  300. [readme-transform-with-preset-env-img]: https://csstools.github.io/postcss-preset-env/readme-transform-with-preset-env.svg
  301. [readme-transform-with-preset-env-url]: https://csstools.github.io/postcss-preset-env/