crass
A CSS utility library for JS
Last updated 8 years ago by mattbasta .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install crass 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 0.12.3                                ...           latest (7 years ago)

37 Versions

  • 0.12.3                                ...           7 years ago
  • 0.12.2                                ...           7 years ago
  • 0.12.1                                ...           7 years ago
  • 0.12.0                                ...           7 years ago
  • 0.11.2                                ...           7 years ago
  • 0.11.1                                ...           7 years ago
  • 0.10.4                                ...           7 years ago
  • 0.10.3                                ...           8 years ago
  • 0.10.2                                ...           8 years ago
  • 0.10.1                                ...           8 years ago
  • 0.10.0                                ...           8 years ago
  • 0.9.3                                ...           8 years ago
  • 0.9.2                                ...           8 years ago
  • 0.9.1                                ...           8 years ago
  • 0.8.0                                ...           8 years ago
  • 0.7.12                                ...           8 years ago
  • 0.7.11                                ...           8 years ago
  • 0.7.10                                ...           8 years ago
  • 0.7.9                                ...           8 years ago
  • 0.7.8                                ...           9 years ago
  • 0.7.7                                ...           10 years ago
  • 0.7.6                                ...           10 years ago
  • 0.7.5                                ...           10 years ago
  • 0.7.4                                ...           10 years ago
  • 0.7.3                                ...           10 years ago
  • 0.7.2                                ...           10 years ago
  • 0.7.1                                ...           10 years ago
  • 0.7.0                                ...           10 years ago
  • 0.6.6                                ...           10 years ago
  • 0.6.5                                ...           10 years ago
  • 0.6.4                                ...           10 years ago
  • 0.6.3                                ...           10 years ago
  • 0.6.2                                ...           10 years ago
  • 0.6.1                                ...           10 years ago
  • 0.6.0                                ...           10 years ago
  • 0.5.1                                ...           11 years ago
  • 0.5.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (5)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE