Crate const_css_minify

source ·
Expand description

github crates.io docs.rs

Include a minified css file as an inline const in your high-performance compiled web application.

You can call it with a path to your source css file, just like you might use the built-in macro include_str!():

use const_css_minify::minify;

// this is probably the pattern you want to use
const CSS: &str = minify!("./path/to/style.css");

IMPORTANT! the current version of const_css_minify resolves paths relative to the crate root (i.e. the directory where your Cargo.toml is). This behaviour is DIFFERENT from the rust built-in macros like include_str!() which use a path relative to the source file from which it’s invoked. Consider the current behaviour unstable and likely to change - our preference would be to match the established convention, but implementing this change is dependant on the stabilisation of a source path api in proc_macro as per https://github.com/rust-lang/rust/issues/54725

It’s also possible to include a raw string with your css directly in your rust source:

use const_css_minify::minify;

const CSS: &str = minify!(r#"
    input[type="radio"]:checked, .button:hover {
        color: rgb(0 255 100% / 0.8);
        margin: 10px 10px;
    }
"#);
assert_eq!(CSS, "input[type=\"radio\"]:checked,.button:hover{color:#0ffc;margin:10px 10px}");

Note also that the current version of const_css_minify does not support passing in a variable. only the above two patterns of a path to an external file or a literal str will work.

const_css_minify is not a good solution if your css changes out-of-step with your binary, as you will not be able to change the css without recompiling your application.

§const_css_minify will:
  • remove unneeded whitespace and linebreaks
  • remove comments
  • remove unneeded trailing semicolon in each declaration block
  • opportunistically minify colors specified either by literal hex values or by rgb(), rgba(), hsl() and hsla() functions (in either legacy syntax with commas or modern syntax without commas) without changing the color. e.g. #ffffff will be substituted with #fff, hsl(180 50 50) with #40bfbf, rgba(20%, 40%, 60%, 0.8) with #369c, etc. const-css-minify will not attempt to calculate nested/complicated/relative rgb expressions (which will be passed through unadulturated for the end user’s browser to figure out for itself) but many simple/literal expressions will be resolved and minified.
  • silently ignore css syntax errors originating in your source file*, and in so doing possibly elicit slightly different failure modes from renderers by altering the placement of whitespace around misplaced operators
§const_css_minify will not:
  • compress your css using gz, br or deflate
  • change the semantic meaning of your semantically valid css
  • make any substitutions other than identical literal colors
  • alert you to invalid css* - it’s not truly parsing the css, just scanning for and removing characters it identifies as unnecessary

note*: The current version of const-css-minify will emit compile-time warning messages for some syntax errors (specifically unclosed quote strings and comments) which indicate an error in the css (or a bug in const-css-minify), however these messages do not offer much help to the user to locate the source of the error. Internally, these error states are identifed and handled to avoid panicking due to indexing out-of-bounds, and so reporting the error message at compile time is in a sense ‘for free’, but this is a non-core feature of the library and may be removed in a future version if it turns out to do more harm than good. In any case, const-css-minify generally assumes it is being fed valid css as input and offers no guarantees about warnings. const-css-minify should not be relied upon for linting of css.

const_css_minify is a lightweight solution - the current version of const_css_minify has zero dependencies outside rust’s built-in std and proc_macro libraries.

Macros§

  • Produce a minified css file as an inline const