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.

193 lines
4.9 KiB

  1. # PostCSS Custom Properties [<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. [![CSS Standard Status][css-img]][css-url]
  4. [![Build Status][cli-img]][cli-url]
  5. [![Support Chat][git-img]][git-url]
  6. [PostCSS Custom Properties] lets you use Custom Properties in CSS, following
  7. the [CSS Custom Properties] specification.
  8. ```pcss
  9. :root {
  10. --color: red;
  11. }
  12. h1 {
  13. color: var(--color);
  14. }
  15. /* becomes */
  16. :root {
  17. --color: red;
  18. }
  19. h1 {
  20. color: red;
  21. color: var(--color);
  22. }
  23. ```
  24. ## Usage
  25. Add [PostCSS Custom Properties] to your project:
  26. ```bash
  27. npm install postcss-custom-properties --save-dev
  28. ```
  29. Use [PostCSS Custom Properties] to process your CSS:
  30. ```js
  31. const postcssCustomProperties = require('postcss-custom-properties');
  32. postcssCustomProperties.process(YOUR_CSS /*, processOptions, pluginOptions */);
  33. ```
  34. Or use it as a [PostCSS] plugin:
  35. ```js
  36. const postcss = require('postcss');
  37. const postcssCustomProperties = require('postcss-custom-properties');
  38. postcss([
  39. postcssCustomProperties(/* pluginOptions */)
  40. ]).process(YOUR_CSS /*, processOptions */);
  41. ```
  42. [PostCSS Custom Properties] runs in all Node environments, with special instructions for:
  43. | [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) |
  44. | --- | --- | --- | --- | --- | --- |
  45. ## Options
  46. ### preserve
  47. The `preserve` option determines whether Custom Properties and properties using
  48. custom properties should be preserved in their original form. By default, both
  49. of these are preserved.
  50. ```js
  51. postcssCustomProperties({
  52. preserve: false
  53. });
  54. ```
  55. ```pcss
  56. :root {
  57. --color: red;
  58. }
  59. h1 {
  60. color: var(--color);
  61. }
  62. /* becomes */
  63. h1 {
  64. color: red;
  65. }
  66. ```
  67. ### importFrom
  68. The `importFrom` option specifies sources where Custom Properties can be imported
  69. from, which might be CSS, JS, and JSON files, functions, and directly passed
  70. objects.
  71. ```js
  72. postcssCustomProperties({
  73. importFrom: 'path/to/file.css' // => :root { --color: red }
  74. });
  75. ```
  76. ```pcss
  77. h1 {
  78. color: var(--color);
  79. }
  80. /* becomes */
  81. h1 {
  82. color: red;
  83. }
  84. ```
  85. Multiple sources can be passed into this option, and they will be parsed in the
  86. order they are received. JavaScript files, JSON files, functions, and objects
  87. will need to namespace Custom Properties using the `customProperties` or
  88. `custom-properties` key.
  89. ```js
  90. postcssCustomProperties({
  91. importFrom: [
  92. 'path/to/file.css', // :root { --color: red; }
  93. 'and/then/this.js', // module.exports = { customProperties: { '--color': 'red' } }
  94. 'and/then/that.json', // { "custom-properties": { "--color": "red" } }
  95. {
  96. customProperties: { '--color': 'red' }
  97. },
  98. () => {
  99. const customProperties = { '--color': 'red' };
  100. return { customProperties };
  101. }
  102. ]
  103. });
  104. ```
  105. See example imports written in [CSS](test/import-properties.css),
  106. [JS](test/import-properties.js), and [JSON](test/import-properties.json).
  107. ### exportTo
  108. The `exportTo` option specifies destinations where Custom Properties can be exported
  109. to, which might be CSS, JS, and JSON files, functions, and directly passed
  110. objects.
  111. ```js
  112. postcssCustomProperties({
  113. exportTo: 'path/to/file.css' // :root { --color: red; }
  114. });
  115. ```
  116. Multiple destinations can be passed into this option, and they will be parsed
  117. in the order they are received. JavaScript files, JSON files, and objects will
  118. need to namespace Custom Properties using the `customProperties` or
  119. `custom-properties` key.
  120. ```js
  121. const cachedObject = { customProperties: {} };
  122. postcssCustomProperties({
  123. exportTo: [
  124. 'path/to/file.css', // :root { --color: red; }
  125. 'and/then/this.js', // module.exports = { customProperties: { '--color': 'red' } }
  126. 'and/then/this.mjs', // export const customProperties = { '--color': 'red' } }
  127. 'and/then/that.json', // { "custom-properties": { "--color": "red" } }
  128. cachedObject,
  129. customProperties => {
  130. customProperties // { '--color': 'red' }
  131. }
  132. ]
  133. });
  134. ```
  135. See example exports written to [CSS](test/export-properties.css),
  136. [JS](test/export-properties.js), [MJS](test/export-properties.mjs), and
  137. [JSON](test/export-properties.json).
  138. [cli-img]: https://img.shields.io/travis/postcss/postcss-custom-properties/master.svg
  139. [cli-url]: https://travis-ci.org/postcss/postcss-custom-properties
  140. [css-img]: https://cssdb.org/badge/custom-properties.svg
  141. [css-url]: https://cssdb.org/#custom-properties
  142. [git-img]: https://img.shields.io/badge/support-chat-blue.svg
  143. [git-url]: https://gitter.im/postcss/postcss
  144. [npm-img]: https://img.shields.io/npm/v/postcss-custom-properties.svg
  145. [npm-url]: https://www.npmjs.com/package/postcss-custom-properties
  146. [CSS Custom Properties]: https://www.w3.org/TR/css-variables-1/
  147. [PostCSS]: https://github.com/postcss/postcss
  148. [PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties