Registry / web-framework / lightningcss

lightningcss

JSON →
library0.3.1jsnpmunverified

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.

npm install lightningcss
INSTALL
IMPORT
SIG · LIGHTNINGCSS
L
lightningcss
web-frameworkjavascriptv0.3.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

transform
import { transform } from 'lightningcss';
const { transform } = require('lightningcss');
The `lightningcss` package is primarily distributed as an ESM module, though CJS is available. For modern Node.js and bundler environments, prefer ESM imports. For CJS, ensure your toolchain supports it or use dynamic import.
bundle
import { bundle } from 'lightningcss';
const bundle = require('lightningcss').bundle;
`bundle` is used for processing `@import` rules, which might require asynchronous operations (e.g., file system access). It can be used directly or passed to a build tool's configuration.
browserslistToTargets
import { browserslistToTargets } from 'lightningcss';
This utility function converts a Browserslist query into the specific target format expected by `lightningcss` functions like `transform` and `bundle`. It's essential for configuring browser compatibility.

This example demonstrates how to use `lightningcss` to parse, minify, apply vendor prefixes, and lower modern CSS syntax (like nesting, custom media, and advanced color functions) for broad browser compatibility, while generating a source map.

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();
lightningcss --version
Debug
Known issues
breakingIn 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.
fix
Review any relative color calculations (e.g., `lab(from purple calc(l * .8) a b)`) that use percentages and update them to use numbers where appropriate, following the updated CSS Color Level 5 specification.
affects: >=1.30.0
gotchaThe `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.
fix
Ensure your Node.js environment is version 12.0.0 or higher. You can update Node.js using `nvm` or by installing a newer version directly.
affects: <12.0.0
gotchaDue 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.
fix
If encountering installation or runtime errors on specific platforms, first ensure you are on the latest patch version of `lightningcss`. If issues persist, check the GitHub issues for similar reports or open a new one with your environment details.
affects: all
gotchaWhen 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.
fix
Provide a custom `resolver` function to the `bundle` options if your `@import` paths are not standard file system paths, or if you need to handle external imports. Ensure all imported CSS files exist and are accessible.
affects: all
deprecatedThe `@value` at-rule of CSS Modules has been deprecated and `lightningcss` will issue a warning if it encounters it during processing.
fix
Migrate from the deprecated `@value` at-rule to modern CSS Custom Properties or other CSS Module features that align with current specifications.
affects: all
Errors
Common errors & fixes
Error: Cannot find module 'lightningcss'
Attempting to import `lightningcss` using CommonJS `require()` syntax in an environment configured for ESM, or an incorrect package path.
fix
Ensure 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'
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.
fix
Check 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.
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.
fix
Ensure 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.
Upgrade
Version history
0.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
lightningcss — npm install lightningcss · libregistry