N Next.js 中文
原文 ↗

useLightningcss

启用对 Lightning CSS 的实验性支持。

更新于 2026-03-11Next.js 16.4.6

此功能目前处于实验阶段,可能会发生变化,不建议用于生产环境。请试用并通过 GitHub 分享你的反馈。

在 webpack 中使用 Lightning CSS 的实验性支持。Lightning CSS 是一个用 Rust 编写的快速 CSS 转换器和压缩器。

如果未设置此选项,基于 webpack 的 Next.js 默认使用 PostCSS 和 postcss-preset-env。

自 Next 14.2 起,Turbopack 默认使用 Lightning CSS。此配置选项对 Turbopack 没有影响。Turbopack 始终使用 Lightning CSS。

TypeScript

next.config.tsts
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  experimental: {
    useLightningcss: false, // default, ignored on Turbopack
  },
}

export default nextConfig

JavaScript

next.config.jsjs
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    useLightningcss: true, // disables PostCSS on webpack
  },
}

module.exports = nextConfig

lightningCssFeatures

默认情况下,Lightning CSS 会根据你的 browserslist 目标决定要转译哪些 CSS 特性。lightningCssFeatures 选项允许你覆盖此行为,无论浏览器支持情况如何,都强制始终转译特定特性(include)或永不转译特定特性(exclude)。

这适用于 webpack(启用 useLightningcss 时)和 Turbopack。

TypeScript

next.config.tsts
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  experimental: {
    useLightningcss: true,
    lightningCssFeatures: {
      // Always transpile these features, even if targets support them
      include: ['light-dark', 'oklab-colors'],
      // Never transpile these features, even if targets don't support them
      exclude: ['nesting'],
    },
  },
}

export default nextConfig

JavaScript

next.config.jsjs
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    useLightningcss: true,
    lightningCssFeatures: {
      // Always transpile these features, even if targets support them
      include: ['light-dark', 'oklab-colors'],
      // Never transpile these features, even if targets don't support them
      exclude: ['nesting'],
    },
  },
}

module.exports = nextConfig

选项

选项 类型 描述
include string[] 无论浏览器目标如何,始终转译的特性。
exclude string[] 即使浏览器目标要求,也永不转译的特性。

可用的特性

各项特性:

特性名称 描述
nesting CSS 嵌套
not-selector-list 带有多个选择器的 :not
dir-selector :dir() 选择器
lang-selector-list :lang() 多语言
is-selector :is() 选择器
text-decoration-thickness-percent text-decoration-thickness 中的百分比值
media-interval-syntax 媒体查询范围区间语法
media-range-syntax 媒体查询范围语法(width >= 600px)
custom-media-queries @custom-media 规则
clamp-function clamp() 函数
color-function color() 函数
oklab-colors oklab() 和 oklch() 颜色
lab-colors lab() 和 lch() 颜色
p3-colors Display P3 颜色
hex-alpha-colors 带 alpha 通道的 4 位和 8 位十六进制颜色
space-separated-color-notation 以空格分隔的颜色表示法(rgb(0 0 0))
font-family-system-ui system-ui 字体系列
double-position-gradients 双位置渐变断点
vendor-prefixes 带厂商前缀的属性和值
logical-properties 逻辑属性和值
light-dark light-dark() 颜色函数

复合组(一次性启用多个特性的简写):

组名称 包含
selectors nesting, not-selector-list, dir-selector, lang-selector-list, is-selector
media-queries media-interval-syntax, media-range-syntax, custom-media-queries
colors color-function, oklab-colors, lab-colors, p3-colors, hex-alpha-colors, space-separated-color-notation, light-dark

版本历史

版本 变更
16.2.0 新增了 lightningCssFeatures。
15.1.0 Turbopack 已移除对 useSwcCss 的支持。
14.2.0 Turbopack 的默认 CSS 处理器已从 @swc/css 改为 Lightning CSS。在 Turbopack 上,useLightningcss 会被忽略,并新增了一个旧版的 experimental.turbo.useSwcCss 选项。