Lightning CSS
Lightning CSS is an exceptionally fast CSS parser, transformer, and minifier written in Rust, primarily maintained by the Parcel team. Currently at version 1.32.0, it undergoes active development with frequent minor and patch releases, ensuring up-to-date feature support and performance improvements. Its key differentiators include leveraging Rust for unparalleled performance, a browser-grade parser (based on Mozilla's `cssparser` and `selectors` crates), and sophisticated typed property values that ensure consistent and accurate transformations across various CSS features. Beyond basic minification, it performs advanced optimizations such as combining longhand properties into shorthands, merging adjacent rules, and reducing `calc()` expressions. It also provides robust vendor prefixing based on configurable browser targets (integrating with Browserslist) and comprehensive syntax lowering for modern CSS features like CSS Nesting, Custom Media Queries, and advanced Color Level 4/5 functions, enabling developers to write modern CSS while ensuring broad browser compatibility and optimized output size.
Common errors
-
Error: Cannot find module 'lightningcss'
cause Attempting to import `lightningcss` using CommonJS `require()` syntax in an environment configured for ESM, or an incorrect package path.fixEnsure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import { ... } from 'lightningcss';`. If you must use CommonJS, ensure your bundler or Node.js version is configured to correctly handle dual CJS/ESM packages, or use dynamic `import('lightningcss')`. -
Error: Unknown pseudo-class or pseudo-element '::some-new-pseudo'
cause You are using a very new or experimental CSS pseudo-class/element that `lightningcss` doesn't yet support out of the box, or it requires enabling a specific 'draft' feature.fixCheck the `lightningcss` documentation and release notes for support of the specific feature. If it's a draft feature, you might need to explicitly enable it via the `drafts` option in `transform` or `bundle` configuration (e.g., `drafts: { 'css-nesting': true }`). If it's not supported, consider a polyfill or waiting for a future release. -
Minified output does not include expected vendor prefixes or syntax lowering.
cause The `targets` option, which specifies the target browsers for prefixing and syntax lowering, is either missing, incorrect, or doesn't cover the browsers where the features need to be transformed.fixEnsure you are passing a valid `targets` array to the `transform` or `bundle` function. This often involves using `browserslistToTargets(browserslist(YOUR_BROWSERSLIST_QUERY))` to generate the correct targets. Verify your Browserslist query correctly includes the desired browsers and versions.
Warnings
- breaking In v1.30.0, the parsing of relative color syntax was updated to align with the latest CSS spec. This change specifically affects how numbers and percentages are handled in relative color calculations, where percentages might now need to be expressed as numbers. Existing code using relative color calculations with percentages may require updates.
- gotcha The `lightningcss` package has Node.js engine requirements (Node.js >= 12.0.0). Using older Node.js versions may lead to installation failures or runtime errors, as the native Rust binaries might not be compatible.
- gotcha Due to its Rust implementation, `lightningcss` ships native binaries. While generally robust, specific environments (e.g., certain Linux distributions, ARM-based systems, or Node.js worker threads) have historically seen isolated installation or runtime issues that required targeted fixes.
- gotcha When bundling CSS with `@import` rules using the `bundle` function, `lightningcss` needs to resolve these imports. Incorrect `resolver` configuration or missing files will lead to build failures or unresolved `@import` statements in the output.
- deprecated The `@value` at-rule of CSS Modules has been deprecated and `lightningcss` will issue a warning if it encounters it during processing.
Install
-
npm install lightningcss -
yarn add lightningcss -
pnpm add lightningcss
Imports
- transform
const { transform } = require('lightningcss');import { transform } from 'lightningcss'; - bundle
const bundle = require('lightningcss').bundle;import { bundle } from 'lightningcss'; - browserslistToTargets
import { browserslistToTargets } from 'lightningcss';
Quickstart
import { transform, browserslistToTargets } from 'lightningcss';
import * as browserslist from 'browserslist';
const cssInput = `
@custom-media --viewport-medium (width <= 800px);
:root {
--primary-color: oklch(50% 0.2 250);
}
.container {
display: flex;
gap: 10px;
@media (--viewport-medium) {
flex-direction: column;
}
background-color: color-mix(in srgb, var(--primary-color) 80%, black);
font-size: clamp(1rem, 2vw, 1.5rem);
}
`;
async function processCss() {
const targets = browserslistToTargets(browserslist('>= 0.25%', 'not dead'));
const { code, map } = transform({
filename: 'input.css',
code: Buffer.from(cssInput),
minify: true,
targets,
nesting: true, // Enable CSS Nesting syntax lowering
customMedia: true, // Enable Custom Media Query syntax lowering
drafts: { // Enable experimental draft features like Color Level 5
'nesting': true,
'customMedia': true,
'css-color-5': true
},
sourceMap: true
});
console.log('Minified CSS:');
console.log(code.toString());
console.log('\nSource Map (first 100 chars):');
console.log(map?.toString().substring(0, 100));
}
processCss();