postcss-color-golf
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

postcss-color-golf PostCSS Logo

the code golfer’s color minifier for CSS!

npm version Types Included PostCSS plugin License: CC0-1.0 npm downloads bundle size

A PostCSS plugin that aggressively minifies CSS color values:

  • Shortens hex codes (#aabbcc#abc)
  • Converts rgb()/rgba() to hex when shorter
  • Replaces color names with their shortest possible equivalent
  • Designed for the most compact CSS output—perfect for code golf!

Install

npm install postcss-color-golf --save-dev

Usage

ESM (Node ≥ 12+, modern bundlers, most setups)

import postcss from "postcss";
import postcssColorGolf from "postcss-color-golf";

postcss([
  postcssColorGolf()
]).process(YOUR_CSS).then(result => {
  console.log(result.css);
});

CommonJS (require)

No .default needed! The dual build makes this work as expected.

const postcss = require("postcss");
const postcssColorGolf = require("postcss-color-golf");

postcss([
  postcssColorGolf()
]).process(YOUR_CSS).then(result => {
  console.log(result.css);
});

postcss.config.js

module.exports = {
  plugins: [
    require("postcss-color-golf")()
    // ...other plugins
  ]
};

TypeScript

Type definitions are included for a great developer experience:

import postcss from "postcss";
import postcssColorGolf from "postcss-color-golf";

postcss([postcssColorGolf()]).process(cssString).then(result => {
  console.log(result.css);
});

What does it do?

  • Shortens hex codes: #aabbcc#abc
  • Converts rgb/rgba to hex: rgb(255,0,0)#f00 rgba(0,255,0,0.5)#0f080
  • Replaces color names: blue#00f, fuchsia#f0f
  • Aggressive color minification: Optimizes every color value it finds for the shortest possible output.

Example

Input:

a {
  color: rgb(255,0,0);
  background: aliceblue;
  border: 1px solid #aabbcc;
  box-shadow: 0 0 3px rgba(0,255,0,0.5);
}

Output:

a {
  color:#f00;
  background:#f0f8ff;
  border:1px solid #abc;
  box-shadow:0 0 3px #0f080;
}

Options

Currently, there are no options—just plug it in and go! Future versions may add configuration for specific minification strategies.


License

CC0 1.0 Universal (Public Domain Dedication) Use it for anything, commercial or personal, with or without attribution.

Package Sidebar

Install

npm i postcss-color-golf

Version

1.1.0

License

CC0-1.0

Unpacked Size

21.8 kB

Total Files

7

Last publish

Collaborators

  • xero