1. 核心概念
  2. 检测源文件中的类

核心概念

检测源文件中的类

理解并自定义 Tailwind 如何扫描你的源文件。

概述(Overview)

Tailwind 的工作原理是扫描项目中的工具类,然后根据你实际使用的类生成所有必要的 CSS。

🌐 Tailwind works by scanning your project for utility classes, then generating all of the necessary CSS based on the classes you've actually used.

这确保了你的 CSS 尽可能小,同时也使像 任意值 这样的功能成为可能。

🌐 This makes sure your CSS is as small as possible, and is also what makes features like arbitrary values possible.

如何检测类别(How classes are detected)

Tailwind 将你的所有源文件视为纯文本,并且不会尝试以任何方式将你的文件解析为代码。

🌐 Tailwind treats all of your source files as plain text, and doesn't attempt to actually parse your files as code in any way.

相反,它只会根据 Tailwind 在类名中期望的字符,在文件中查找可能为类的任何标记:

🌐 Instead it just looks for any tokens in your file that could be classes based on which characters Tailwind is expecting in class names:

JSX
export function Button({ color, children }) {  const colors = {    black: "bg-black text-white",    blue: "bg-blue-500 text-white",    white: "bg-white text-black",  };  return (    <button className={`${colors[color]} rounded-full px-2 py-1.5 font-sans text-sm/6 font-medium shadow`}>      {children}    </button>  );}

然后,它会尝试为所有这些标记生成 CSS,丢弃任何未映射到框架所知的工具类的标记。

🌐 Then it tries to generate the CSS for all of these tokens, throwing away any tokens that don't map to a utility class the framework knows about.

动态类名(Dynamic class names)

由于 Tailwind 将你的源文件扫描为纯文本,因此它无法理解你使用的编程语言中的字符串连接或插值。

🌐 Since Tailwind scans your source files as plain text, it has no way of understanding string concatenation or interpolation in the programming language you're using.

不要动态构建类名

HTML
<div class="text-{{ error ? 'red' : 'green' }}-600"></div>

在上面的示例中,字符串 text-red-600text-green-600 并不存在,因此 Tailwind 不会生成那些类。

🌐 In the example above, the strings text-red-600 and text-green-600 do not exist, so Tailwind will not generate those classes.

相反,请确保你使用的任何类名都完整存在:

🌐 Instead, make sure any class names you’re using exist in full:

始终使用完整的类名

HTML
<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>

如果你正在使用像 React 或 Vue 这样的组件库,这意味着你不应该使用 props 来动态构造类:

🌐 If you're using a component library like React or Vue, this means you shouldn't use props to dynamically construct classes:

不要使用 props 动态构建类名

JSX
function Button({ color, children }) {  return <button className={`bg-${color}-600 hover:bg-${color}-500 ...`}>{children}</button>;}

相反,将 props 映射到在构建时可静态检测的完整类名:

🌐 Instead, map props to complete class names that are statically detectable at build-time:

总是将属性映射到静态类名

JSX
function Button({ color, children }) {  const colorVariants = {    blue: "bg-blue-600 hover:bg-blue-500",    red: "bg-red-600 hover:bg-red-500",  };  return <button className={`${colorVariants[color]} ...`}>{children}</button>;}

这有一个额外的好处,可以让你将不同的属性值映射到不同的颜色阴影,例如:

🌐 This has the added benefit of letting you map different prop values to different color shades for example:

JSX
function Button({ color, children }) {  const colorVariants = {    blue: "bg-blue-600 hover:bg-blue-500 text-white",    red: "bg-red-500 hover:bg-red-400 text-white",    yellow: "bg-yellow-300 hover:bg-yellow-400 text-black",  };  return <button className={`${colorVariants[color]} ...`}>{children}</button>;}

只要你始终在代码中使用完整的类名,Tailwind 每次都会完美地生成所有 CSS。

🌐 As long as you always use complete class names in your code, Tailwind will generate all of your CSS perfectly every time.

扫描哪些文件(Which files are scanned)

Tailwind 将扫描项目中的每个文件以查找类名,但以下情况除外:

🌐 Tailwind will scan every file in your project for class names, except in the following cases:

  • 位于你的 .gitignore 文件中的文件
  • node_modules 目录中的文件
  • 二进制文件,如图片、视频或 zip 文件
  • CSS 文件
  • 通用包管理器锁定文件

如果你需要扫描 Tailwind 默认忽略的任何文件,你可以显式注册这些来源。

🌐 If you need to scan any files that Tailwind is ignoring by default, you can explicitly register those sources.

显式注册源(Explicitly registering sources)

使用 @source 明确注册相对于样式表的源路径:

🌐 Use @source to explicitly register source paths relative to the stylesheet:

CSS
@import "tailwindcss";@source "../node_modules/@acmecorp/ui-lib";

当你需要扫描使用 Tailwind 构建的外部库时,这特别有用,因为依赖通常列在你的 .gitignore 文件中,并且默认情况下会被 Tailwind 忽略。

🌐 This is especially useful when you need to scan an external library that is built with Tailwind, since dependencies are usually listed in your .gitignore file and ignored by Tailwind by default.

设置基本路径(Setting your base path)

默认情况下,Tailwind 使用当前工作目录作为扫描类名的起点。

🌐 Tailwind uses the current working directory as its starting point when scanning for class names by default.

要显式设置源检测的基础路径,请在 CSS 中导入 Tailwind 时使用 source() 函数:

🌐 To set the base path for source detection explicitly, use the source() function when importing Tailwind in your CSS:

CSS
@import "tailwindcss" source("../src");

这在使用 monorepos 时很有用,其中你的构建命令从 monorepo 的根目录而不是每个项目的根目录运行。

🌐 This can be useful when working with monorepos where your build commands run from the root of the monorepo instead of the root of each project.

忽略特定路径(Ignoring specific paths)

使用 @source not 来忽略扫描类名时相对于样式表的特定路径:

🌐 Use @source not to ignore specific paths, relative to the stylesheet, when scanning for class names:

CSS
@import "tailwindcss";@source not "../src/components/legacy";

当你的项目中存在大型目录并且你知道它们不使用 Tailwind 类(例如旧版组件或第三方库)时,这将非常有用。

🌐 This is useful when you have large directories in your project that you know don't use Tailwind classes, like legacy components or third-party libraries.

禁用自动检测(Disabling automatic detection)

如果你想显式注册所有来源,可以使用 source(none) 完全禁用自动来源检测:

🌐 Use source(none) to completely disable automatic source detection if you want to register all of your sources explicitly:

CSS
@import "tailwindcss" source(none);@source "../admin";@source "../shared";

这在具有多个 Tailwind 样式表的项目中很有用,你希望确保每个样式表仅包含每个样式表所需的类。

🌐 This can be useful in projects that have multiple Tailwind stylesheets where you want to make sure each one only includes the classes each stylesheet needs.

将特定工具添加到安全列表(Safelisting specific utilities)

如果你需要确保 Tailwind 生成在内容文件中不存在的某些类名,可以使用 @source inline() 来强制生成它们:

🌐 If you need to make sure Tailwind generates certain class names that don’t exist in your content files, use @source inline() to force them to be generated:

CSS
@import "tailwindcss";@source inline("underline");
Generated CSS
.underline {  text-decoration-line: underline;}

将变体添加到安全列表(Safelisting variants)

你也可以使用 @source inline() 来生成带有变体的类。例如,要生成带有悬停和聚焦变体的 underline 类,请将 {hover:,focus:,} 添加到源输入中:

🌐 You can also use @source inline() to generate classes with variants. For example, to generate the underline class with hover and focus variants, add {hover:,focus:,} to the source input:

CSS
@import "tailwindcss";@source inline("{hover:,focus:,}underline");
Generated CSS
.underline {  text-decoration-line: underline;}@media (hover: hover) {  .hover\:underline:hover {    text-decoration-line: underline;  }}@media (focus: focus) {  .focus\:underline:focus {    text-decoration-line: underline;  }}

将范围添加到安全列表(Safelisting with ranges)

源输入是 brace expanded,所以你可以一次生成多个类。例如,要生成所有带悬停效果的红色背景颜色,可以使用一个范围:

🌐 The source input is brace expanded, so you can generate multiple classes at once. For example, to generate all the red background colors with hover variants, use a range:

CSS
@import "tailwindcss";@source inline("{hover:,}bg-red-{50,{100..900..100},950}");
Generated CSS
.bg-red-50 {  background-color: var(--color-red-50);}.bg-red-100 {  background-color: var(--color-red-100);}.bg-red-200 {  background-color: var(--color-red-200);}/* ... */.bg-red-800 {  background-color: var(--color-red-800);}.bg-red-900 {  background-color: var(--color-red-900);}.bg-red-950 {  background-color: var(--color-red-950);}@media (hover: hover) {  .hover\:bg-red-50:hover {    background-color: var(--color-red-50);  }  /* ... */  .hover\:bg-red-950:hover {    background-color: var(--color-red-950);  }}

这会生成从 100 到 900、每次递增 100 的红色背景颜色,以及第一个和最后一个色调 50 和 950。它还为每个这些类添加了 hover: 变体。

🌐 This generates red background colors from 100 to 900 in increments of 100, along with the first and last shades of 50 and 950. It also adds the hover: variant for each of those classes.

明确排除类别(Explicitly excluding classes)

使用 @source not inline() 来防止生成特定的类,即使它们在你的源文件中被检测到:

🌐 Use @source not inline() to prevent specific classes from being generated, even if they are detected in your source files:

CSS
@import "tailwindcss";@source not inline("{hover:,focus:,}bg-red-{50,{100..900..100},950}");

这将明确排除红色背景工具及其悬停和聚焦变体的生成。

🌐 This will explicitly exclude the red background utilities, along with their hover and focus variants, from being generated.

TailwindCSS 中文网 - 粤ICP备13048890号