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.

137 lines
3.7 KiB

  1. # PostCSS Environment Variables [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" 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 Environment Variables] lets you use `env()` variables in CSS,
  7. following the [CSS Environment Variables] specification.
  8. ```pcss
  9. @media (max-width: env(--branding-small)) {
  10. body {
  11. padding: env(--branding-padding);
  12. }
  13. }
  14. /* becomes */
  15. @media (min-width: 600px) {
  16. body {
  17. padding: 20px;
  18. }
  19. }
  20. /* when the `importFrom` option is: {
  21. "environmentVariables": {
  22. "--branding-small": "600px",
  23. "--branding-padding": "20px"
  24. }
  25. } */
  26. ```
  27. ## Usage
  28. Add [PostCSS Environment Variables] to your project:
  29. ```bash
  30. npm install postcss-env-function --save-dev
  31. ```
  32. Use [PostCSS Environment Variables] to process your CSS:
  33. ```js
  34. const postcssEnvFunction = require('postcss-env-function');
  35. postcssEnvFunction.process(YOUR_CSS /*, processOptions, pluginOptions */);
  36. ```
  37. Or use it as a [PostCSS] plugin:
  38. ```js
  39. const postcss = require('postcss');
  40. const postcssEnvFunction = require('postcss-env-function');
  41. postcss([
  42. postcssEnvFunction(/* pluginOptions */)
  43. ]).process(YOUR_CSS /*, processOptions */);
  44. ```
  45. [PostCSS Environment Variables] runs in all Node environments, with special instructions for:
  46. | [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) |
  47. | --- | --- | --- | --- | --- | --- |
  48. ## Options
  49. ### importFrom
  50. The `importFrom` option specifies sources where Environment Variables can be
  51. imported from, which might be JS and JSON files, functions, and directly passed
  52. objects.
  53. ```js
  54. postcssCustomProperties({
  55. importFrom: 'path/to/file.js' /* module.exports = {
  56. environmentVariables: {
  57. '--branding-padding': '20px',
  58. '--branding-small': '600px'
  59. }
  60. } */
  61. });
  62. ```
  63. ```pcss
  64. @media (max-width: env(--branding-small)) {
  65. body {
  66. padding: env(--branding-padding);
  67. }
  68. }
  69. /* becomes */
  70. @media (min-width: 600px) {
  71. body {
  72. padding: 20px;
  73. }
  74. }
  75. ```
  76. Multiple sources can be passed into this option, and they will be parsed in the
  77. order they are received. JavaScript files, JSON files, functions, and objects
  78. will need to namespace Custom Properties using the `environmentVariables` or
  79. `variables-variables` key.
  80. ```js
  81. postcssCustomProperties({
  82. importFrom: [
  83. 'path/to/file.js', // module.exports = { environmentVariables: { '--branding-padding': '20px' } }
  84. 'and/then/this.json', // { "environment-variables": { "--branding-padding": "20px" } }
  85. {
  86. environmentVariables: { '--branding-padding': '20px' }
  87. },
  88. () => {
  89. const environmentVariables = { '--branding-padding': '20px' };
  90. return { environmentVariables };
  91. }
  92. ]
  93. });
  94. ```
  95. See example imports written in [JS](test/import-variables.js) and
  96. [JSON](test/import-variables.json).
  97. [cli-img]: https://img.shields.io/travis/jonathantneal/postcss-env-function.svg
  98. [cli-url]: https://travis-ci.org/jonathantneal/postcss-env-function
  99. [css-img]: https://cssdb.org/badge/environment-variables.svg
  100. [css-url]: https://cssdb.org/#environment-variables
  101. [git-img]: https://img.shields.io/badge/support-chat-blue.svg
  102. [git-url]: https://gitter.im/postcss/postcss
  103. [npm-img]: https://img.shields.io/npm/v/postcss-env-function.svg
  104. [npm-url]: https://www.npmjs.com/package/postcss-env-function
  105. [CSS Environment Variables]: https://drafts.csswg.org/css-env-1/
  106. [PostCSS]: https://github.com/postcss/postcss
  107. [PostCSS Environment Variables]: https://github.com/jonathantneal/postcss-env-function