8

在文件中托管代码(graphql/css/js/html)很棒。

我使用 TailwindCSS 库为我的组件设置样式,并使用classnames包动态插入类。

我还使用Prettier来格式化我的代码。问题是它会格式化我的类规则,每行一个类,这很快就会使我的文件膨胀。

通缉:

const container = cn("flex", "flex-col", "min-h-lg", "mb-12", "transition-opacity", "duration-500", "ease-in-out");
const buttons = cn(
  "inline-grid", "row-gap-5", "col-gap-10", "grid-rows-5", "grid-flow-col",
  "sm:grid", "sm:grid-cols-3",
);
const character = cn(
  "hidden", "row-span-5", "h-48", "place-self-end", "origin-center",
  "animation-once", "animation-ease-out", "animation-0.8s", "animation-fill-forwards",
  "sm:block", "sm:animation-lightspeedin",
);

我得到的更漂亮:

const container = cn(
  "flex",
  "flex-col",
  "min-h-lg",
  "mb-12",
  "transition-opacity",
  "duration-500",
  "ease-in-out"
);
const buttons = cn(
  "inline-grid",
  "row-gap-5",
  "col-gap-10",
  "grid-rows-5",
  "grid-flow-col",
  "sm:grid",
  "sm:grid-cols-3"
);
const character = cn(
  "hidden",
  "row-span-5",
  "h-48",
  "place-self-end",
  "origin-center",
  "animation-once",
  "animation-ease-out",
  "animation-0.8s",
  "animation-fill-forwards",
  "sm:block",
  "sm:animation-lightspeedin"
);

如您所见,它很快就会失控。我想知道其他具有类似堆栈的人正在做什么来保持他们的文件可读。我一直在努力寻找少于 100 行的文件,但是使用 prettier+tailwindcss 已经不可能了。prettier-ignore-{start,end} 如果 js/jsx/ts/tsx 文件支持它,我会选择的。你忽略每一条规则吗?

// prettier-ignore
const container = cn("flex", "flex-col", "min-h-lg", "mb-12", "transition-opacity", "duration-500", "ease-in-out");
// prettier-ignore
const buttons = cn(
  "inline-grid", "row-gap-5", "col-gap-10", "grid-rows-5", "grid-flow-col",
  "sm:grid", "sm:grid-cols-3",
);
// prettier-ignore
const character = cn(
  "hidden", "row-span-5", "h-48", "place-self-end", "origin-center",
  "animation-once", "animation-ease-out", "animation-0.8s", "animation-fill-forwards",
  "sm:block", "sm:animation-lightspeedin",
);

或者您是否将所有类移动到文件底部以使其不那么可见/减少滚动以获取组件的需要?prettier-ignore-{start,end}我想我会两者都做,并在可用时将它们移回文件顶部。

4

1 回答 1

1

我不认为这是你可以改变 Prettier 的事情。这是很自以为是的,而且有充分的理由。您可以调整的越少,它就越能保留您的代码(大部分时间)。我认为 Prettier 的分支允许更多的定制,但老实说,我有一个更简单的解决方案给你。像这样添加你的类:

const container = cn("flex flex-col min-h-lg mb-12 transition-opacity");

const buttons = cn(
  "inline-grid row-gap-5 col-gap-10 grid-rows-5 grid-flow-col",
  "sm:grid sm:grid-cols-3",
);

const character = cn(
  "hidden row-span-5 h-48 place-self-end origin-center",
  "animation-once animation-ease-out animation-0.8s animation-fill-forwards",
  "sm:block sm:animation-lightspeedin",
);
于 2021-11-26T10:22:10.533 回答