npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

postcss-plugin-var-hash-postfix

v0.2.0

Published

PostCSS plugin to append a hash to the end of a CSS Variable

Downloads

254

Readme

PostCSS Plugin Var Hash Postfix

A PostCSS plugin to add hashes or other postfixes to your CSS Variables (CSS Custom Properties). Take your source CSS from

--purple: rebeccapurple;

div {
  color: var(--purple);
}

to

--purple-abc123: rebeccapurple;

div {
  color: var(--purple-abc123);
}

Will also transform CSS Variables declared using the @property at-rule synatx.

Setup

Install the postcss-plugin-var-hash-postfix package:

npm i postcss-plugin-var-hash-postfix

Add the postcss-plugin-var-hash-postfix plugin to your PostCSS config:

import postCssVarHashPostfix from "postcss-plugin-var-hash-postfix";

await postCss([
  postCssVarHashPostfix({
    hash: "abc123",
  }),
]);

See below for additional configuration and setup.

Why

CSS Variables (CSS Custom Properties) are a great way to share config, styles, colors, etc. across an app, website, or framework; they even penetrate the shadow dom of custom web components.

But maybe you want to make it so some of your CSS Variables aren't easily overridden by other developers; perhaps you're using them for some config that's internal to your library but isn't meant to be used by external developers.

This plugin enables you to append a hash to your variables automatically, as often as each build if you want. This way the variable changes frequently enough to discourage use by others.

Or, alternatively, you just want to add a hash to your CSS Variables for fun.

Config

Plugin Options

hash

The hash to apply as a postfix to your CSS Variables. You can hardcode a hash and update it manually:

postCssVarHashPostfix({
  hash: "abc123",
});

Or use an environment variable to change the hash on every build, such as:

postCssVarHashPostfix({
  hash: process.env.GIT_SHA,
});

If you don't want a full-length SHA for the hash, add the maxLength option as well.

Falsy hash

If the hash is falsy ('', false, null, undefined, etc.), then no hash is appended.

This is intended to help you easily skip appending a hash (e.g. for local development) while still applying a hash at build time.

Note: when hash is falsy, the plugin will bail early and not apply any other config options.

maxLength

Will truncate your hashes to a specific length, so you don't have to do it manually.

// input: --test: rebeccapurple;

postCssVarHashPostfix({
  hash: "1234567890",
  maxLength: 5,
});

// output: --test-12345: rebeccapurple;

ignorePrefixes

An array of strings for which any CSS Variable that starts with the prefix will not have a hash appended to them. This allows you to provide CSS Variables that are intended to be used by other developers, while still protecting the CSS Variables you don't intend to be part of your public API.

// input: --ignore: blue; --test: green;

postCssVarHashPostfix({
  hash: "123abc",
  ignorePrefixes: ["ignore"],
});

// output: --ignore: blue; --test-abc123: green;

Note that this option will ignore anything that starts with the string; with the example above, both --ignore and --ignorethis would not have a hash appended to them.

delimiter

Customize the delimiter used to separate the existing CSS Variable name and the hash.

// input: --test: rebeccapurple;

postCssVarHashPostfix({
  hash: "123abc",
  delimiter: "_",
});

// output: --test_123abc: rebeccapurple;