Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

postcss-variable-compress

navanshu14.3kMIT3.0.0TypeScript support: included

PostCSS plugin cleans up the variable names and saves space. It can will reduce your css variable to smaller variables

postcss, css, postcss-plugin, minify, compress, cssnano, css-variables, clean-css, shrik-css, vite, nextjs

readme

PostCSS Variable Compress npm GitHub branch checks state

postcss-variable-compress is a PostCSS plugin minifies variable names and saves space. Even if you have 1295 css variables still they will not exceed by two characters. It will transform css variable without breaking your stylesheet. Get if from NPM.

If you want it not modify some css variables, then pass them --{variable-name} as an array to the plugin.

If you are looking for a plugin that can work on separate files go to import splitFiles.js, works the same but it doesn't resets the variables.

:root {
  --first-color: #16f;
  --second-color: #ff7;
}

#firstParagraph {
  background-color: var(--first-color);
  color: var(--second-color);
}

#container {
  --first-color: #290;
}
:root {
  --0: #16f;
  --1: #ff7;
}

#firstParagraph {
  background-color: var(--0);
  color: var(--1);
}

#container {
  --0: #290;
}

Usage

Step 1: Install plugin:

npm install --save-dev postcss postcss-variable-compress

Step 2: Check you project for existed PostCSS config: postcss.config.js in the project root, "postcss" section in package.json or postcss in bundle config.

If you do not use PostCSS, add it according to official docs and set this plugin in settings.

Step 3: Add the plugin at the end of the plugins list:

module.exports = {
  plugins: [
    require('cssnano'),
+   require('postcss-variable-compress')
  ]
}

Api


module.exports = {
  plugins: [
    require('cssnano'),
    require('postcss-variable-compress')(
      // pass in css variables to avoid
      'light', 'dark', 'font', 'vh', 'r' // unprefixed
      // or
      '--height', '--font' // prefixed
      // or pass in as many function as your want
      // they take single param which is a the name of css variable
      // you can do checks on it and
      // return true if you want it to be skipped
      // for example
      (name) => name.includes('skip') // name prefixed css variable example --height
      // avoid regex if you can they are bad
    )
  ]
}

changelog

Change Log

V2.0.0

V0.2.6

  • Updating issues
  • Update postcss

V0.2.5

  • Coverage 100%.

v0.2.0

  • Same api can be passed with function so you can check if a variable has to be ignored

v0.1.3

  • Fixed the scoping issue in the variable
  • Trying to circumvent the Regex. Can't remove it yet but have some checks before regex in place.
  • Plugin notifies postcss about if a Declaration has bee processed.

v0.1.2

  • Fixing readme

v0.1.1

  • Updated readme

v0.1.0

  • Initial release