Skip to content

Commit 3a88ae0

Browse files
authored
Document @source not and @source inline (#2181)
1 parent c750f5a commit 3a88ae0

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

src/docs/detecting-classes-in-source-files.mdx

+131
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TipGood, TipBad, TipInfo } from "@/components/tips";
22
import { Iframe } from "@/components/iframe";
33
import { Example } from "@/components/example";
44
import { Figure } from "@/components/figure";
5+
import { CodeExampleStack } from "@/components/code-example";
56

67
export const title = "Detecting classes in source files";
78
export const description = "Understanding and customizing how Tailwind scans your source files.";
@@ -167,6 +168,19 @@ To set the base path for source detection explicitly, use the `source()` functio
167168

168169
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.
169170

171+
### Ignoring specific paths
172+
173+
Use `@source not` to ignore specific paths, relative to the stylesheet, when scanning for class names:
174+
175+
```css
176+
/* [!code filename:CSS] */
177+
@import "tailwindcss";
178+
/* [!code highlight:2] */
179+
@source not "../src/components/legacy";
180+
```
181+
182+
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.
183+
170184
### Disabling automatic detection
171185

172186
Use `source(none)` to completely disable automatic source detection if you want to register all of your sources explicitly:
@@ -181,3 +195,120 @@ Use `source(none)` to completely disable automatic source detection if you want
181195
```
182196

183197
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.
198+
199+
## Safelisting specific utilities
200+
201+
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:
202+
203+
<CodeExampleStack>
204+
```css
205+
/* [!code filename:CSS] */
206+
@import "tailwindcss";
207+
/* [!code highlight:2] */
208+
@source inline("underline");
209+
```
210+
211+
```css
212+
/* [!code filename:Generated CSS] */
213+
.underline {
214+
text-decoration: underline;
215+
}
216+
```
217+
218+
</CodeExampleStack>
219+
220+
### Generating variants
221+
222+
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:
223+
224+
<CodeExampleStack>
225+
```css
226+
/* [!code filename:CSS] */
227+
@import "tailwindcss";
228+
/* [!code highlight:2] */
229+
@source inline("{hover:,focus:,}underline");
230+
```
231+
232+
```css
233+
/* [!code filename:Generated CSS] */
234+
.underline {
235+
text-decoration: underline;
236+
}
237+
@media (hover: hover) {
238+
.hover\:underline:hover {
239+
text-decoration: underline;
240+
}
241+
}
242+
@media (focus: focus) {
243+
.focus\:underline:focus {
244+
text-decoration: underline;
245+
}
246+
}
247+
```
248+
249+
</CodeExampleStack>
250+
251+
### Generating ranges
252+
253+
The source input is [brace expanded](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html), so you can generate multiple classes at once. For example, to generate all the red background colors with hover variants, use a range:
254+
255+
<CodeExampleStack>
256+
```css
257+
/* [!code filename:CSS] */
258+
@import "tailwindcss";
259+
/* [!code highlight:2] */
260+
@source inline("{hover:,}bg-red-{50,{100..900..100},950}");
261+
```
262+
263+
```css
264+
/* [!code filename:Generated CSS] */
265+
.bg-red-50 {
266+
background-color: var(--color-red-50);
267+
}
268+
.bg-red-100 {
269+
background-color: var(--color-red-100);
270+
}
271+
.bg-red-200 {
272+
background-color: var(--color-red-200);
273+
}
274+
275+
/* ... */
276+
277+
.bg-red-800 {
278+
background-color: var(--color-red-800);
279+
}
280+
.bg-red-900 {
281+
background-color: var(--color-red-900);
282+
}
283+
.bg-red-950 {
284+
background-color: var(--color-red-950);
285+
}
286+
@media (hover: hover) {
287+
.hover\:bg-red-50:hover {
288+
background-color: var(--color-red-50);
289+
}
290+
291+
/* ... */
292+
293+
.hover\:bg-red-950:hover {
294+
background-color: var(--color-red-950);
295+
}
296+
}
297+
```
298+
299+
</CodeExampleStack>
300+
301+
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.
302+
303+
### Explicitly excluding classes
304+
305+
Use `@source not inline()` to prevent specific classes from being generated, even if they are detected in your source files:
306+
307+
```css
308+
/* [!code filename:CSS] */
309+
@import "tailwindcss";
310+
/* [!code highlight:2] */
311+
@source not inline("{hover:,focus:,}bg-red-{50,{100..900..100},950}");
312+
```
313+
314+
This will explicitly exclude the red background utilities, along with their hover and focus variants, from being generated.

0 commit comments

Comments
 (0)