PostCSS 架构(PostCSS Architecture)

¥PostCSS Architecture

PostCSS 架构的总体概述。对于每个希望为核心做出贡献或更好地理解该工具的人来说,它都是有用的。

¥General overview of the PostCSS architecture. It can be useful for everyone who wishes to contribute to the core or develop a better understanding of the tool.

目录

¥Table of Contents

概述(Overview)

¥Overview

本节描述 PostCSS 背后的想法

¥This section describes ideas lying behind PostCSS

在深入探讨 PostCSS 的开发之前,我们先简要介绍一下什么是 PostCSS,什么不是。

¥Before diving deeper into the development of PostCSS let's briefly describe what is PostCSS and what is not.

PostCSS

工作流程(Workflow)

¥Workflow

这是整个 PostCSS 工作流程的高级概述

¥This is a high-level overview of the whole PostCSS workflow

从上图中可以看出,PostCSS 架构非常简单,但其中的某些部分可能会被误解。

¥As you can see from the diagram above, PostCSS architecture is pretty straightforward but some parts of it could be misunderstood.

你可以看到一个叫做 Parser 的部分,这个结构将在稍后详细描述,现在把它想象成一个可以理解你的 CSS 语法并创建它的对象表示的结构。

¥You can see a part called Parser, this construct will be described in details later on, just for now think about it as a structure that can understand your CSS like syntax and create an object representation of it.

话虽如此,编写解析器的方法很少。

¥That being said, there are few ways to write a parser.

我们来思考一下为什么第二种方式更能满足我们的需求。

¥Let's think about why the second way is better for our needs.

首先,因为字符串到标记步骤比解析步骤花费更多时间。我们对大型源字符串进行操作并逐个字符地处理它,这就是为什么它在性能方面是非常低效的操作,我们应该只执行一次。

¥First of all, because string to tokens step takes more time than parsing step. We operate on large source string and process it char by char, this is why it is very inefficient operation in terms of performance and we should perform it only once.

但从其他侧令牌到 AST 的转换在逻辑上更加复杂,因此通过这种分离,我们可以编写非常快的分词器(但有时难以阅读代码)和易于阅读(但速度慢)的解析器。

¥But from other side tokens to AST transformation is logically more complex so with such separation we could write very fast tokenizer (but from this comes sometimes hard to read code) and easy to read (but slow) parser.

总结起来分为两个步骤可以提高性能和代码可读性。

¥Summing it up splitting into two steps improve performance and code readability.

现在让我们更仔细地了解在 PostCSS 工作流程中起主要作用的结构。

¥So now let's look more closely on structures that play the main role in PostCSS workflow.

核心结构(Core Structures)

¥Core Structures

API 参考(API Reference)

¥API Reference

此处 可以找到更多描述性 API 文档

¥More descriptive API documentation could be found here