PostCSS 简介 #

什么是 PostCSS? #

PostCSS 是一个用 JavaScript 转换 CSS 的工具。它通过插件系统实现各种 CSS 转换功能,是目前最流行的 CSS 处理工具之一。与传统的 CSS 预处理器不同,PostCSS 更像是一个 CSS 处理平台,你可以根据需要选择和组合各种插件。

核心特点 #

特点 说明
插件驱动 通过插件实现各种功能,按需组合
标准 CSS 输入输出都是标准 CSS,无需学习新语法
AST 解析 将 CSS 解析为抽象语法树,精确操作
高性能 基于 JavaScript,处理速度快
可扩展 可以轻松开发自定义插件

PostCSS 的定位 #

预处理器 vs 后处理器 #

text
┌─────────────────────────────────────────────────────────────┐
│                      CSS 处理流程                            │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  预处理器(Less/Sass)                                       │
│  ┌──────────┐    ┌──────────┐    ┌──────────┐              │
│  │ 预处理语法 │ -> │   编译器  │ -> │ 标准 CSS  │              │
│  │ (.less)  │    │          │    │  (.css)  │              │
│  └──────────┘    └──────────┘    └──────────┘              │
│                                                             │
│  后处理器(PostCSS)                                         │
│  ┌──────────┐    ┌──────────┐    ┌──────────┐              │
│  │ 标准 CSS  │ -> │ PostCSS  │ -> │ 优化 CSS  │              │
│  │  (.css)  │    │  + 插件   │    │  (.css)  │              │
│  └──────────┘    └──────────┘    └──────────┘              │
│                                                             │
└─────────────────────────────────────────────────────────────┘

PostCSS 的双重身份 #

PostCSS 既可以作为后处理器,也可以作为预处理器:

javascript
// 作为后处理器
// 输入:标准 CSS
// 输出:添加前缀、压缩后的 CSS

// 作为预处理器
// 输入:使用未来语法的 CSS
// 输出:兼容当前浏览器的 CSS

为什么使用 PostCSS? #

CSS 开发的痛点 #

传统 CSS 开发面临以下问题:

css
/* 浏览器兼容性问题 */
.button {
  display: flex;           /* 需要添加前缀 */
  user-select: none;       /* 需要添加前缀 */
  backdrop-filter: blur(10px); /* 需要添加前缀 */
}

/* 代码重复 */
.button-primary {
  border-radius: 4px;
  padding: 10px 20px;
  font-size: 14px;
  background: #007bff;
}

.button-secondary {
  border-radius: 4px;
  padding: 10px 20px;
  font-size: 14px;
  background: #6c757d;
}

/* 无法使用未来特性 */
.container {
  display: grid;
  gap: 20px;           /* 旧浏览器不支持 */
  aspect-ratio: 16/9;  /* 新特性 */
}

PostCSS 的解决方案 #

css
/* 输入:简洁的 CSS */
:root {
  --primary: #007bff;
  --secondary: #6c757d;
}

.button {
  display: flex;
  user-select: none;
}

.button-primary {
  border-radius: 4px;
  padding: 10px 20px;
  background: var(--primary);
}

.container {
  display: grid;
  gap: 20px;
  aspect-ratio: 16/9;
}
css
/* 输出:兼容性好的 CSS */
:root {
  --primary: #007bff;
  --secondary: #6c757d;
}

.button {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.button-primary {
  border-radius: 4px;
  padding: 10px 20px;
  background: var(--primary);
}

.container {
  display: -ms-grid;
  display: grid;
  gap: 20px;
  -ms-grid-gap: 20px;
  aspect-ratio: 16/9;
}

PostCSS 的历史 #

text
2013年 ─── Andrey Sitnik 创建 PostCSS
    │
    │      最初用于 Autoprefixer
    │
2014年 ─── 插件生态系统开始发展
    │
    │      cssnext、PreCSS 等出现
    │
2015年 ─── 被 Twitter、Google、Adobe 采用
    │
    │      成为前端工具链标配
    │
2016年 ─── PostCSS 6.0 发布
    │
    │      更好的 API 和性能
    │
2018年 ─── PostCSS 7.0 发布
    │
    │      改进的错误处理
    │
2021年 ─── PostCSS 8.0 发布
    │
    │      全新的插件 API
    │
至今   ─── 持续更新迭代
    │
    │      每周数百万次下载

PostCSS 的工作原理 #

AST(抽象语法树) #

PostCSS 将 CSS 解析为 AST,然后通过插件操作这棵树:

text
CSS 源码
    │
    ▼
┌─────────────────────────────────────────────────────────────┐
│                      Parser(解析器)                        │
│                                                             │
│  color: red;  ───────>  Declaration {                       │
│                            prop: 'color',                   │
│                            value: 'red'                     │
│                          }                                  │
└─────────────────────────────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────────────────────────────┐
│                    Plugin(插件处理)                        │
│                                                             │
│  Plugin 1: Autoprefixer                                     │
│  Plugin 2: cssnano                                          │
│  Plugin 3: ...                                              │
└─────────────────────────────────────────────────────────────┘
    │
    ▼
┌─────────────────────────────────────────────────────────────┐
│                    Stringifier(生成器)                     │
│                                                             │
│  Declaration { }  ───────>  color: red;                     │
└─────────────────────────────────────────────────────────────┘
    │
    ▼
CSS 输出

节点类型 #

PostCSS AST 包含以下节点类型:

节点类型 说明 示例
Root 根节点,包含整个 CSS 整个文件
AtRule @ 规则 @media, @keyframes
Rule 选择器规则 .class { }
Declaration 声明 color: red;
Comment 注释 /* comment */
javascript
// AST 结构示例
{
  type: 'root',
  nodes: [
    {
      type: 'rule',
      selector: '.button',
      nodes: [
        {
          type: 'declaration',
          prop: 'color',
          value: 'red'
        }
      ]
    }
  ]
}

PostCSS vs 其他工具 #

PostCSS vs Less #

特性 PostCSS Less
处理方式 后处理/转换 预处理
输入语法 标准 CSS 自定义语法
变量 通过插件 内置支持
嵌套 通过插件 内置支持
Mixin 通过插件 内置支持
灵活性 极高 中等
学习成本 中等

PostCSS vs Sass #

特性 PostCSS Sass
处理方式 后处理/转换 预处理
输入语法 标准 CSS SCSS/Sass
功能来源 插件 内置
可扩展性 极高 中等
社区插件 丰富 有限
编译速度 中等

PostCSS vs Lightning CSS #

特性 PostCSS Lightning CSS
语言 JavaScript Rust
速度 极快
插件系统 丰富 有限
自定义 高度可定制 较少
生态 成熟 发展中

PostCSS 的优势 #

1. 模块化 #

javascript
// 只使用需要的插件
module.exports = {
  plugins: [
    require('autoprefixer'),
    require('cssnano')
  ]
}

2. 标准 CSS #

css
/* 不需要学习新语法,直接写标准 CSS */
.container {
  display: grid;
  gap: 20px;
}

3. 丰富的插件生态 #

text
PostCSS 插件分类:

├── 兼容性处理
│   ├── autoprefixer
│   ├── postcss-preset-env
│   └── postcss-color-hex-alpha
│
├── 代码优化
│   ├── cssnano
│   ├── postcss-discard-comments
│   └── postcss-merge-rules
│
├── 开发增强
│   ├── postcss-nested
│   ├── postcss-simple-vars
│   └── postcss-mixins
│
├── 代码检查
│   ├── stylelint
│   └── postcss-bem-linter
│
└── 特殊功能
    ├── postcss-pxtorem
    ├── postcss-sprites
    └── rtlcss

4. 与构建工具集成 #

javascript
// Webpack
module.exports = {
  module: {
    rules: [
      { test: /\.css$/, use: ['postcss-loader'] }
    ]
  }
}

// Vite
// 自动支持 postcss.config.js

// Gulp
gulp.task('css', () => {
  return gulp.src('src/*.css')
    .pipe(postcss())
    .pipe(gulp.dest('dist'))
})

应用场景 #

1. 自动添加浏览器前缀 #

css
/* 输入 */
.button {
  display: flex;
  user-select: none;
}

/* 输出 */
.button {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

2. 使用未来 CSS 特性 #

css
/* 输入 */
:root {
  --primary: #007bff;
}

.button {
  color: var(--primary);
  nesting: & .icon {
    margin-right: 8px;
  }
}

/* 输出 */
:root {
  --primary: #007bff;
}

.button {
  color: var(--primary);
}

.button .icon {
  margin-right: 8px;
}

3. CSS 压缩优化 #

css
/* 输入 */
.container {
  margin: 10px 10px 10px 10px;
  padding: 0px;
}

.button {
  color: #ff0000;
}

/* 输出 */
.container{margin:10px;padding:0}.button{color:red}

4. 单位转换 #

css
/* 输入 */
.text {
  font-size: 32px;
  line-height: 48px;
}

/* 输出(使用 postcss-pxtorem) */
.text {
  font-size: 2rem;
  line-height: 3rem;
}

著名项目使用案例 #

项目 使用方式
Bootstrap 使用 Autoprefixer 处理浏览器前缀
Tailwind CSS 基于 PostCSS 构建
Create React App 内置 PostCSS 支持
Vue CLI 默认集成 PostCSS
Next.js 支持 PostCSS 配置
Vite 内置 PostCSS 支持

开发工具支持 #

VS Code 插件 #

  • PostCSS Language Support:语法高亮
  • PostCSS Sorting:属性排序
  • CSS Peek:跳转到定义
  • stylelint:代码检查

在线工具 #

下一步 #

现在你已经了解了 PostCSS 的基本概念,接下来学习 安装与配置 开始实际使用!

最后更新:2026-03-28