postcss-env-function
Use env() variables in CSS
Last updated 3 months ago by romainmenke .
MIT-0 · Repository · Bugs · Original npm · Tarball · package.json
$ npm install postcss-env-function 
SYNC missed versions from official npm registry.

PostCSS Environment Variables PostCSS Logo

NPM Version Build Status Discord

Baseline Status CSS Standard Status

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(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */)

PostCSS Environment Variables runs in all Node environments, with special instructions for:

Node PostCSS CLI Webpack Gulp Grunt

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' /* module.exports = {
      environmentVariables: {
        '--branding-padding': '20px',
        '--branding-small': '600px'
      }
    } */
})
@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: [
    /* Import from a CommonJS file:
    
    module.exports = {
      environmentVariables: {
        '--branding-padding': '20px'
      }
    } */
    'path/to/file.js',

    /* Import from a JSON file:

    {
      "environment-variables": {
        "--branding-padding": "20px"
      }
    } */
    'and/then/this.json',

    /* Import from an JavaScript Object: */
    {
      environmentVariables: { '--branding-padding': '20px' }
    },

    /* Import from a JavaScript Function: */
    () => {
      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

Current Tags

  • 7.0.0                                ...           latest (3 months ago)

16 Versions

  • 7.0.0                                ...           3 months ago
  • 6.0.0                                ...           a year ago
  • 5.0.1                                ...           2 years ago
  • 5.0.0                                ...           2 years ago
  • 4.0.6                                ...           3 years ago
  • 4.0.5                                ...           3 years ago
  • 4.0.4                                ...           3 years ago
  • 4.0.3                                ...           3 years ago
  • 4.0.2                                ...           3 years ago
  • 4.0.1                                ...           3 years ago
  • 4.0.0                                ...           3 years ago
  • 3.0.0                                ...           5 years ago
  • 2.0.2                                ...           6 years ago
  • 2.0.1                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.0.0                                ...           7 years ago
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (0)
None
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE