css-parser is a complete TypeScript library for parsing, validating, transforming and aggressively optimizing stylesheets._
CSS has evolved considerably. Modern stylesheets contain nested rules, custom properties, advanced color functions and increasingly sophisticated syntax. Processing them requires more than a collection of regular expressions and string replacements.
css-parser is a TypeScript library designed to understand and process CSS at a structural level. It combines parsing, validation, transformation and minification in a single package, with an extensible, strongly typed AST API for developers building their own CSS tools.
Beyond parsing and minification
css-parser provides a comprehensive set of tools for working with CSS:
- Parsing: A fault-tolerant parser based on the CSS Syntax Module Level 3 specification.
- Validation: CSS validation using W3C specification data.
- Transformation: CSS nesting, modern color functions, custom functions, mixins and other syntax transformations.
- Minification: Advanced optimizations including shorthand computation, mathematical expression evaluation, declaration removal and color normalization.
- AST manipulation: Traverse, inspect and modify stylesheets through a structured, typed API.
- CSS Modules and source maps: Support for modern development workflows.
The library also includes an advanced color engine supporting color spaces and functions such as oklab(), oklch(), color-mix() and relative colors.
An AST built for extensibility
At the heart of css-parser is an arena-based AST architecture.
Instead of representing every node as an independent JavaScript object, version 2 stores node and token data in typed arrays, referencing structures through numeric identifiers.
The AST API provides developers with typed access to CSS structures, making it possible to implement custom visitors, inspect declarations, modify selectors and build their own transformations.
This makes css-parser a foundation for CSS compilers, analyzers, optimization tools and development environments.
Minification: putting the results to the test
One of css-parser's main strengths is its minification quality.
The project includes an interactive benchmark comparing its output against six established tools:
- Lightning CSS
- cssnano
- csso
- clean-css
- css-tree
- esbuild
The benchmark uses real-world stylesheets, including Bootstrap, Tailwind CSS, Foundation, Font Awesome and Animate.css.
Some results against Lightning CSS are particularly interesting:
| Stylesheet | Lightning CSS | css-parser |
|---|---|---|
| Tailwind CSS | 1,864,728 B | 1,624,148 B |
| Bootstrap 4 | 153,616 B | 144,391 B |
| Bootstrap 5 | 159,987 B | 150,764 B |
Across the complete benchmark suite, the reported total output is:
| Output size | |
|---|---|
| Lightning CSS | 2,494,113 bytes |
| css-parser | 2,148,783 bytes |
| Difference | 345.33 KB |
That's approximately 13.84% less CSS compared with Lightning CSS.
Minification quality can also be traded against execution speed. css-parser provides different optimization configurations, allowing developers to choose the balance that best suits their build pipeline.
Explore the complete results in the interactive benchmark.
How does it compare?
Each competing tool has its own strengths:
| Library | Primary focus |
|---|---|
| Lightning CSS | Fast CSS parsing, transformation and minification |
| cssnano | Modular PostCSS-based optimization |
| csso | Structural CSS optimization |
| clean-css | CSS minification |
| css-tree | CSS parsing, analysis and generation |
| esbuild | Fast bundling and compilation |
css-parser combines parsing, validation, transformation and optimization in one TypeScript library, while emphasizing the quality of the generated CSS.
Rather than replacing every specialized tool, it offers an integrated alternative for developers who want a comprehensive CSS processing API.
Get started
css-parser has no runtime dependencies and supports Node.js, Deno, Bun and browsers.
Installation:
bash
npm install @tbela99/css-parser A simple minification example:
typescript
import { transformSync } from "@tbela99/css-parser"; const result = transformSync({ input: ` .example { color: red; margin: 0px; } `, minify: true }); console.log(result.code); Explore css-parser
css-parser combines modern CSS syntax support, a typed and extensible AST, and powerful optimization capabilities in a single TypeScript library.
If you're building CSS tooling or want more control over how your stylesheets are processed and optimized, give it a try.
- NPM: https://www.npmjs.com/package/@tbela99/css-parser
- GitHub: https://github.com/tbela99/css-parser
- Documentation: https://tbela99.github.io/css-parser/docs/
- Online playground: https://tbela99.github.io/css-parser/playground/
- Benchmark: https://tbela99.github.io/css-parser/benchmark/index.html