# Lightning CSS

**Lightning CSS** 是一款用 [Rust](https://www.edgepedia.org/zh/rust-programming-language) 编写的极速 CSS 解析器、转换器、打包器和压缩器，Rust 也是 Parcel 打包器背后的系统编程语言。它能够解析和转换样式表，为旧版浏览器编译现代 CSS 语法，添加浏览器厂商前缀，打包 `@import` 规则，并压缩输出；既可以在 Parcel 中使用，也可以作为独立的 [JavaScript](https://www.edgepedia.org/zh/javascript) 或 Rust 库、通过独立的命令行工具，或作为其他构建工具中的插件来使用。<sup>[1](https://github.com/parcel-bundler/lightningcss)</sup><sup> • </sup><sup>[2](https://lightningcss.dev/)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 用 Rust 编写的 CSS 解析器、转换器、打包器和压缩器，以 npm 包 `lightningcss` 的形式发布，采用 MPL-2.0 许可证<sup>[1](https://github.com/parcel-bundler/lightningcss)</sup><sup> • </sup><sup>[3](https://github.com/parcel-bundler/lightningcss/blob/master/package.json)</sup> |
| 解析器基础 | 构建在 Mozilla 创建、Firefox 和 Servo 使用的 `cssparser` 和 `selectors` 程序包之上<sup>[1](https://github.com/parcel-bundler/lightningcss)</sup> |
| 浏览器目标 | 将嵌套、自定义媒体查询、高色域色彩空间、逻辑属性和新的选择器特性转换为面向所配置浏览器目标的兼容语法，并自动添加浏览器厂商前缀<sup>[2](https://lightningcss.dev/)</sup> |
| 采用情况 | Vite 默认用它来压缩生产构建中的 CSS；Tailwind CSS 在底层用它处理嵌套 CSS、打包导入和添加前缀；Parcel 用它进行 CSS 压缩<sup>[4](https://vite.dev/guide/features.html)</sup><sup> • </sup><sup>[5](https://tailwindcss.com/docs/compatibility)</sup><sup> • </sup><sup>[6](https://parceljs.org/languages/css/)</sup> |
| CSS Modules | 支持局部作用域的类和 id 选择器、局部作用域的自定义标识符（如 `@keyframes` 名称）、`:local()` 和 `:global()`，以及 `composes` 属性<sup>[1](https://github.com/parcel-bundler/lightningcss)</sup> |
| 速度 | 在一份 Netflix 生产环境样式表上，lightningcss 在 16 毫秒内完成，产出 29,767 个 gzip 字节；而 cssnano 耗时 332 毫秒，产出 32,749 字节。该项目声称比 CSSNano 快 100 倍以上<sup>[7](https://gazagnaire.org/blog/2026-07-22-cascade-minify.html)</sup><sup> • </sup><sup>[8](https://www.gjensidige.builders/_/attachment/download/0fced6b6-251b-451a-9f0b-10ace2000765%3A5acfdb1363e920ccc2bc4d1b75a19e36bcee1036/slides-export.pdf)</sup> |
| 当前版本 | v1.33.0，于 7 月 20 日发布，新增对 `::highlight` 伪元素和 `@position-try` at-rule 的支持<sup>[9](https://github.com/parcel-bundler/lightningcss/releases)</sup> |

## 设计

**带类型的属性值。** 许多 CSS 解析器将属性值视为无类型的令牌序列，这迫使每个转换器都要重新解释这些值，造成重复工作和不一致。Lightning CSS 则不同，它使用 CSS 规范中的语法来解析所有值，并为每个属性暴露特定的值类型，因此转换操作可以作用于真正的 `Length` 或 `Color`，而非原始令牌。<sup>[1](https://github.com/parcel-bundler/lightningcss)</sup>

该解析器直接支持现代语法，包括双值 `overflow` 简写、媒体查询范围语法（如 `@media (100px < width < 500px)`）、多值 `display` 属性（例如 `inline flex`），以及 `system-ui` 字体族回退。<sup>[1](https://github.com/parcel-bundler/lightningcss)</sup>

## 浏览器目标与压缩

根据已配置的浏览器目标，Lightning CSS 会自动将较新的 CSS 能力——嵌套、自定义媒体查询、高色域色彩空间、逻辑属性以及近期新增的选择器特性——转换为更广泛支持的语法，同时应用相应的浏览器厂商前缀，使源代码保持整洁、无冗余。<sup>[2](https://lightningcss.dev/)</sup> 压缩功能可通过 API 中的 `minify` 选项或命令行中的 `--minify` 标志启用。压缩器还会移除所配置目标不需要的厂商前缀属性；在为现代浏览器编译时，`transition` 属性的前缀版本会被移除，因为所有目标浏览器都支持无前缀版本。在重新排序可能改变编译行为的地方，规则顺序会被保留。<sup>[10](https://lightningcss.dev/minification.html)</sup>

## 打包

调用 `bundle` 或 `bundleAsync` API 函数会将 `@import` 规则内联到单个样式表中。条件导入会被保留：`@import "b.css" print;` 编译后，被导入的规则会包裹在 `@media print` 中，而 `supports()` 查询则变为 `@supports` 块。被导入的规则还可以放入 CSS 级联层中，嵌套导入则放入嵌套层。当文件通过 CSS Modules 组合或外部变量相互引用时，依赖项也会被打包。<sup>[11](https://third-party-mirror.googlesource.com/lightningcss/%2B/refs/heads/master/website/pages/bundling.md)</sup>

某些组合不受支持并会发出编译器错误，包括以不同的层名导入同一文件，以及嵌套匿名层。<sup>[11](https://third-party-mirror.googlesource.com/lightningcss/%2B/refs/heads/master/website/pages/bundling.md)</sup>

## CSS Modules

Lightning CSS 编译 CSS Modules 的一个子集特性：局部作用域的类和 id 选择器，局部作用域的自定义标识符（如 `@keyframes` 名称、网格线和区域、`@counter-style` 名称），可选的 CSS 变量和其他短横线标识符作用域，`:local()` 和 `:global()` 选择器，以及 `composes` 属性。CSS Modules 项目称 Lightning CSS 支持 CSS Modules 的几乎所有特性。<sup>[1](https://github.com/parcel-bundler/lightningcss)</sup><sup> • </sup><sup>[12](https://github.com/css-modules/css-modules/blob/master/docs/get-started.md)</sup>

## 自定义转换

访问器 API 允许开发者用 JavaScript 实现自定义转换插件，用于非标准扩展，如自定义简写属性、额外的 at-rule（如混入），以及构建时转换（如单位换算或内联常量）。访问器是针对特定值类型（如 `Rule`、`Property` 或 `Length`）调用的函数，明确所处理的类型可以保持 JavaScript 调用次数较低。使用 JavaScript 访问器进行编译的耗时可能是不使用时的约 2 倍，因此为浏览器兼容性而做的标准转换是在 Lightning CSS 内部用 Rust 实现的。未知的 at-rule 以原始令牌形式存储在 AST 中，以便访问器可以解释它们；访问器可以返回一个 `raw` 字符串，Lightning CSS 会将其重新解析到 AST 中。<sup>[13](https://lightningcss.dev/transforms.html)</sup>

## 采用情况

**构建工具。** Vite 默认使用 Lightning CSS 压缩生产构建中的 CSS，而 [PostCSS](https://www.edgepedia.org/zh/postcss) 负责其他 CSS 处理；通过 `css.transformer: 'lightningcss'` 可以实验性地完全用作转换器，选项通过 `css.lightningcss` 传入。<sup>[4](https://vite.dev/guide/features.html)</sup> [Tailwind CSS](https://www.edgepedia.org/zh/tailwind-css) 在底层使用 Lightning CSS 处理嵌套 CSS、打包导入和添加浏览器厂商前缀，其 `optimize()` 路径会调用 Lightning CSS，结合 browserslist 集成进行降级处理。<sup>[5](https://tailwindcss.com/docs/compatibility)</sup> Parcel 默认使用 Lightning CSS 进行 CSS 压缩，`@parcel/optimizer-cssnano` 可作为替代方案。<sup>[6](https://parceljs.org/languages/css/)</sup>

**基准测试中的性能。** 一项针对六份真实生产样式表的独立对比测试中，lightningcss 和 esbuild 在 Netflix 样式表上均不到 16 毫秒，分别产出 29,767 和 33,799 个 gzip 字节；而 csso 和 cssnano 分别耗时 140 和 332 毫秒，产出 33,089 和 32,749 字节。在全部 72 个测试样本中，lightningcss 拒绝了一个文件；在剩余文件中，一个较慢的 OCaml 压缩器在 59 个文件上产出了更小的 gzip 输出，而 lightningcss 仅在 12 个文件上胜出，这表明其速度并非以每个文件都达到最小可能输出为代价。<sup>[7](https://gazagnaire.org/blog/2026-07-22-cascade-minify.html)</sup>

## 版本发布

该项目通过其 GitHub 仓库频繁发布版本；近期版本从 v1.28.2 到 v1.33.0。1.33.0 版本由 Devon Govett 于 7 月 20 日发布，新增了对 `::highlight` 伪元素和 `@position-try` at-rule 的支持，修复了 `list-style` 简写的解析问题，修复了 CSS Modules 中 `:nth-child(an + b of X)` 选择器的哈希计算问题，并更新了浏览器兼容性数据。<sup>[9](https://github.com/parcel-bundler/lightningcss/releases)</sup>

## References

1. [parcel-bundler/lightningcss, GitHub](https://github.com/parcel-bundler/lightningcss)
2. [Lightning CSS website](https://lightningcss.dev/)
3. [lightningcss/package.json, GitHub](https://github.com/parcel-bundler/lightningcss/blob/master/package.json)
4. [Features, Vite guide](https://vite.dev/guide/features.html)
5. [Compatibility, Tailwind CSS documentation](https://tailwindcss.com/docs/compatibility)
6. [CSS, Parcel documentation](https://parceljs.org/languages/css/)
7. [Thomas Gazagnaire, Cascade: A Typed CSS Toolkit in OCaml (minification benchmarks)](https://gazagnaire.org/blog/2026-07-22-cascade-minify.html)
8. [Faster FE-stack in 2025, Gjensidige slides](https://www.gjensidige.builders/_/attachment/download/0fced6b6-251b-451a-9f0b-10ace2000765%3A5acfdb1363e920ccc2bc4d1b75a19e36bcee1036/slides-export.pdf)
9. [Releases, parcel-bundler/lightningcss, GitHub](https://github.com/parcel-bundler/lightningcss/releases)
10. [Minification, Lightning CSS documentation](https://lightningcss.dev/minification.html)
11. [Bundling, Lightning CSS documentation](https://third-party-mirror.googlesource.com/lightningcss/%2B/refs/heads/master/website/pages/bundling.md)
12. [CSS Modules get-started documentation, css-modules/css-modules, GitHub](https://github.com/css-modules/css-modules/blob/master/docs/get-started.md)
13. [Custom Transforms, Lightning CSS documentation](https://lightningcss.dev/transforms.html)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Web development and web-platform technologies › CSS tooling and utility frameworks

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/lightning-css

English original: https://www.edgepedia.org/lightning-css
