CSS compressor written in OCaml

Taddeus Kroes e981b090ee Updated README há 11 anos atrás
.gitignore 6ee79d6b7b Cleanup há 11 anos atrás
Makefile e981b090ee Updated README há 11 anos atrás
README.md e981b090ee Updated README há 11 anos atrás
color_names.ml 23ce71c2e2 Removed some useless color minifications há 11 anos atrás
duplicates.ml d36f52bf3f Implemented duplicate declaration pruning há 11 anos atrás
lexer.mll aaaf2601ed Fixed support for @-<prefix>-keyframes syntax há 11 anos atrás
main.ml 0f0ddfeda0 Forgot newline in usage message há 11 anos atrás
parse.ml 70f032a31d Lexer now correctly tracks line numbers + some general cleanup há 11 anos atrás
parser.mly 3b7dc07458 Extended 'Simple' selector type to algebraic data types há 11 anos atrás
selector.ml 099cdb0189 Renamed a file há 11 anos atrás
shorthand.ml 2406d894cf Improved shorthand folding when component expressions are !important há 11 anos atrás
simple.ml bcf7891738 Merge branch 'minimal' há 11 anos atrás
stringify.ml bed50c9bba Minified numbers are now rounded to 2 decimal digits há 11 anos atrás
types.ml 43ee471f69 Changed main function for minimal command-line usage (just simple minification operations, for now) há 11 anos atrás
util.ml 5003457c8a Added --sort option há 11 anos atrás

README.md

About

mincss is an extendible CSS minifier written in OCaml. It features a complete parser for the CSS3 language, along with type definitions that are consistent with the official CSS specification and a traversal utility function for use in transformation passes.

Optimizations

Whitespace compression

a,                                  |  a,.myclass [class~="foo"]>p{color:#fff}
.myclass [class ~= "foo"] >  p {    |
    color: #fff;                    |
}                                   |

Compression of simple expressions

color: white;                       |  color: #fff;
font-weight: normal;                |  font-weight: 400;

Generation of shorthand properties

font-weight: normal;                |  font: normal 12px/15px sans-serif;
font-size: 12px;                    |
line-height: 15px;                  |
font-family: sans-serif;            |

Any existing shorthands are first unfolded into their non-shorthand counterparts, after which the last value is used for shorthand generation:

font: normal 12px/15px sans-serif;  |  font: bold 12px/15px sans-serif;
font-weight: bold;                  |

Command-line interface

Output of mincss -h:

Usage: ./mincss [<options>] [<file> ...]

Generic options:
 -h, --help        Show this help message
 -v, --verbose     Verbose mode: show compression rate
 -o <file>         Output file (defaults to stdout)
 <file> ...        Input files (default is to read from stdin)

Optimization flags (if none are specified, all are enabled):
 -w, --whitespace  Eliminate unnecessary whitespaces (has the greatest effect, omit for pretty-printing)
 -c, --simple      Shorten colors and font weights
 -s, --shorthands  Generate shorthand properties
 -d, --duplicates  Prune duplicate properties (WARNING: may affect cross-browser hacks)
 -p, --pretty      Shorthand for -c -s -d
 -e, --echo        Just parse and pretty-print, no optimizations

Formatting options:
 --sort            Sort declarations in each selector group

Building mincss

Dependencies are OCaml 4.0 and menhir.

Bootstrapping on a Debian system can be done as follows:

$ sudo apt-get install ocaml opam git
$ opam init
$ opam switch 4.01.0
$ opam install menhir
$ git clone git@github.com:taddeus/mincss.git
$ cd mincss
$ make
$ ./mincss --help