1.2.1 • Published 2 years ago

@zonx/short-css-vars v1.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

short-css-vars

NPM Version Build Status License MIT

Utility to shorten the names of CSS variables in stylesheets.

CSS variables are renamed with a unique hash that is consistent across processed files. This allows you to have common style files that reference variables with varying values defined in theme files.

Usage

For many cases you can simply configure these plugins in your build chains:

Otherwise, see the API docs below to use the utility directly.

Formatting

The default formatter uses the popular string-hash with a djb2-like algorithm, then base-36 encodes the result. In the rare chance of collisions, an error will be thrown with the colliding names. A custom formatter can provided via the options

Installation

$ npm install short-css-vars

ShortCssVars

Shorten lengthy CSS variable names

Kind: global class

new ShortCssVars([options])

Each instance keeps track of renamed variables to check for collisions and to provide a mapping of changed names.

ParamTypeDescription
[options]objectOptional configurations
[options.formatter]functionCustom formatter
[options.ignore]RegExp | string | functionRule to ignore certain variable names

shortCssVars.replaceName(varName)

Shortens the name part of a variable string

Kind: instance method of ShortCssVars

ParamTypeDescription
varNamestringVariable name including -- prefix

shortCssVars.replaceCss(css)

Shortens the names of variables throughout CSS

Kind: instance method of ShortCssVars

ParamTypeDescription
cssstringText containing CSS variables

shortCssVars.getMap()

Get a mapping of original names to shortened names

Kind: instance method of ShortCssVars
Example

{
  'long-variable-name-that-defines-a-particular-color': '1vf2dsn'
}