• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A CSS minifier and utility library for JavaScript

crass

A CSS minification, pretty printing, and general utility library written in JS.

Build Status Build status

Why Crass?

Crass is one of only a handful of CSS minifiers that creates a full parse tree of the CSS. Most other CSS minifiers operate on the string source instead, which makes it impossible to perform all types of optimizations.

Pros:

  • Better minification, particularly after gzip
  • Support for consistent pretty printing
  • Support for settled CSS4 and all of CSS3
  • Ability to strip obsolete tags (removed prefixes, old standard specs, etc.) by browser version
  • Convert all colors to their smallest possible form
  • Unsafe optimizations are opt-in only

Cons:

  • Slower minification times
  • Cannot minify CSS with syntax errors
  • Certain "CSS hacks" that use invalid syntax are unsupported

Installation

Crass is built with ES2015 and requires Node 6 or higher.

npm install --save-dev crass

API

var crass = require('crass');

// Parse any valid CSS stylesheet:
var parsed = crass.parse('b {font-weight: bold;}');

// Optimize the stylesheet:
parsed = parsed.optimize();

// Pretty print the stylesheet:
console.log(parsed.pretty());

// Print a minified version of the stylesheet:
console.log(parsed.toString());

// The constructors for the AST nodes used to represent the
// parsed CSS are available on `crass.objects`.

Improvements on the API will be made in the future.

Command Line Interface

If you npm install -g crass, you'll get crass on your PATH.

crass input.css [--optimize [--O1]] [--min x,y,z] [--pretty] [--saveie] [--css4]

If you don't specify --min, crass will automatically default to the latest browser version from two years ago. At the time of writing, this is Chrome 39, Firefox 31, IE 11, and Opera 26.

  • --optimize: Flag to enable basic optimization
  • --O1: Only applies when --optimize is active. Flag to enable more advanced optimizations, though these are not guaranteed to work for all CSS.
  • --min: Setting this flag followed by a comma-separated list of browser versions will instruct Crass to strip CSS that would otherwise only apply to browsers older than the versions listed. For example, --min ie9,fx30 would strip CSS that applies only to Firefox 29 and below and Internet Explorer 8 and below. The following prefixes are supported: ie, op, fx, chr
  • --pretty: Flag to enable pretty printing of output
  • --saveie: Flag to enable features to specifically support Internet Explorer 6 and below
  • --css4: Flag that allows optimized output to contain CSS4 features and syntax. This is not be supported in all modern browsers. You should only use this if you explicitly want CSS4 output.

Minification

Outputting a crass object as a string will perform the equivalent of most CSS minification tools. The corresponding styles are output in the minimum amount of CSS possible, without any whitespace.

Some minifiers also perform basic replacement and removal operations to replace certain patterns with other patterns. Using the --optimize and --O1 flags on the command line and .optimize() and .optimize({o1: true}) in the API will perform many of these operations along with additional optimizations that are not possible with traditional minification tools.

For example, since most minification tools do not truly parse CSS, they cannot perform any reordering or transformation. Crass, on the other hand, will rewrite code like this:

b, c, a {
    third: rgba(255, 255, 255, 0.9);
    second: abc;
    first: 50%;
}

into something that looks like:

a, b, c {
    first: 50%;
    second: abc;
    third: hsla(0, 0%, 100%, 0.9);
}

Reordering selectors and declarations significantly improves minified code sizes. Colors can be translated between HSL/RGB/hex/etc. to use the smallest form.

Benchmarks

Crass performs very well in many CSS minification benchmarks. See goalsmashers' css minification benchmark for more.

FAQ

Will there be a version that runs in the browser?

You can import Crass into your project using any appropriate build tool, like browserify or Webpack. Crass's importable modules have no dependencies on anything browser-incompatible.

Check out the Github pages for Crass for a simple browser-ready version:

http://www.mattbasta.com/crass/

What about comments? Docblocks?

All comments are ignored at the moment. Support for storing comment data may be added in the future, and contributions to add this support are welcome.

What about @import statements?

Crass does not follow @import statements. You should use another CSS processing tool to resolve @imports and inline them appropriately, then use Crass to minify the result.

More Repositories

1

client-performance-handbook

The Client-Side Performance Handbook
CSS
41
star
2

Legend-of-Adventure

An online RPG/action/adventure game.
TypeScript
17
star
3

itsdangerous-php

A PHP port of the `itsdangerous` Python library
PHP
14
star
4

btype

A fast language for the web
JavaScript
7
star
5

jinja2js

A project to let you compile Jinja2 templates to JavaScript.
Python
5
star
6

mattbasta.github.com

My delightful user page
JavaScript
5
star
7

fastchardet

A less thorough (and faster) version of python-chardet
Python
4
star
8

webraft

A WIP implementation-agnostic Raft implementation
JavaScript
4
star
9

mister

A tiny/friendly map/reduce library that wants to be your neighbor.
Python
4
star
10

ivote-teama

iVote - Team A, Spring 2012
C#
3
star
11

chaos

An angry primate that figures out what's wrong with your Python
Python
3
star
12

addon-packager

The easiest way to build an add-on since fizzy pop.
Python
3
star
13

areweequalyet

Are we equal yet?
CSS
2
star
14

Tinytape

The best freakin music site on the internet
PHP
2
star
15

futzy

A structured fuzzy matching library for performance
TypeScript
2
star
16

apploader

Loadin apps all up in hurrr
Python
2
star
17

minebroom

Minebroom: A crappy JS version of Minesweeper!
JavaScript
2
star
18

minutegram

Pictures that take a minute
JavaScript
2
star
19

lavish

A next-generation web-based shell
JavaScript
2
star
20

panopticam

A place to see your friends
JavaScript
2
star
21

emojisaurusrex

Quick access for cool shit or whatever
HTML
2
star
22

widgetgrid-ar

Automatically exported from code.google.com/p/ar-widgetgrid
Java
2
star
23

dotfiles

My personal dotfiles
Shell
1
star
24

superior-marco-cousins

Jump for great platform game
JavaScript
1
star
25

bastascript

A JavaScript dialect that makes it easier to write effective functional code.
JavaScript
1
star
26

Diggy

A dwarf that digs for tracebacks in amo-validator.
Python
1
star
27

box-boards

JavaScript
1
star
28

oath

A promise utility library
JavaScript
1
star
29

munchylunchy-api-harness

A nice little class that lets you conveniently access MunchyLunchy's API
Python
1
star
30

cootie

A simple multi-application manager for VPSs.
JavaScript
1
star
31

moz-movie-night

Official Mozilla Intern Movie Night voting application.
PHP
1
star
32

monacle

Definitely not monocle
Python
1
star
33

spice-cadets

Java
1
star
34

box-sums

A data-focused realtime collaboration app
JavaScript
1
star
35

hotword.js

JavaScript
1
star
36

random-crap

A collection of the random crap I've made over the course of the last decade of my life.
PHP
1
star
37

bots

IRC bots
JavaScript
1
star
38

galsim

N-Body simulation, written in Java. May or may not use CUDA.
Java
1
star
39

testpackagedapp

Generate packaged apps for testing purposes
Python
1
star
40

bearicorn

When beast meets Canada
JavaScript
1
star
41

mocha-junit-reporter

The Mocha JUnit reporter, but with parallel support
JavaScript
1
star
42

Grizwald

A statistics engine to diagnose and analyze problems with the AMO validator.
JavaScript
1
star
43

graphicsbc

A microscript for creating graphics
JavaScript
1
star
44

php-honeypot

A simple comment spam honeypot written in PHP.
PHP
1
star