prettier-plugin-classnames
一个 Prettier 插件,根据 printWidth 选项自动换行冗长的类名。

安装方法[^1]
npm install -D prettier prettier-plugin-classnames[^1]: 如果你的 prettier-plugin-classnames 版本低于 0.4.0,你还需要安装 @prettier/sync。
配置
JSON 示例:
{
"plugins": ["prettier-plugin-classnames"]
}JS 示例(CommonJS 模块):
module.exports = {
plugins: ['prettier-plugin-classnames'],
customAttributes: ['myClassProp'],
customFunctions: ['clsx'],
};JS 示例(ES 模块):
export default {
plugins: ['prettier-plugin-classnames'],
endingPosition: 'absolute',
};选项
自定义属性
包含类名的属性列表。
即使未指定任何选项,也始终支持 class 和 className 属性。
默认值 | CLI 覆盖 | API 覆盖
--- | --- | ---
[] | --custom-attributes | customAttributes: ["
自定义函数
包含类名的函数列表。
即使未指定任何选项,也始终支持 classNames 函数。
默认值 | CLI 覆盖 | API 覆盖
--- | --- | ---
[] | --custom-functions | customFunctions: ["
结束位置
首次在 v0.5.0 中提供。
absolute-with-indent 于 v0.6.0 添加。
absolute-with-indent 于 v0.8.0 移除,但使用 absolute 可获得相同输出。
默认值在 v0.8.0 中从 relative 更改为 absolute。
这是替换原始类名为多行类名时,每行结束类名的标准。
absolute示例:
--------------------------------------------------| printWidth=50
export function Callout({ children }) {
return (
{children}
);
}
``
relative 示例:
`
--------------------------------------------------| printWidth=50
export function Callout({ children }) {
return (
{children}
);
}
`
默认 | CLI 覆盖 | API 覆盖
--- | --- | ---
"absolute" | --ending-position | endingPosition: ""语法转换
首次在 v0.7.7 中提供。
如果在非表达式语法中编写的类名出现换行,则会转换为表达式语法。此转换不支持可逆格式化。
默认 | CLI 覆盖 | API 覆盖
--- | --- | ---
false | --syntax-transformation | syntaxTransformation: 与同类插件的版本关联
从
0.6.0 开始,当一方有小版本发布时,我计划如果可能,也将该更改反映到另一方。
与其他 Prettier 插件的兼容性
如果多个 Prettier 插件都能处理您想格式化的文本,Prettier 只会使用这些插件中的最后一个。
在这种情况下,您可以通过添加 prettier-plugin-merge 来按顺序应用这些插件,从而进行如下配置。
JSON 示例:
json
{
"plugins": [
"prettier-plugin-tailwindcss",
"prettier-plugin-classnames",
"prettier-plugin-merge"
]
}
``--- Tranlated By Open Ai Tx | Last indexed: 2025-09-09 ---