CSS Minifier and Utility Library
Crass is a JavaScript utility library designed for CSS minification, pretty-printing, and general manipulation. Unlike many other CSS minifiers that operate on string-based transformations, Crass constructs a full parse tree of the CSS, enabling deeper and more accurate optimizations, particularly for gzip compression, and consistent pretty-printing. It supports CSS3 and CSS4 features, with options to strip obsolete tags based on browser versions and convert colors to their smallest forms. The current stable version is 0.12.3, last published in February 2018. The project is no longer actively maintained, making it an abandoned library. Its primary differentiator is its parse-tree approach, which, while slower, allows for advanced optimizations not possible with string-manipulation tools.
Common errors
-
Error: Cannot find module 'crass'
cause The package `crass` is not installed in the project's `node_modules` directory or globally.fixRun `npm install crass` or `npm install -D crass` (for development dependency) in your project directory, or `npm install -g crass` for global CLI usage. -
SyntaxError: Unexpected token 'export' (or 'import')
cause Attempting to use ES module `import`/`export` syntax with Crass, which is a CommonJS module, without a transpilation step.fixChange your import statement to `const crass = require('crass');` and ensure your environment supports CommonJS modules, or configure a bundler (like Webpack or Rollup) to transpile ES modules to CommonJS if you must use `import` syntax in your source. -
Parse Error: Unexpected token...
cause The input CSS string contains syntax errors that prevent Crass from successfully parsing it into an Abstract Syntax Tree (AST).fixReview the CSS input for syntax errors. Crass requires valid CSS to build its parse tree. Use a CSS linter or online validator to identify and correct issues in your stylesheet.
Warnings
- breaking The `crass` package is no longer actively maintained. The last release was in February 2018. This means there will be no new features, bug fixes, or security updates.
- gotcha Crass is built with ES2015 and requires Node.js version 6.11.2 or higher. Using it with older Node.js versions may lead to syntax errors or runtime issues.
- gotcha Due to its parse-tree approach, Crass can be slower than string-based CSS minifiers. This might be noticeable in build processes for very large stylesheets.
- gotcha Crass cannot minify CSS with syntax errors. It relies on a valid parse tree, so malformed CSS will cause parsing to fail.
Install
-
npm install crass -
yarn add crass -
pnpm add crass
Imports
- crass
import crass from 'crass';
const crass = require('crass'); - parse
import { parse } from 'crass';const parsed = crass.parse(cssString);
- optimize
const optimized = parsed.optimize({ o1: true });
Quickstart
const crass = require('crass');
// Example CSS with various features for demonstration
const cssInput = `
@charset "UTF-8";
/* A comment */
.container {
display: flex;
justify-content: center;
align-items: center;
margin: 10px 20px 5px;
padding: 1em;
color: #FF0000; /* Red */
font-size: 16px;
border: 1px solid #ccc;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
@media (max-width: 600px) {
.container {
flex-direction: column;
margin: 5px;
}
}
a:hover {
text-decoration: underline;
}
/*
Crass can also perform optimizations like
shorthand conversions and color compression.
*/
body {
background: rgb(255, 255, 255);
}
`;
// Parse the CSS stylesheet into an AST
let parsed = crass.parse(cssInput);
console.log('--- Original Parse Tree (toString output) ---');
console.log(parsed.toString()); // Default toString() output is minified
// Optimize the stylesheet (basic optimization)
parsed = parsed.optimize();
console.log('\n--- Optimized & Minified ---');
console.log(parsed.toString());
// Optimize with advanced optimizations (O1 flag)
// Note: O1 can be more aggressive and might not be suitable for all CSS.
const parsedO1 = crass.parse(cssInput).optimize({ o1: true });
console.log('\n--- Optimized (O1) & Minified ---');
console.log(parsedO1.toString());
// Pretty print the stylesheet for readability
console.log('\n--- Pretty Printed ---');
console.log(parsed.pretty());
// Access AST nodes (example: count rules)
const rules = parsed.node.rules.filter(node => node.type === 'rule');
console.log(`\nFound ${rules.length} CSS rules after parsing and optimizing.`);