# PostCSS

**PostCSS** 是一个 [JavaScript](https://www.edgepedia.org/zh/javascript) 框架，通过插件来转换 CSS（以及类 CSS 语法）：它将样式表解析为抽象语法树，让每个插件检查并修改这棵树，再把结果序列化回 CSS。其自身的架构文档指出，PostCSS 不是 Sass 或 Less 那样的样式预处理器；它不定义任何自定义样式表语言，任何合法的 CSS 都能被它处理。<sup>[1](https://postcss.org/docs/postcss-architecture)</sup> 该项目由安德烈·西特尼克创建，他也是 Autoprefixer 的作者。<sup>[2](https://medium.com/evil-martians/postcss-2nd-birthday-2c76c223c78f)</sup>

| 关键事实 | 详情 |
|---|---|
| 它是什么 | 一个做 CSS 语法转换的工具，一个用来构建 CSS 操作工具的框架；不是预处理器语言<sup>[1](https://postcss.org/docs/postcss-architecture)</sup> |
| 起源 | 首次提交于 2013 年 9 月 7 日；Autoprefixer 于 2013 年 12 月 22 日完成从 Rework 到 PostCSS 的迁移<sup>[2](https://medium.com/evil-martians/postcss-2nd-birthday-2c76c223c78f)</sup> |
| 处理流水线 | 源字符串 → 词法单元（词法分析器）→ 抽象语法树（解析器）→ 插件转换（处理器）→ CSS 字符串（序列化器）<sup>[1](https://postcss.org/docs/postcss-architecture)</sup> |
| 抽象语法树节点类型 | `Root`、`AtRule`、`Rule`、`Declaration`、`Comment`<sup>[3](https://postcss.org/docs/writing-a-postcss-plugin)</sup> |
| 基于它构建的知名工具 | Autoprefixer、Stylelint、cssnano、postcss-preset-env<sup>[1](https://postcss.org/docs/postcss-architecture)</sup><sup> • </sup><sup>[4](https://postcss.org/docs/postcss-plugins)</sup> |
| 当前发布线 | PostCSS 8；所列最新版本为 8.5.29，由安德烈·西特尼克签署<sup>[5](https://github.com/postcss/postcss/releases)</sup> |
| 采用情况 | 在 2016 年的一项开发者调查中，8.31% 的受访者使用它；由于与其他工具组合使用的情况未被计入，实际比例可能更高<sup>[6](https://lib.uty.ac.id/repository/journal/Information%20an%20open%20acces%20journal%20MDPI/vol%209%202018/issue%201%20jan/information-09-00017.pdf)</sup> |

## PostCSS 是什么，不是什么

PostCSS 占据的生态位与 Sass 或 Less 不同。那些预处理器定义了带有变量、混入和嵌套的样式表语言，再编译成 CSS。PostCSS 则处理 CSS 语法本身：它解析样式表并运行插件来转换它们，其文档明确表示它“不是像 `Sass` 或 `Less` 那样的样式预处理器”。<sup>[1](https://postcss.org/docs/postcss-architecture)</sup> 由于它工作在语法层面而非某种自定义语言上，它也能处理类 CSS 语法；解析器 API 是一个接收字符串并返回 `Root` 或 `Document` 节点的函数，因此 SCSS 之类的自定义语法可以插进来。<sup>[7](https://github.com/postcss/postcss/blob/main/docs/syntax.md)</sup>

在实践中，两种方法常常结合使用：一条流水线可以先运行 Sass，再对编译产物应用 Autoprefixer 等 PostCSS 插件。<sup>[6](https://lib.uty.ac.id/repository/journal/Information%20an%20open%20acces%20journal%20MDPI/vol%209%202018/issue%201%20jan/information-09-00017.pdf)</sup> PostCSS 也可以通过嵌套、变量和混入相关的插件，替代预处理器中某些便利功能，但一组插件并不能复现 Sass 完整的语法或语义。<sup>[8](https://www.itechguides.com/how-to-use-postcss-as-a-configurable-alternative-to-sass/)</sup>

## 架构

PostCSS 将处理过程拆分为四个协同工作的结构。<sup>[1](https://postcss.org/docs/postcss-architecture)</sup>

**词法分析器。** 词法分析器把源字符串转换为词法单元——描述语法片段（如一条 at 规则、一条注释或一个单词）的简单结构。它向解析器暴露一个流式的 `nextToken()` 接口，一次只存储少量词法单元而非整个列表，从而降低内存占用。项目的架构说明给出了这样拆分的原因：把字符串转换为词法单元是缓慢的、逐字符进行的步骤，应当只运行一次；而把词法单元转换为抽象语法树在逻辑上更为复杂，因此快速的词法分析器可以与可读性强的解析器搭配。<sup>[1](https://postcss.org/docs/postcss-architecture)</sup>

**解析器。** 解析器执行语法分析，产出供插件转换的抽象语法树。它基于词法单元而非源字符串工作，使用词法分析器的 `nextToken` 和 `back` 方法来构建节点；所有节点类型都继承自一个基类 `Node`。<sup>[1](https://postcss.org/docs/postcss-architecture)</sup>

**处理器。** 处理器是一个小型结构，负责初始化插件并运行语法转换，只暴露少量公共 API 方法。<sup>[1](https://postcss.org/docs/postcss-architecture)</sup>

**序列化器。** 序列化器是一个基类，把修改后的抽象语法树转回纯 CSS 字符串，从给定的节点开始遍历这棵树，并调用与各节点类型对应的方法；它的核心方法是 `Stringifier.stringify`。<sup>[1](https://postcss.org/docs/postcss-architecture)</sup>

每个节点还携带一个 `raws` 对象，用来存储不属于 CSS 结构的源相关数据，例如空白符号，从而在转换过程中保留格式。<sup>[7](https://github.com/postcss/postcss/blob/main/docs/syntax.md)</sup>

## 编写与运行插件

大多数插件做两件事：在 CSS 中找到某些东西，比如一个 `will-change` 属性，然后修改找到的元素，例如在它前面插入 `transform: translateZ(0)` 作为面向旧浏览器的 polyfill。<sup>[3](https://postcss.org/docs/writing-a-postcss-plugin)</sup> 插件在其插件对象上为所关心的节点类型注册监听方法，并对特定名称的声明或 at 规则提供简写形式，例如所有 `color` 声明或所有 `@media` at 规则。<sup>[3](https://postcss.org/docs/writing-a-postcss-plugin)</sup>

监听器分两种：进入监听器（`Once`、`Root`、`AtRule`、`Rule`）在节点的子节点被处理之前运行，退出监听器（`OnceExit`、`RootExit`、`AtRuleExit`、`RuleExit`）在所有子节点处理完毕之后运行。<sup>[3](https://postcss.org/docs/writing-a-postcss-plugin)</sup> 该 API 还定义了错误处理：插件可以通过 `node.error()` 抛出错误，解析失败会产生一个 `CssSyntaxError`，携带文件、行号和列号；当输入来自 Sass 之类的上游编译器时，会用源映射报告原始位置。<sup>[9](https://postcss.org/api/)</sup>

## 插件生态

大量工具构建在 PostCSS 之上；项目自己的文档举了 Autoprefixer、[Stylelint](https://www.edgepedia.org/zh/stylelint) 和 cssnano 作为例子，并指出许多开发者是通过构建工具间接使用 PostCSS 的。<sup>[1](https://postcss.org/docs/postcss-architecture)</sup> 官方插件目录将插件分为若干类别，包括控制、套件、未来 CSS 语法、回退、语言扩展、颜色、图像与字体、网格、优化和分析。<sup>[4](https://postcss.org/docs/postcss-plugins)</sup>

**Autoprefixer** 解析 CSS 并使用 Can I Use 的数据添加厂商前缀，由 Browserslist 配置驱动（例如 `.browserslistrc` 文件中的 `> 5%` 这类查询，或 `package.json` 中的 `browserslist` 键），该配置可以与 Babel、Stylelint 等其他工具共享。它默认添加前缀，也会移除过时的前缀；其 IE 网格转换功能默认关闭，因为它并非在所有情况下都有效。<sup>[10](https://github.com/postcss/autoprefixer)</sup>

**postcss-preset-env** 把现代 CSS 转换为大多数浏览器能理解的语法，根据目标浏览器选择 polyfill，并内置了 Autoprefixer，因此只有当浏览器支持列表需要时才应用前缀。<sup>[4](https://postcss.org/docs/postcss-plugins)</sup><sup> • </sup><sup>[11](https://www.npmjs.com/package/postcss-preset-env?activeTab=readme)</sup> **cssnano** 是一组用于为生产环境优化 CSS 体积的插件套件。<sup>[4](https://postcss.org/docs/postcss-plugins)</sup>

## 构建工具中的 PostCSS

构建工具直接集成 PostCSS。[Vite](https://www.edgepedia.org/zh/vite) 预配置为通过 `postcss-import` 插件支持 CSS `@import` 内联，并把任何 `.module.css` 文件视为 CSS Modules 文件；自 Vite 2.7 起，它在生产构建中默认使用 [Lightning CSS](https://www.edgepedia.org/zh/lightning-css) 压缩 CSS，但其他 CSS 处理仍使用 PostCSS，并实验性地支持完全用 Lightning CSS 进行处理。<sup>[12](https://vite.dev/guide/features.html)</sup> `postcss-loader` 和 `postcss-cli` 等运行器在 webpack 和命令行工作流中调用 PostCSS。<sup>[13](https://github.com/postcss/postcss/wiki/PostCSS-8-for-end-users)</sup>

## 版本历史与替代方案

PostCSS 8 放弃了对旧版 Node.js 的支持，但没有改变终端用户 API，并且支持为 PostCSS 7 编写的插件；只有运行器工具（如 `postcss-loader` 或 `postcss-cli`）需要随 `postcss` 依赖一起更新。<sup>[13](https://github.com/postcss/postcss/wiki/PostCSS-8-for-end-users)</sup> 8.5.x 系列的发布持续带来对解析、列表处理和源映射行为的修复。<sup>[5](https://github.com/postcss/postcss/releases)</sup>

较新的主要替代方案是 **Lightning CSS**，一个基于 Rust 的解析器、转换器、打包器和压缩器，声称比同类基于 JavaScript 的工具快 100 倍以上，并在一个工具中处理浏览器目标转换和厂商前缀；Vite 采用它进行压缩正说明了这种竞争。<sup>[14](https://lightningcss.dev/)</sup><sup> • </sup><sup>[12](https://vite.dev/guide/features.html)</sup>

2018 年一项关于 CSS 预处理工具的学术调查总结了 PostCSS 相对 Sass 的取舍：优势在于速度、模块化（开发者只引入自己需要的功能）以及能立即实现新功能而不必等待 Sass 更新；缺点在于规划复杂度增加，因为插件必须按特定顺序调用，语法与 Sass 不同，并且要求其处理输入必须是合法的 CSS。<sup>[6](https://lib.uty.ac.id/repository/journal/Information%20an%20open%20acces%20journal%20MDPI/vol%209%202018/issue%201%20jan/information-09-00017.pdf)</sup> 在该调查分析的 2016 年前端工具调查中，Sass 是 63.39% 受访者的首选处理工具，Less 为 10.14%，PostCSS 为 8.31%；调查指出 PostCSS 的实际使用率可能更高，因为组合使用的情况未被计入。<sup>[6](https://lib.uty.ac.id/repository/journal/Information%20an%20open%20acces%20journal%20MDPI/vol%209%202018/issue%201%20jan/information-09-00017.pdf)</sup>

## References

1. [PostCSS Architecture, postcss.org](https://postcss.org/docs/postcss-architecture)
2. [Andrey Sitnik, "PostCSS Second Birthday," Evil Martians, 2015](https://medium.com/evil-martians/postcss-2nd-birthday-2c76c223c78f)
3. [Writing a PostCSS Plugin, postcss.org](https://postcss.org/docs/writing-a-postcss-plugin)
4. [PostCSS Plugins, postcss.org](https://postcss.org/docs/postcss-plugins)
5. [Releases · postcss/postcss, GitHub](https://github.com/postcss/postcss/releases)
6. ["CSS Preprocessing: Tools and Automation Techniques," Information 9(1):17, MDPI, 2018](https://lib.uty.ac.id/repository/journal/Information%20an%20open%20acces%20journal%20MDPI/vol%209%202018/issue%201%20jan/information-09-00017.pdf)
7. [postcss/docs/syntax.md, GitHub](https://github.com/postcss/postcss/blob/main/docs/syntax.md)
8. ["How to Use PostCSS as a Configurable Alternative to Sass," iTechGuides](https://www.itechguides.com/how-to-use-postcss-as-a-configurable-alternative-to-sass/)
9. [PostCSS API, postcss.org](https://postcss.org/api/)
10. [postcss/autoprefixer, GitHub](https://github.com/postcss/autoprefixer)
11. [postcss-preset-env, npm](https://www.npmjs.com/package/postcss-preset-env?activeTab=readme)
12. [Features, Vite guide](https://vite.dev/guide/features.html)
13. [PostCSS 8 for end users, postcss/postcss Wiki, GitHub](https://github.com/postcss/postcss/wiki/PostCSS-8-for-end-users)
14. [Lightning CSS](https://lightningcss.dev/)

---
*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/postcss

English original: https://www.edgepedia.org/postcss
