What is postcss-env-function?
The postcss-env-function package is a PostCSS plugin that allows you to use environment variables in your CSS. This can be particularly useful for setting values that may change depending on the environment, such as colors, sizes, or other design tokens.
What are postcss-env-function's main functionalities?
Using environment variables in CSS
This feature allows you to define CSS variables that can be set using environment variables. The `env()` function takes the name of the environment variable and an optional fallback value.
/* CSS */
:root {
--main-bg-color: env(MAIN_BG_COLOR, #fff);
}
body {
background-color: var(--main-bg-color);
}
/* JavaScript to set environment variable */
process.env.MAIN_BG_COLOR = '#f0f0f0';
Fallback values
You can provide fallback values for environment variables in case they are not set. This ensures that your CSS will still have a valid value even if the environment variable is missing.
/* CSS */
:root {
--main-text-color: env(MAIN_TEXT_COLOR, #000);
}
p {
color: var(--main-text-color);
}
/* JavaScript to set environment variable */
process.env.MAIN_TEXT_COLOR = '#333';
Other packages similar to postcss-env-function
postcss-custom-properties
The postcss-custom-properties package allows you to use CSS custom properties (variables) in a way that is compatible with older browsers. Unlike postcss-env-function, it does not directly support environment variables but focuses on transforming CSS custom properties to static values.
postcss-preset-env
The postcss-preset-env package lets you convert modern CSS into something most browsers can understand, determining the polyfills you need based on your targeted browsers or runtime environments. It includes features like custom properties, but does not specifically handle environment variables like postcss-env-function.
postcss-simple-vars
The postcss-simple-vars package allows you to use Sass-like variables in your CSS. It is simpler and more limited compared to postcss-env-function, as it does not support environment variables and focuses on static variable definitions.
PostCSS Environment Variables
PostCSS Environment Variables lets you use env()
variables in CSS, following the CSS Environment Variables specification.
⚠️ Custom Environment Variables were never defined in a specification and we are no longer including this in postcss-preset-env
.
In the future there might be renewed interest in Custom Environment Variables.
We advice users of this plugin to seek alternatives such as postcss-design-tokens
@media (max-width: env(--branding-small)) {
body {
padding: env(--branding-padding);
}
}
/* becomes */
@media (min-width: 600px) {
body {
padding: 20px;
}
}
/* when the `importFrom` option is: {
"environmentVariables": {
"--branding-small": "600px",
"--branding-padding": "20px"
}
} */
Usage
Add PostCSS Environment Variables to your project:
npm install postcss postcss-env-function --save-dev
Use it as a PostCSS plugin:
const postcss = require('postcss')
const postcssEnvFunction = require('postcss-env-function')
postcss([
postcssEnvFunction()
]).process(YOUR_CSS )
PostCSS Environment Variables runs in all Node environments, with special instructions for:
Options
importFrom
The importFrom
option specifies sources where Environment Variables can be imported from, which might be JS and JSON files, functions, and directly passed objects.
postcssEnvFunction({
importFrom: 'path/to/file.js'
})
@media (max-width: env(--branding-small)) {
body {
padding: env(--branding-padding);
}
}
/* becomes */
@media (min-width: 600px) {
body {
padding: 20px;
}
}
Multiple sources can be passed into this option, and they will be parsed in the order they are received. JavaScript files, JSON files, functions, and objects will need to namespace Custom Properties using the environmentVariables
or environment-variables
key.
postcssEnvFunction({
importFrom: [
'path/to/file.js',
'and/then/this.json',
{
environmentVariables: { '--branding-padding': '20px' }
},
() => {
const environmentVariables = { '--branding-padding': '20px' }
return { environmentVariables }
}
]
})
See example imports written in JS and JSON.
Currently only valid custom property names (beginning with --
) are accepted.
Not all valid declaration value names are accepted.
disableDeprecationNotice
Silence the deprecation notice that is printed to the console when using `importFrom``.
postcss-env-function is deprecated and will be removed.
Check the discussion on github for more details. https://github.com/csstools/postcss-plugins/discussions/192