https://github.com/postcss/postcss-custom-media seems like a great alternative that works with the latest PostCSS versions.
PostCSS plugin to use CSS Custom Properties in @media query parameters.
There's no specification for this! (but it feels so natural..)
/* input */
:root {
--min-width: 1000px;
--smallscreen: 480px;
}
@media (min-width: var(--min-width)) {}
@media (max-width: calc(var(--min-width) - 1px)) {}
@custom-media --small-device (max-width: var(--smallscreen));
@media (--small-device) {}/* output */
@media (min-width: 1000px) {}
@media (max-width: 999px) {}
@media (max-width: 480px) {}npm install postcss-media-variables --save-dev
postcss-media-variables has to be used twice!
Every other plugin is optional, but keep this order of plugins if you use any of them.
postcss-media-variables(first occurence)postcss-custom-mediapostcss-css-variablesand/orpostcss-custom-propertiespostcss-calcpostcss-media-variables(second occurence)postcss-media-minmax
Since v1.1.0, this plugin plays well with postcss-custom-media!
var fs = require('fs');
var postcss = require('postcss');
var mediaVariables = require('postcss-media-variables');
var cssVariables = require('postcss-css-variables');
var customMedia = require('postcss-custom-media');
var calc = require('postcss-calc');
var mycss = fs.readFileSync('input.css', 'utf8');
// Process your CSS
var output = postcss()
.use(mediaVariables()) // first run
.use(customMedia(/* options */))
.use(cssVariables(/* options */))
.use(calc(/* options */))
.use(mediaVariables()) // second run
.process(mycss, { /* postcss - options */ })
.css;
console.log(output);For more general PostCSS usage, see this section
In the first run, postcss-media-variables inspects every @media and every @custom-media rule, parses their params and wraps the affected @media rules into helper rules.
The information in the helper rules are calculated and resolved then.
In the second run, this plugin removes those helper rules again and uses the newly calculated information from there to change the @media-rule parameters.
This plugin is created in personal need of a solution for the issue Resolve variables in media queries .