使用插件
¥Working with Plugins
我们的插件实现基于 eslint 的插件实现;每个插件都是一个 npm 模块,其名称格式为 commitlint-plugin-<plugin-name>
,例如 commitlint-plugin-jquery
。你还可以使用 @<scope>/commitlint-plugin-<plugin-name>
格式的作用域包,例如 @jquery/commitlint-plugin-jquery
。
¥Our plugin implementation is based off of eslint's plugin implementation; Each plugin is an npm module with a name in the format of commitlint-plugin-<plugin-name>
, such as commitlint-plugin-jquery
. You can also use scoped packages in the format of @<scope>/commitlint-plugin-<plugin-name>
such as @jquery/commitlint-plugin-jquery
.
插件中的规则
¥Rules in Plugins
插件可以公开其他规则以供在 commitlint 中使用。为此,插件必须导出一个 rules
对象,该对象包含规则 ID 到规则的键值映射。规则 ID 不必遵循任何命名约定(例如,可以只是 dollar-sign
)。
¥Plugins can expose additional rules for use in commitlint. To do so, the plugin must export a rules
object containing a key-value mapping of rule ID to rule. The rule ID does not have to follow any naming convention (so it can just be dollar-sign
, for instance).
export default {
rules: {
"dollar-sign": function (parsed, when, value) {
// rule implementation ...
},
},
};
要在 commitlint 中使用规则,你需要使用无前缀的插件名称,后跟一个斜杠,再后跟规则名称。因此,如果此插件名为 commitlint-plugin-myplugin
,那么在你的配置中,你将使用名称 myplugin/dollar-sign
来引用该规则。示例:"rules": {"myplugin/dollar-sign": 2}
。
¥To use the rule in commitlint, you would use the unprefixed plugin name, followed by a slash, followed by the rule name. So if this plugin were named commitlint-plugin-myplugin
, then in your configuration you'd refer to the rule by the name myplugin/dollar-sign
. Example: "rules": {"myplugin/dollar-sign": 2}
.
对等依赖
¥Peer Dependency
为了明确说明插件需要 commitlint 才能正常工作,你必须在 package.json
中将 commitlint 声明为 peerDependency
。该插件支持在 commitlint 版本 7.6.0
中引入。确保 peerDependency
指向 @commitlint 7.6.0
或更高版本。
¥To make clear that the plugin requires commitlint to work correctly you have to declare commitlint as a peerDependency
in your package.json
. The plugin support was introduced in commitlint version 7.6.0
. Ensure the peerDependency
points to @commitlint 7.6.0
or later.
{
"peerDependencies": {
"@commitlint/lint": ">=7.6.0"
}
}
共享插件
¥Share Plugins
为了让你的插件可供社区使用,你必须将其发布到 npm。
¥In order to make your plugin available to the community you have to publish it on npm.
推荐关键字:
¥Recommended keywords:
commitlint
commitlintplugin
将这些关键字添加到你的 package.json
文件中,以便其他人轻松找到。
¥Add these keywords into your package.json
file to make it easy for others to find.
本地插件
¥Local Plugins
如果你想在本地开发插件而不需要发布到 npm
,你可以在本地项目中声明你的插件。请注意,你只能声明一个本地插件。
¥In case you want to develop your plugins locally without the need to publish to npm
, you can send declare your plugins inside your project locally. Please be aware that you can declare only one local plugin.
使用示例
¥Usage Example
export default {
rules: {
"hello-world-rule": [2, "always"],
},
plugins: [
{
rules: {
"hello-world-rule": ({ subject }) => {
const HELLO_WORLD = "Hello World";
return [
subject.includes(HELLO_WORLD),
`Your subject should contain ${HELLO_WORLD} message`,
];
},
},
},
],
};
> echo "feat: random subject" | commitlint # fails
> echo "feat: Hello World" | commitlint # passes
延伸阅读
¥Further Reading