Robust, expressive, and feature-rich CSS superset with dynamic preprocessing capabilities
npx @tessl/cli install tessl/npm-stylus@0.64.0Stylus is a dynamic CSS preprocessor that provides an expressive and feature-rich language for generating CSS. It offers both indented syntax (similar to Sass) and regular CSS syntax, supporting variables, mixins, conditionals, loops, built-in functions, and advanced features like transparent mixins and property lookup.
npm install stylusconst stylus = require('stylus');For ES modules:
import stylus from 'stylus';Individual components:
const { render, Renderer, Parser, nodes, functions, utils } = require('stylus');const stylus = require('stylus');
// Simple compilation with callback
stylus.render('.foo { color: red; }', (err, css) => {
if (err) throw err;
console.log(css); // Output: CSS string
});
// Using Renderer for more control
const renderer = stylus('body\n font 12px Helvetica, Arial, sans-serif')
.set('compress', true)
.include('/path/to/styles');
renderer.render((err, css) => {
if (err) throw err;
console.log(css);
});
// Synchronous compilation
const css = stylus('body\n color red').render();Stylus is built around several key components:
Main entry points for compiling Stylus source code to CSS, including the primary render function and Renderer class with extensive configuration options.
// Primary render function - returns new Renderer instance
function stylus(str, options);
// Static render with callback
function render(str, options, fn);
// Renderer class for advanced compilation control
class Renderer {
constructor(str, options);
render(fn);
set(key, value);
get(key);
include(path);
import(file);
define(name, node, raw);
use(plugin);
deps();
}Comprehensive parsing system that converts Stylus source into a structured Abstract Syntax Tree, with 40+ node types representing all language constructs.
// Parser class for source code analysis
class Parser {
constructor(str, options);
parse();
peek();
advance();
accept(type);
expect(type);
}
// Base node class - all AST nodes inherit from this
class Node {
constructor();
toJSON();
eval();
clone();
toString();
}Over 60 built-in functions organized into categories: color manipulation (rgba, hsl, lighten), mathematical operations (abs, ceil, min), string processing (substr, split, unquote), list operations (push, pop, keys), and utility functions (json, lookup, image-size).
// Color functions
function red(color);
function green(color);
function blue(color);
function rgba(r, g, b, a);
function lighten(color, amount);
function darken(color, amount);
// Math functions
function abs(n);
function ceil(n);
function floor(n);
function min(a, b);
function max(a, b);
// String functions
function length(expr);
function substr(string, start, length);
function unquote(str);
function quote(str);Comprehensive utility functions for path handling, value coercion, type checking, and development support including file lookup, type assertions, and object manipulation.
// Path utilities
function lookup(path, paths, ignore);
function find(path, paths, ignore);
function absolute(path);
function join(base, path);
// Type utilities
function coerce(left, right);
function unwrap(expr);
function assertString(node, name);
function assertColor(node, name);Complete command-line interface for file compilation, watching, CSS conversion, and build integration with options for compression, source maps, debugging, and plugin support.
# Basic compilation
stylus input.styl -o output.css
# Watch mode with compression
stylus -w -c input.styl -o output.css
# CSS to Stylus conversion
stylus --css < input.css > output.styl
# With plugins and includes
stylus -u autoprefixer-stylus -I ./includes input.stylExpress/Connect middleware for automatic Stylus compilation in web applications with configurable paths, compression, source maps, and development features.
// Middleware function
function middleware(options);
// Middleware options interface
interface MiddlewareOptions {
src: string;
dest?: string;
compile?: Function;
compress?: boolean;
firebug?: boolean;
linenos?: boolean;
sourcemap?: boolean;
force?: boolean;
}// Renderer options
interface RendererOptions {
filename?: string;
paths?: string[];
compress?: boolean;
linenos?: boolean;
firebug?: boolean;
sourcemap?: boolean | SourceMapOptions;
includeCSS?: boolean;
resolveURL?: boolean;
hoistAtrules?: boolean;
prefix?: string;
imports?: string[];
functions?: object;
globals?: object;
}
// Source map options
interface SourceMapOptions {
comment?: boolean;
inline?: boolean;
sourcesContent?: boolean;
basePath?: string;
}
// Parser options
interface ParserOptions {
filename?: string;
cache?: boolean;
compress?: boolean;
}