postcss_PostCSS简介

2023-11-15

postcss

Most developers who spend their time working with CSS are familiar with preprocessors such Less, Sass, and Stylus. These tools have become a vital part of the web development ecosystem. Writing styles for a website without using features like nesting, variables, or mixins now seems cumbersome and awkward. Although each of them is a great and extremely useful product, let’s take a step back and consider if using preprocessors in such a way is indeed the best approach.

花费大量时间使用CSS的大多数开发人员都熟悉Less,Sass和Stylus等预处理器。 这些工具已成为Web开发生态系统的重要组成部分。 现在,不使用嵌套,变量或混合功能之类的网站来编写样式似乎很麻烦和尴尬。 尽管它们中的每一个都是一个出色且非常有用的产品,但让我们退后一步,考虑一下以这种方式使用预处理器是否确实是最好的方法。

I see a couple of problems with tradition preprocessors:

我看到传统预处理器有两个问题:

  • They are not extendable. Whichever preprocessor you choose, you are limited to the set of features that it provides. If you need anything on top of that, you’ll need to add it as a separate step in your build process. If you feel like writing your own extension, you’re on your own.

    它们是不可扩展的。 无论选择哪种预处理器,都只能使用它提供的功能集。 如果除此之外还需要其他任何内容,则需要在构建过程中将其作为单独的步骤添加。 如果您想编写自己的扩展程序,那您就一个人了。

  • You can’t leave anything out. Some of the features preprocessors provide such as Sass’s @extend may be detrimental to you, and you might want to leave them out completely. Unfortunately, while you can avoid using them, you can’t remove that part of the tool to minimize code.

    你不能遗漏任何东西。 预处理器提供的某些功能(例如Sass的@extend 可能对不利 ,您可能希望将其完全排除在外。 不幸的是,尽管可以避免使用它们,但不能删除该工具的那部分以最小化代码。

  • They push out CSS standards. You might say that each of the preprocessors has become a standard of its own. Regrettably, they don’t aim at being compatible with the W3C standards, which means that they cannot be used as polyfills for early testing of the newer W3C standards.

    他们推出CSS标准。 您可能会说每个预处理器都已成为其自己的标准。 遗憾的是,它们并不旨在与W3C标准兼容,这意味着它们不能用作新W3C标准的早期测试的填充料。

This is where PostCSS comes in.

这就是PostCSS出现的地方。

什么是PostCSS? (What is PostCSS?)

PostCSS is not a preprocessor per se; it doesn’t transform CSS. As a matter of fact, it doesn’t do much by itself at all. What it does is provide a CSS parser and a framework for creating plugins that can analyse, lint, handle assets, optimise, create fallbacks, and otherwise transform parsed CSS. PostCSS parses CSS into an abstract syntax tree (AST), passes it through a series of plugins, and then concatenates back into a string. If you’re familiar with JavaScript tooling, then you can think of PostCSS as Babel for CSS.

PostCSS本身不是预处理器。 它不会转换CSS。 事实上,它本身并没有做太多事情。 它的作用是提供CSS解析器和用于创建插件的框架,这些插件可以分析,处理,处理资产,优化,创建后备以及转换已解析CSS。 PostCSS将CSS解析为抽象语法树(AST),将其通过一系列插件传递,然后串联回字符串。 如果您熟悉JavaScript工具,那么可以将PostCSS视为CSS的Babel

There are currently more than 200 plugins for PostCSS, many of which are listed on the PostCSS GitHub page, while others can be found on the useful PostCSS directory postcss.parts. PostCSS can be integrated in most the build tools including Gulp, Grunt, webpack or npm.

当前,有超过200个PostCSS插件,其中许多列在PostCSS GitHub页面上 ,而其他插件可在有用的PostCSS目录postcss.parts中找到 。 PostCSS可以集成到大多数构建工具中,包括Gulp,Grunt,webpack或npm。

So how does PostCSS tackle the problems we listed earlier?

那么PostCSS如何解决我们前面列出的问题?

  • Each plugin is installed separately. This means you choose which ones you need and in what order they should be applied. Usually, plugins can be additionally configured using some set of options.

    每个插件都单独安装。 这意味着您可以选择所需的内容以及应采用的顺序。 通常,可以使用一些选项来额外配置插件。

  • You can write your own plugins. Each PostCSS plugin receives parsed CSS as an input parameter, analyses or modifies it, and returns it in the same manner. This means that plugins don’t need to handle parsing CSS and converting it back into a string. So the ability to build your own plugins is not as difficult as you might think.

    您可以编写自己的插件。 每个PostCSS插件都将解析后CSS作为输入参数,对其进行分析或修改,然后以相同的方式返回。 这意味着插件无需处理解析CSS并将其转换回字符串的过程。 因此,构建自己的插件的能力并不像您想象的那样困难。

  • PostCSS can be used to polyfill real W3C features. There are a lot of plugins that aim to implement features from new W3C specifications. This will enable you to write code that respects standards and is likely to be compatible with future versions of CSS.

    PostCSS可用于填充真实的W3C功能。 有许多插件旨在实现新的W3C规范中的功能。 这将使您能够编写符合标准并且可能与CSS的未来版本兼容的代码。

使用PostCSS (Using PostCSS)

Theory is great, but let’s move on to some juicy practice. Let’s install PostCSS and see what it can actually do. We won’t go into much detail about setting up proper project builds, since that’s a topic that deserves an article of its own. Instead we’ll run PostCSS directly from the command line. You can find more info on using PostCSS with your favourite build tool on its Github page.

理论很棒,但让我们继续进行一些多汁的实践。 让我们安装PostCSS并查看其实际功能。 我们不会详细介绍如何设置适当的项目构建,因为这是一个主题,值得一读。 相反,我们将直接从命令行运行PostCSS。 您可以在其Github页面上找到有关将PostCSS与您最喜欢的构建工具一起使用的更多信息。

安装PostCSS (Installing PostCSS)

PostCSS is installed via node and npm, so make sure you have those installed before you start. To install PostCSS globally on your system run:

PostCSS是通过node和npm安装的,因此在开始之前请确保已安装了这些CSS。 要在系统上全局安装PostCSS,请运行:

npm install -g postcss-cli

You can make sure it’s working by running:

您可以通过运行以下命令确保其正常工作:

postcss --help

This will give you a list of parameters that the CLI accepts. You can also find these in the postcss-cli documenation.

这将为您提供CLI接受的参数列表。 您也可以在postcss-cli文档中找到这些内容

运行PostCSS (Running PostCSS)

Now that we have PostCSS installed, let’s give it something to work with. Create a styles.css file in your project folder and add some CSS. For example, define a flexbox container:

现在我们已经安装了PostCSS,让我们给它一些有用的东西。 在您的项目文件夹styles.css创建一个styles.css文件,并添加一些CSS。 例如,定义一个flexbox容器:

.container {
  display: flex;
}

Flexbox is a great feature, but it does require vendor prefixes to run on certain browsers. I would hate to maintain these manually. Fortunately, Autoprefixer, one of the most popular PostCSS plugins does exactly that. It automatically adds vendor prefixes based on the information provided via Can I use. I’ll show you how to use it to keep our vendor prefixes up to date.

Flexbox是一个很棒的功能,但是它确实需要供应商前缀才能在某些浏览器上运行。 我不愿手动维护这些。 幸运的是, Autoprefixer (最受欢迎的PostCSS插件之一)可以做到这一点。 它会根据“ 我可以使用”提供的信息自动添加供应商前缀。 我将向您展示如何使用它来使我们的供应商前缀保持最新。

To install Autoprefixer run:

要安装Autoprefixer,请运行:

npm install -g autoprefixer

Next, navigate to your project folder and create a dist folder that will contain the processed CSS:

接下来,导航到您的项目文件夹并创建一个dist文件夹,其中将包含已处理CSS:

mkdir dist

Then run PostCSS:

然后运行PostCSS:

postcss -u autoprefixer styles.css -d dist

What this says is: run Autoprefixer on styles.css and save the output to dist/styles.css. Now if you open dist/styles.css you will see your CSS with all of the required vendor prefixes:

这是说: styles.css上运行Autoprefixer并将输出保存到dist/styles.css 。 现在,如果您打开dist/styles.css您将看到带有所有必需的供应商前缀CSS:

.container {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
}

If you run PostCSS with a -w flag, this will also start a watcher process and automatically rebuild styles.css each time the file is modified.

如果使用-w标志运行PostCSS,则每次修改文件时,这还将启动观察程序并自动重建styles.css

插件配置 (Plugin Configuration)

We can configure Autoprefixer to add prefixes according to the browsers we plan on supporting. This can be done using the browsers option. When running PostCSS via the CLI, plugin configuration needs to be defined in a separate .json file, for example, postcss.json. Let’s create the file in the current folder and configure Autoprefixer to support the 2 most recent versions of each browser.

我们可以配置Autoprefixer来根据我们计划支持的浏览器添加前缀。 可以使用browsers选项来完成。 通过CLI运行PostCSS时,插件配置需要在单独的postcss.json文件中定义,例如postcss.json 。 让我们在当前文件夹中创建文件,并将Autoprefixer配置为支持每个浏览器的2个最新版本。

{
  "autoprefixer": {
    "browsers": ["last 2 versions"]
  }
}

We can now re-run the PostCSS watcher with the new configuration file:

现在,我们可以使用新的配置文件重新运行PostCSS watcher:

postcss styles.css -u autoprefixer -c postcss.json -d dist -w

启用源地图 (Enabling Source Maps)

Source maps are essential for debugging compiled CSS. PostCSS can generate inline source maps in the output file if you add the --map option (or the -m flag).

源映射对于调试已编译CSS是必不可少的。 如果添加--map选项(或-m标志),则PostCSS可以在输出文件中生成内联源地图。

甚至更多的插件 (Even More Plugins)

PostCSS has an amazing number of plugins for linting, quality checks, fallback, old browser support, inlining assets, generating sprites, optimisation, new syntax support, and future CSS features. As mentioned, you can find a structured catalogue of plugins at postcss.parts.

PostCSS拥有大量插件,可用于棉绒,质量检查,后备,旧版浏览器支持,内联资产,生成Sprite,优化,新语法支持以及将来CSS功能。 如前所述,您可以在postcss.parts找到结构化的插件目录。

To give you a sample, here are a few more plugins that demonstrate the power of PostCSS.

为了给您一个示例,这里有更多的插件演示了PostCSS的功能。

CSS变量的自定义属性插件 (The Custom Properties Plugin for CSS Variables)

The postcss-custom-properties plugin aims to implement support for the W3C custom properties specification (aka native variables). It allows you to define custom properties in a selector with arbitrary values and reference these in other places in the stylesheet.

postcss-custom-properties插件旨在实现对W3C自定义属性规范 (即本地变量 )的支持。 它允许您在选择器中定义具有任意值的自定义属性,并在样式表的其他位置引用这些属性。

This serves a similar purpose to Less and Sass variables: to store common values and eliminate code duplication. The main difference is that the scoping mechanism is a bit different; similar to regular properties, CSS custom properties propagate along the element cascade, instead of being block scoped.

这与Less和Sass变量具有相似的用途:存储公用值并消除代码重复。 主要区别在于作用域机制有所不同。 与常规属性类似,CSS自定义属性沿元素级联传播,而不是在块范围内传播。

Here’s an example of how this plugin works. The following code:

这是此插件如何工作的示例。 如下代码:

:root {
  --container-width: 800px;
}

.container {
  width: var(--container-width);
}

Will compile to:

将编译为:

.container {
  width: 800px;
}

自定义选择器插件 (The Custom Selectors Plugin)

The postcss-custom-selectors plugin implements the Custom Selector specification. This allows you to pre-define selectors and reference them later in the code. For example, we can save all headers in one selector and re-use this as a variable:

postcss-custom-selectors插件实现了Custom Selector规范 。 这使您可以预定义选择器,并在以后的代码中引用它们。 例如,我们可以将所有标头保存在一个选择器中,然后将其重新用作变量:

@custom-selector :--headings h1, h2, h3, h4, h5, h6;

:--headings {
  color: mediumblue;
}

Which will compile as follows:

编译如下:

h1,
h2,
h3,
h4,
h5,
h6 {
  color: mediumblue;
}

俄罗斯样式表插件 (The Russian Stylesheets Plugin)

Ever wanted to learn Russian but were too busy writing CSS? No worries, you can now do both by writing CSS in Russian! Just use the Russian Stylesheets plugin. Check this out:

是否曾经想学习俄语,却又忙于编写CSS? 不用担心,您现在可以通过用俄语编写CSS来完成这两项工作! 只需使用俄语样式表插件即可。 看一下这个:

h1 {
  размер-шрифта: 20пикселей;
  цвет: красный;
  цвет-фона: белый;
  вес-шрифта: жирный;
}

Which is translated into:

转换为:

h1 {
  font-size: 20px;
  color: red;
  background-color: white;
  font-weight: bold;
}

Well… I never said all the plugins were equally useful!

好吧……我从未说过所有插件都同样有用!

包装东西 (Wrapping Things Up)

Preprocessors like Less and Sass are great. They have contributed much to our development process and are definitely not something we can easily leave behind. But, I feel that that the time has come to stop and reconsider whether those tools are the right way to do things in the long run.

Less和Sass之类的预处理器很棒。 它们为我们的开发过程做出了很大贡献,而且绝对不是我们可以轻易抛弃的东西。 但是,我觉得现在该停止了,重新考虑这些工具从长远来看是否是正确的做事方式。

We don’t just need new features writing stylesheets, we also need modularity, additional exposure of the new standards, as well as flexible build processes. PostCSS provides just that and might be a game changer in the CSS world.

我们不仅需要编写样式表的新功能,还需要模块化,对新标准的更多了解以及灵活的构建过程。 PostCSS正是提供了这一点,并且可能会改变CSS世界。

翻译自: https://www.sitepoint.com/an-introduction-to-postcss/

postcss

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

postcss_PostCSS简介 的相关文章

随机推荐

  • 安装虚拟机VMWare后再装LINUX系统(含避坑指南)

    一 下载 到官网下载虚拟机VMware Download VMware Workstation Pro 二 安装前的电脑设置 不要着急装 不设置好是安装不了的 还会引起暴躁 1 BIOS模式开启intel vt x 一般是禁用的 即 Int
  • 基于Native.js实现Android文件的读写操作

    Native js技术 简称NJS 是一种将手机操作系统的原生对象转义 映射为JS对象 在JS里编写原生代码的技术 我们在进行APP混合开发过程中免不了需要调用Andriod底层的一些API 也免不了可能会涉及到文件读取的操作之类的功能 这
  • Devops

    最近老是碰到这个名词 所以想了解一下这个到底是撒玩意 DevOps Development和Operations的组合词 是一组过程 方法与系统的统称 用于促进开发 应用程序 软件工程 技术运营和质量保障 QA 部门之间的沟通 协作与整合
  • windows利用kubectl命令和vscode远程操作kubenetes(k8s)

    windows 中安装kubecltl 命令 下载地址 http pwittrock github io docs tasks tools install kubectl install with chocolatey on windows
  • java 判断两个list是否相等的方法

    以下示例可以当做一个反面教材 实际是错误的 public static boolean isEquals List
  • 用apache实现禁止IP段或者主机对某个目录的访问

    Allow 指令 说明 控制哪些主机能够访问服务器的一个区域 语法 Allow from all host env env variable host env env variable 上下文 目录 htaccess 覆盖项 Limit 状
  • LayUI系列(一)之layui介绍以及登录功能的实现

    文章目录 一 layui简介 1 1 layui介绍 1 2 主要构成 1 3 迭代历程 1 4 layui easyui和bootstrap对比 1 4 1 layui与bootstrap对比 这两个都属于ui渲染框架 1 4 2 lay
  • TensorFlow框架做实时人脸识别小项目(一)

    人脸识别是深度学习最有价值也是最成熟的的应用之一 在研究环境下 人脸识别已经赶上甚至超过了人工识别的精度 一般来说 一个完整的人脸识别项目会包括两大部分 人脸检测与人脸识别 下面就我近期自己练习写的一个 粗糙 的人脸识别小项目讲起 也算是做
  • 二、关系模型

    关系模型 关系模型由关系数据结构 关系操作集合和关系完整性约束三部分组成 关系数据结构 关系模型的数据结构非常简单 只包含单一的数据结构 关系 域 一组具有相同数据类型的值的集合 笛卡儿积 在域上的一种集合运算 例如A 1 2 B a b
  • NNDL 作业7:第五章课后题(1×1 卷积核

    习题一 证明卷积具有交换性 即证明公式 首先 宽卷积定义为 其中 表示宽卷积运算 我们不妨先设一个二维图像和一个二维卷积核 然后对该二维图像X进行零填充 两端各补U 1 和V 1 个零 得到全填充的图像 现有 根据宽卷积定义 为了让x的下标
  • 学 Python 这么久,终于把类函数 & 成员函数 & 静态函数给整明白了!

    前言 学过 Python 的小伙伴应该知道 在类定义过程中 会依据对具体需求的分析 对类函数 成员函数 静态函数进行声明与定义 Python 基础稍弱的同学可能会对这三个函数的名称有些陌生 但你很有可能曾经见到过他们仨 只是没认出来而已 开
  • el-menu动态渲染多级菜单

    思路 创建子菜单递归组件 这里我命名为MySubMenu vue 父组件 引用自定义组件MySubMenu 将菜单数组传递给子组件
  • qt 信号量 linux 信号量 semctl,Linux信号量 共享内存和消息队列

    Linux信号量 共享内存和消息队列 Linux信号量 共享内存和消息队列 1 信号量 使用信号量可以实现进程间同步 主要函数定义 include int semctl int sem id int sem num int command
  • Chisel 手册 英文版

    Chisel Manual Jonathan Bachrach Huy Vo Krste Asanovi EECS Department UC Berkeley jrb huytbvo krste eecs berkeley edu Apr
  • HuTool 工具类简单使用

    官方文档 https hutool cn docs 简介 Hutool是一个小而全的Java工具类库 通过静态方法封装 降低相关API的学习成本 提高工作效率 使Java拥有函数式语言般的优雅 让Java语言也可以 甜甜的 Hutool中的
  • Python爬虫实战案例——第三例

    文章中所有内容仅供学习交流使用 不用于其他任何目的 严禁将文中内容用于任何商业与非法用途 由此产生的一切后果与作者无关 若有侵权 请联系删除 起点中文网月票榜加密字体处理 字体加密的原理 就是将一种特定的字体库来代替浏览器本身的字体库显示的
  • JeecgBoot页面级联选择框(三种方法)

    JeecgBoot页面级联选择框 三种方法 1 form表单级联选择框 已实践 2 j vxe table联动示例 已实践 3 j editable table三级联动 简单测试 只贴代码 不做过多说明 一 form表单级联选择框 1 1
  • 【Mac】Mac 下安装MySQL优化工具mysqltuner执行报错 [!!] Attempted to use login credentials

    1 背景 base lcc lcc MySQLTuner perl mysqltuner pl socket var lib mysql mysql sock gt gt MySQLTuner 1 7 19 Major Hayden
  • FPGA选型

    原来SG6twotwo FPGA推荐型号0 GX480系列 FPGA推荐型号1 GX570系列 FPGA推荐型号2 GX660系列 SG622 FPGA型号 10AX027 F34 10AX048 10AX057 10AX066 引脚数量
  • postcss_PostCSS简介

    postcss Most developers who spend their time working with CSS are familiar with preprocessors such Less Sass and Stylus