Lightning CSS
Rust-based CSS parser, transformer, and minifier with a Node API for transforming, bundling, and lowering modern CSS.
Official Documentation ↗v1. Check the official docs for the latest API surface.Installation
npm install --save-dev lightningcsspnpm add -D lightningcssMethods
No methods match this filter.
Overview
Lightning CSS is a Rust-based CSS parser and minifier with a Node API for transforming, bundling, and lowering modern CSS to target browsers. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
transform— Transforms a CSS buffer, applying targets and returning code plus referenced assets.bundle— Resolves @import and inlines referenced CSS into a single output with assets.transformStyleAttribute— Transforms inline style attribute declarations rather than full stylesheets.
Example
const { transform, browserslistToTargets } = require('lightningcss');
const out = transform({
filename: 'a.css',
code: Buffer.from('a { color: oklch(50% 0.1 200); }'),
minify: true,
});
Notes
Transforms are driven by explicit browser targets, so quality depends on correct targets. The API works on Buffers rather than strings.
Verification
Transform a stylesheet using a modern color function and confirm it is lowered to a value the declared targets support.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://lightningcss.dev/docs.html · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:39.618Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- However, sometimes you may encounter a third party library that you can’t easily modify, which unintentionally contains invalid syntax, or IE-specific hacks. (https://lightningcss.dev/docs.html)
- In these cases, you can enable the errorRecovery option (or –error-recovery CLI flag). (https://lightningcss.dev/docs.html)
- This will skip over invalid rules and declarations, omitting them in the output, and producing a warning instead of an error. (https://lightningcss.dev/docs.html)
FAQ
- How do I specify browser targets?
- Use browserslistToTargets(browserslist(...)) from the package, or pass a targets object with per-engine minimum versions and features.
- Does Lightning CSS handle vendor prefixes?
- Yes. It adds prefixes implied by your targets and by the features you enable, similar to Autoprefixer.
- How are CSS modules supported?
- Enable cssModules in transform options; the result includes an exports map from original class names to hashed names.
Report a problem with this API docOpens a pre-filled issue on GitHub.