Raku CSS minifier
Find a file
2026-06-06 05:44:30 +03:00
bin 6 fixes: rgb rounding, selector case, CLI edge, gradual eviction, redundant pass 2026-06-06 05:34:59 +03:00
lib/CSS 0.0.9 2026-06-06 05:44:30 +03:00
t Filter empty rulesets from sep join, fix hyper→for, fold rgb-to-hex into apply-color-names, various minor cleanups 2026-06-06 00:45:58 +03:00
.gitignore
Changes 0.0.9 2026-06-06 05:44:30 +03:00
dist.ini
LICENSE
META6.json 0.0.9 2026-06-06 05:44:30 +03:00
README.md Document CSS_MINIFIER_CACHE_MAX env var in README and POD 2026-06-06 05:42:37 +03:00

CSS::Minifier

Raku CSS minifier - produces smaller CSS through value normalization, color shortening, shorthand consolidation, and structural deduplication/merging.

Features

  • Plugin pipeline - modular optimization passes (values, colors, dedup, merging)
  • Two levels - level 1 (safe normalizations) vs level 2 (structural optimizations)
  • Color optimization - shorten hex colors, optionally convert to named colors
  • Shorthand consolidation - margin, padding, border, border-{top,right,bottom,left}, outline, list-style, background, font longhands consolidated to shorthand
  • Value normalization - bold700, normal400
  • Duplicate removal - identical rulesets and @font-face blocks deduplicated
  • Ruleset merging - same-selector rules merged, same-declaration selectors combined
  • License preservation - /*! */ comments kept with --preserve-licenses
  • Custom property support - --* declarations preserved verbatim, not dedupped
  • CLI tool - cssminify with stdin/file input and output redirection

Installation

zef install CSS::Minifier

Installing from source

cd /path/to/CSS-Minifier
zef install .

Requires Raku 6.d or later.

Usage

Command Line

# Read from file
cssminify input.css > output.css

# Level 2 (structural optimizations: dedup, merging)
cssminify -l2 input.css > output.css

# Write to file
cssminify -o output.css input.css

# Read from stdin
cat input.css | cssminify

# Preserve license comments
cssminify -p input.css

# Disable hex color shortening (keep #FF0000 instead of #F00)
cssminify --no-color-masks input.css

# Separate rules with newlines (readable output)
cssminify -r input.css

# Convert hex colors to named colors
cssminify -n input.css

# Verbose output (log plugin names)
cssminify -v input.css

# Print version
cssminify --version

Flags

Short Long Description
-o --output Write to file instead of stdout
-l --level Optimization level: 1 (safe normalizations) or 2 (structural, default)
-p --preserve-licenses Keep /*! */ license comments
-n --color-names Convert hex colors to named colors
-m / -/m --color-masks / --no-color-masks Enable/disable hex color shortening
-r --readable Separate rules with newlines
-v --verbose Log plugin names to stderr
--version Print version and exit

Short flags with values can use =, fused, or space-separated syntax: -l=2, -l2, or -l 2.

Environment

Variable Description
CSS_MINIFIER_CACHE_MAX Max internal character-array cache entries (default 500). Increase for large stylesheets to reduce repeated string scanning during parsing.

Library

use CSS::Minifier;

# Level 2 - structural optimizations (dedup, merging, default)
my $min = CSS::Minifier.minify($css);

# Level 1 - safe normalizations only
my $min = CSS::Minifier.minify($css, :level(1));

# Preserve license comments
my $min = CSS::Minifier.minify($css, :preserve-licenses);

# Convert hex colors to named colors
my $min = CSS::Minifier.minify($css, :color-names);

# Disable hex color shortening
my $min = CSS::Minifier.minify($css, :!color-masks);

# Add custom plugins (runs after all level plugins, in append order)
# Custom plugins must implement the CSS::Minifier::Plugin role.
my $min = CSS::Minifier.minify($css, :level(2), :extra-plugins[$my-plugin]);

Optimization Levels

Level 1

  • Font-weight normalization (bold700, normal400)
  • Color shortening (#FF0000#F00, red#F00)
  • Comment removal (/*! */ kept with --preserve-licenses)
  • Whitespace and quote normalization
  • Shorthand consolidation (margin, padding, border, border-*, outline, list-style, background, font)

Level 2 (default)

  • Duplicate ruleset removal
  • @font-face deduplication
  • Same-selector ruleset merging
  • Same-declaration selector combination

Examples

Input

h1 { color: red; }
h2 { color: red; }
h1 { margin: 0; }

Level 1 Output

h1{color:#F00}h2{color:#F00}h1{margin:0}

Level 2 Output

h1,h2{color:#F00}h1{margin:0}

AUTHOR

Sasha Abbott sashaa@disroot.org

LICENSE

This library is free software; you can redistribute it and/or modify it under CC0.