web 3d图形渲染器
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.

381 lines
10 KiB

  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. [![Support Chat][git-img]][git-url]
  5. [PostCSS Preset Env] lets you convert modern CSS into something most browsers
  6. can understand, determining the polyfills you need based on your targeted
  7. browsers or runtime environments.
  8. ```bash
  9. npm install postcss-preset-env
  10. ```
  11. ```pcss
  12. @custom-media --viewport-medium (width <= 50rem);
  13. @custom-selector :--heading h1, h2, h3, h4, h5, h6;
  14. :root {
  15. --mainColor: #12345678;
  16. }
  17. body {
  18. color: var(--mainColor);
  19. font-family: system-ui;
  20. overflow-wrap: break-word;
  21. }
  22. :--heading {
  23. background-image: image-set(url(img/heading.png) 1x, url(img/heading@2x.png) 2x);
  24. @media (--viewport-medium) {
  25. margin-block: 0;
  26. }
  27. }
  28. a {
  29. color: rgb(0 0 100% / 90%);
  30. &:hover {
  31. color: rebeccapurple;
  32. }
  33. }
  34. /* becomes */
  35. :root {
  36. --mainColor: rgba(18, 52, 86, 0.47059);
  37. }
  38. body {
  39. color: rgba(18, 52, 86, 0.47059);
  40. color: var(--mainColor);
  41. font-family: system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Droid Sans, Helvetica Neue;
  42. word-wrap: break-word;
  43. }
  44. h1, h2, h3, h4, h5, h6 {
  45. background-image: url(img/heading.png);
  46. }
  47. @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  48. h1, h2, h3, h4, h5, h6 {
  49. background-image: url(img/heading@2x.png)
  50. }
  51. }
  52. @media (max-width: 50rem) {
  53. h1, h2, h3, h4, h5, h6 {
  54. margin-top: 0;
  55. margin-bottom: 0;
  56. }
  57. }
  58. a {
  59. color: rgba(0, 0, 255, 0.9)
  60. }
  61. a:hover {
  62. color: #639;
  63. }
  64. ```
  65. Without any configuration options, [PostCSS Preset Env] enables **Stage 2**
  66. features and supports **all** browsers.
  67. [![Transform with Preset Env][readme-transform-with-preset-env-img]][readme-transform-with-preset-env-url]
  68. [![Style with Preset Env][readme-style-with-preset-env-img]][readme-style-with-preset-env-url]
  69. ## Usage
  70. Add [PostCSS Preset Env] to your project:
  71. ```bash
  72. npm install postcss-preset-env --save-dev
  73. ```
  74. Use [PostCSS Preset Env] to process your CSS:
  75. ```js
  76. const postcssPresetEnv = require('postcss-preset-env');
  77. postcssPresetEnv.process(YOUR_CSS /*, processOptions, pluginOptions */);
  78. ```
  79. Or use it as a [PostCSS] plugin:
  80. ```js
  81. const postcss = require('postcss');
  82. const postcssPresetEnv = require('postcss-preset-env');
  83. postcss([
  84. postcssPresetEnv(/* pluginOptions */)
  85. ]).process(YOUR_CSS /*, processOptions */);
  86. ```
  87. [PostCSS Preset Env] runs in all Node environments, with special instructions for:
  88. | [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) |
  89. | --- | --- | --- | --- | --- | --- |
  90. ## Options
  91. ### stage
  92. The `stage` option determines which CSS features to polyfill, based upon their
  93. stability in the process of becoming implemented web standards.
  94. ```js
  95. postcssPresetEnv({ stage: 0 })
  96. ```
  97. The `stage` can be `0` (experimental) through `4` (stable), or `false`. Setting
  98. `stage` to `false` will disable every polyfill. Doing this would only be useful
  99. if you intended to exclusively use the [`features`](#features) option.
  100. Without any configuration options, [PostCSS Preset Env] enables **Stage 2**
  101. features.
  102. ### features
  103. The `features` option enables or disables specific polyfills by ID. Passing
  104. `true` to a specific feature ID will enable its polyfill, while passing `false`
  105. will disable it.
  106. ```js
  107. postcssPresetEnv({
  108. /* use stage 3 features + css nesting rules */
  109. stage: 3,
  110. features: {
  111. 'nesting-rules': true
  112. }
  113. })
  114. ```
  115. Passing an object to a specific feature ID will both enable and configure it.
  116. ```js
  117. postcssPresetEnv({
  118. /* use stage 3 features + css color-mod (warning on unresolved) */
  119. stage: 3,
  120. features: {
  121. 'color-mod-function': { unresolved: 'warn' }
  122. }
  123. })
  124. ```
  125. Any polyfills not explicitly enabled or disabled through `features` are
  126. determined by the [`stage`](#stage) option.
  127. ### browsers
  128. The `browsers` option determines which polyfills are required based upon the
  129. browsers you are supporting.
  130. [PostCSS Preset Env] supports any standard [browserslist] configuration, which
  131. can be a `.browserslistrc` file, a `browserslist` key in `package.json`, or
  132. `browserslist` environment variables.
  133. The `browsers` option should only be used when a standard browserslist
  134. configuration is not available.
  135. ```js
  136. postcssPresetEnv({ browsers: 'last 2 versions' })
  137. ```
  138. If not valid browserslist configuration is specified, the
  139. [default browserslist query](https://github.com/browserslist/browserslist#queries)
  140. will be used.
  141. ### insertBefore / insertAfter
  142. The `insertBefore` and `insertAfter` keys allow you to insert other PostCSS
  143. plugins into the chain. This is only useful if you are also using sugary
  144. PostCSS plugins that must execute before or after certain polyfills.
  145. Both `insertBefore` and `insertAfter` support chaining one or multiple plugins.
  146. ```js
  147. import postcssSimpleVars from 'postcss-simple-vars';
  148. postcssPresetEnv({
  149. insertBefore: {
  150. 'all-property': postcssSimpleVars
  151. }
  152. })
  153. ```
  154. ### autoprefixer
  155. [PostCSS Preset Env] includes [autoprefixer] and [`browsers`](#browsers) option
  156. will be passed to it automatically.
  157. Specifying the `autoprefixer` option enables passing
  158. [additional options](https://github.com/postcss/autoprefixer#options)
  159. into [autoprefixer].
  160. ```js
  161. postcssPresetEnv({
  162. autoprefixer: { grid: true }
  163. })
  164. ```
  165. Passing `autoprefixer: false` disables autoprefixer.
  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/master.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. [autoprefixer]: https://github.com/postcss/autoprefixer
  291. [browserslist]: https://github.com/browserslist/browserslist#readme
  292. [caniuse]: https://caniuse.com/
  293. [cssdb]: https://cssdb.org/
  294. [PostCSS]: https://github.com/postcss/postcss
  295. [PostCSS Preset Env]: https://github.com/csstools/postcss-preset-env
  296. [readme-style-with-preset-env-img]: https://csstools.github.io/postcss-preset-env/readme-style-with-preset-env.svg
  297. [readme-style-with-preset-env-url]: https://codepen.io/pen?template=OZRovK
  298. [readme-transform-with-preset-env-img]: https://csstools.github.io/postcss-preset-env/readme-transform-with-preset-env.svg
  299. [readme-transform-with-preset-env-url]: https://csstools.github.io/postcss-preset-env/