Improving Compression - contivero/hasmin GitHub Wiki

Improving Compression

Table of Contents

Using Lower Case

A big part of CSS is case-insensitive (specially when used to style HTML). As such, Hasmin lowercases most of its output to aid gzip’s algorithm compress better. Among the usual stuff such as color values and property names, Hasmin lowercases:

  1. Font families, (i.e. <font-family> values), since according to §5.1 of CSS Fonts Module Level 3, UAs must much font family names case insensitively.

  2. Unquoted parameters to the local() function in the src property (See §4.3).

  3. Character encodings written as a <string> in the @charset rule. E.g. @charset "UTF-8" may be rewritten as @charset "utf-8"

  4. Common values and functions written by developers in camelCase, such as currentColor or translateZ()

Lowercasing is one of the most consistent ways of reducing the final compressed size. Nonetheless, disable with --no-lowercasing if needed.

Normalizing Quotes

Values of type <string> are enclosed between either single, or double quotes. To increase repetition, and potentially avoid a character from being include in the Huffman tree of literals, quotation marks can be normalized. This means converting all the <string>s to use double, or single quotes, if possible. Ideally, the file would be analyzed to find the appropriate quotes to use, although currently Hasmin just tries to convert everything to double quotes. If desired, it can be disabled with --no-quote-normalization.

Sorting Declarations

Declarations may be freely reordered as long as there is not interdependency between them (e.g. between shorthands and their longhands). Currently, only a naive lexicographical ordering is implemented; this is unsafe and thus disabled by default, but can be enabled using the --sort-declarations flag.

Sorting Selectors

Contrary to the case with declarations, selectors in a selector list may be shuffled without restrictions. Currently, only a lexicographical order is supported, and enabled by default. The gzip gains by doing this tend to be modest at best, and sometimes might even be detrimental. If that’s the case, disable with --no-selector-sorting.

⚠️ **GitHub.com Fallback** ⚠️