Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-short-css-vars

Package Overview
Dependencies
Maintainers
11
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-short-css-vars

Optimization plugin to shorten the names of CSS variables

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
11
Weekly downloads
 
Created
Source

babel-plugin-short-css-vars

NPM Version Build Status License MIT

Babel plugin using short-css-vars to shorten the names of CSS variables in JavaScript code.

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.

Installation

$ npm install babel-plugin-short-css-vars --save-dev

Example

Input

const sheet = document.createElement('style');
sheet.innerHTML = `
:root { --long-variable-name-that-defines-a-particular-color: blue; }
`;

const div = document.createElement('div');
div.style.color = "var(--long-variable-name-that-defines-a-particular-color)";

Output

const sheet = document.createElement('style');
sheet.innerHTML = `
:root { --vf2dsn: blue; }
`;

const div = document.createElement('div');
div.style.color = "var(--vf2dsn)";

This plugin also works with JSX/React source.

Usage

{
  "plugins": [
    "babel-plugin-short-css-vars"
  ]
}

See the babel docs for more configuration details.

Options

This plugin supports all the utility options. Because these options will need to be a RegExp or function, the Babel config willl need to be a JavaScript configuration file.

formatter

See the utility for how the default formatting works. If you wish to use a different pattern, simply provide a different formatter function via options.

// babel.config.js
const fnv1a = require('fnv1a');

module.exports = {
  "plugins": [
    [
      "babel-plugin-short-css-vars",
      {
        formatter: name => fnv1a(name).toString(36)
      }
    ]
  ]
}

The name provided to the formatter is the variable name WITHOUT the '--' prefix. The return value should also be name-only. So my-custom-var rather than --my-custom-var.

ignore

If you need to avoid hashing certain variable names, you can ignore them with this option by either RegExp, function, or string (which will be turned into RegExp). This can be useful if you are referencing CSS Variables in an app, yet you do not have control over some of the declarations.

// babel.config.js
module.exports = {
  "plugins": [
    [
      "babel-plugin-short-css-vars",
      {
        ignore: /^third-party-theme/
      }
    ]
  ]
}

In another scenario, if you have several short variable names (for example --blue) you can use a function to ignore names by length.

postcss([shortCssVars({
  ignore: name => name.length <= 4
})])

The name provided to ignore is the variable name WITHOUT the '--' prefix. So blue rather than --blue.

Keywords

FAQs

Package last updated on 01 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc