A lightweight CSS preprocessor that provides CSS parsing, AST manipulation, vendor prefixing, and serialization capabilities.
npx @tessl/cli install tessl/npm-stylis@4.3.0Stylis is a lightweight CSS preprocessor that provides essential CSS preprocessing capabilities including CSS nesting, selector namespacing, automatic vendor prefixing, CSS minification, and ES module compatibility. The library operates on an Abstract Syntax Tree (AST) structure offering a compile-serialize-stringify workflow for parsing CSS strings, manipulating them through middleware functions, and outputting processed CSS.
npm install stylisimport { compile, serialize, stringify } from 'stylis';For individual module imports:
import { compile, parse } from 'stylis'; // Parser functions
import { serialize, stringify } from 'stylis'; // Serializer functions
import { middleware, prefixer, namespace } from 'stylis'; // Middleware system
import { tokenize } from 'stylis'; // Tokenization utilitiesimport { compile, serialize, stringify } from 'stylis';
// Basic CSS processing
const css = 'h1 { color: red; &:hover { color: blue; } }';
const processed = serialize(compile(css), stringify);
console.log(processed); // "h1{color:red;}h1:hover{color:blue;}"
// With vendor prefixing
import { middleware, prefixer } from 'stylis';
const prefixed = serialize(
compile('div { display: flex; }'),
middleware([prefixer, stringify])
);
console.log(prefixed); // Includes -webkit-box, -ms-flexbox, etc.Stylis is built around several key components:
value, type, props, children, and metadata propertiesTransform CSS strings into Abstract Syntax Trees with support for nesting, at-rules, comments, and declarations.
function compile(value: string): object[];
function parse(
value: string,
root: object,
parent: object,
rule: string[],
rules: string[],
rulesets: string[],
pseudo: number[],
points: number[],
declarations: string[]
): object;Convert AST nodes back into CSS strings with customizable formatting and processing.
function serialize(children: object[], callback: function): string;
function stringify(element: object, index: number, children: object[], callback: function): string;Serialization and Stringification
Pluggable transformation pipeline with built-in middleware for vendor prefixing, namespacing, and custom processing.
function middleware(collection: function[]): function;
function prefixer(element: object, index: number, children: object[], callback: function): void;
function namespace(element: object): void;Character-by-character parsing utilities for custom CSS processing and analysis.
function tokenize(value: string): string[];
function alloc(value: string): any[];
function dealloc(value: any): any;Helper functions for string manipulation, array operations, and CSS processing constants.
// Constants
const COMMENT: string; // 'comm'
const RULESET: string; // 'rule'
const DECLARATION: string; // 'decl'
// Utility functions
function hash(value: string, length: number): number;
function trim(value: string): string;
function replace(value: string, pattern: string|RegExp, replacement: string): string;All AST nodes in Stylis follow a consistent structure:
interface StylisNode {
value: string; // Original CSS text
type: string; // Node type ('rule', 'decl', 'comm', or at-rule name)
props: string | string[]; // Parsed properties (selectors for rules, property name for declarations)
children: object[] | string; // Child nodes or property value
line: number; // Source line number
column: number; // Source column number
length: number; // Character length
return: string; // Generated output (used by serializers)
root: object; // Parent node in compiled output
parent: object; // Parent node in input structure
siblings: object[]; // Sibling nodes
}Stylis is designed to be fault-tolerant and will parse most CSS input without throwing errors. Invalid CSS is typically preserved in the AST structure and passed through to the output, allowing downstream tools to handle validation and error reporting.