Skip to main content

编写自定义语法

¥Writing custom syntaxes

自定义语法是社区编写的 PostCSS 语法,用于支持其他样式语言,例如 SCSS 和容器,例如 HTML,使用 customSyntax 选项

¥Custom syntaxes are PostCSS syntaxes written by the community to support other styling languages, e.g. SCSS, and containers, e.g. HTML, using the customSyntax option.

要编写一个,请熟悉 PostCSS 的 如何编写自定义语法 指南。你可以使用 很棒的 Stylelint 中现有的自定义语法之一作为参考。

¥To write one, familiarize yourself with PostCSS's how to write custom syntax guide. You can use one of the existing custom syntaxes from Awesome Stylelint for reference.

例如:

¥For example:

import postcss from "postcss";

function parse(css, opts) {
const root = postcss.root();
// adding other nodes to root...
return root;
}

function stringify(node, builder) {
// just use the default stringifier
postcss.stringify(node, builder);

// or write custom stringifier...
}

export default { parse, stringify };

发布自定义语法后,我们建议创建一个共享配置:

¥After publishing your custom syntax, we recommend creating a shared config that:

  • 延长 标准配置

    ¥extends the standard config

  • 打包你的自定义语法

    ¥bundles your custom syntax

  • 关闭任何不兼容的内置规则

    ¥turns off any incompatible built-in rules

所有内容都在 overrides 内,表示支持的文件扩展名。

¥All within an overrides for the supported file extensions.

例如,如果你要为名为 "foo" 的语言(使用文件扩展名 .foo)创建自定义语法,我们建议使用以下内容创建名为 "stylelint-config-standard-foo" 的共享配置:

¥For example, if you're creating a custom syntax for a language called "foo" (which uses the file extension .foo), we recommend creating a shared-config called "stylelint-config-standard-foo" with the following content:

import yourCustomSyntax from "postcss-your-custom-syntax";

export default {
overrides: [
{
files: ["*.foo", "**/*.foo"],
customSyntax: yourCustomSyntax,
extends: ["stylelint-config-standard"],
rules: {
"at-rule-no-unknown": null
// ..
}
}
]
};

我们建议在 PostCSS v7 不再流通之前要求使用自定义语法。

¥We recommended requiring the custom syntax until PostCSS v7 is no longer in circulation.