MyNixOS website logo
Description

CSS Minifier.

A CSS minifier which not only aims at reducing the amount of bytes of the output, but also at improving gzip compression. It may be used as a library, or a stand-alone executable. For the library, refer to the Hasmin module documentation. For the program: the output is the minified CSS file, but hasmin allows also its compression into gzip using Google's Zopfli library.

To use it: ./hasmin input.css > output.css

By default, most minification techniques are enabled. For a list of available flags, do: ./hasmin --help

Hasmin - A Haskell CSS Minifier

Build Status Hackage Hackage-Deps License

Aside from the usual techniques (e.g. whitespace removal, color minification, etc.), the idea was to explore new possibilities, by implementing things other minifiers weren't doing, or they were, but not taking full advantage of.

Also, the minifier implements some techniques that do nothing for minified sizes, but attempt to improve post-compression sizes (at least when using DEFLATE, i.e. gzip).

For a list of techniques, see the wiki. To use it as a library, see below.

Building & Installing

Stack

The easiest and prefered way is using stack. To directly install, use the following one-liner:

git clone [email protected]:contivero/hasmin.git && cd hasmin && stack install

Stack installs inside ~/.local/bin by default. If you'd rather just try hasmin out, replace install by build, and then use stack exec hasmin from inside the hasmin directory (keep in mind it has a slight additional delay at the beginning when run this way).

Cabal

Alternatively, you can use cabal:

$ cd path/to/hasmin/repo
$ cabal update                      # Make sure to have the latest package list
$ cabal sandbox init                # Initialise a sandbox
$ cabal install --only-dependencies # Install dependencies into the sandbox
$ cabal build                       # Build hasmin inside the sandbox

Minifier Usage

Hasmin expects a path to the CSS file, and outputs the minified result to stdout. Say you have a sheet.css and want to minify it, and save it as sheet.min.css. Then, run:

$ hasmin sheet.css > sheet.min.css

Every technique is enabled by default, except for:

  1. Escaped character conversions (e.g. converting \2714 into , enabled with --convert-escaped-characters).
  2. Dimension minifications (e.g. converting 12px to 9pt, enabled with --dimension-min, or just -d).
  3. Lexicographical sorting of declarations (enabled with --sort-declarations).

The first two are disabled mainly because they are—on average, not always—detrimental for DEFLATE compression. As for declaration sorting, whether it benefits or hurts compression rates is very stylesheet-dependent, and the current implementation is quite naive, hence unsafe.

When something needs to be disabled, use the appropriate flag. Not every technique can be toggled, but if there is any one in particular that you need to and can't, open an issue about it.

Note: there is a problem in Windows when using the --convert-escaped-characters flag to enable the conversion of escaped characters. A workaround is changing the code page, which can be done by running chcp 65001 in the terminal (whether cmd, or cygwin).

Library

The preferable way to use Hasmin as a library is to import Hasmin; a usage example can be seen in the module's documentation. That is currently the only module that is sure to abide by PVP. Most other exposed modules are so because tests need it, and thus definitions there may be changed anytime. In case something internal is needed though, feel free to open an issue about it.

Zopfli Integration

Hasmin uses bindings to Google's Zopfli library, allowing the possibility to compress the result.

Since the output is a gzip file, it can be used for the web. It tipically produces files 3~8% smaller than zlib, at the cost of being around 80 times slower, so it is only a good idea if you don't need compression on the fly.

Zopfli is released under the Apache License, Version 2.0.

Metadata

Version

1.0.3

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows