Skip to content

Commit 23bf209

Browse files
authored
Merge pull request #749 from dev-five-git/feat/tailwind-v4-complete
feat(extractor): compile containers, masks, 3D transforms, functional @Utility and class helpers
2 parents 517d9eb + 63b2777 commit 23bf209

17 files changed

Lines changed: 2167 additions & 96 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"changes": {
3+
"bindings/devup-ui-wasm/package.json": "Patch",
4+
"packages/rsbuild-plugin/package.json": "Patch",
5+
"packages/bun-plugin/package.json": "Patch"
6+
},
7+
"note": "More of Tailwind CSS v4 compiles: container queries (@sm:, @max-md:, @min-[400px]:, @sm/main:) and @container utilities, mask-* (all but the gradient masks), text-shadow-*, inset-shadow-*, font-variant-numeric utilities (tabular-nums and the like), 3D transforms (rotate-x/y/z, translate-z, scale-z, translate-3d, scale-3d, transform-3d, backface-*, perspective-*, perspective-origin-*), contain-* and outline-hidden (with its forced-colors fallback). The project's Tailwind CSS is read further: functional @utility with --value() and --modifier(), @apply in @utility, and theme(--name) in arbitrary values; classes of @plugin entries stay as written. A className now compiles the classes of cn()/twMerge() calls whose arguments are all static (merged as tailwind-merge does, the later class wins), of consts bound to class strings, and of cva() calls (base, variants, compound variants). The Rsbuild plugin reads the Tailwind CSS again and compiles every module again when it changes, and the Bun plugin reads it again. No new build errors",
8+
"date": "2026-10-01T00:00:00.000Z"
9+
}

‎apps/landing/src/app/(detail)/docs/migration/tailwind/page.mdx‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ A class is never rewritten into something different from what Tailwind CSS would
3131
Classes compile in the `className` of Devup UI components, and the build only reads what it can know:
3232

3333
- string literals, template literals, `cond ? 'a' : 'b'`, `a && 'b'`, `a || 'b'` and parentheses,
34-
- the arguments of `clsx(...)`, `classnames(...)` and `classNames(...)` (strings, arrays, and the keys of objects), and `['a', cond && 'b'].join(' ')`.
34+
- a `const` of the same file bound to a string (`const base = 'p-4 card'`, read as the classes it holds wherever the `className` reads it; a `let`, a parameter or a template with `${}` is not),
35+
- the arguments of `clsx(...)`, `classnames(...)` and `classNames(...)` (strings, arrays, and the keys of objects), and `['a', cond && 'b'].join(' ')`, each class compiled where it is written,
36+
- `cn(...)` and `twMerge(...)` when **every** argument is a string, a template without `${}`, or an array of them: the build merges them as tailwind-merge does, so a Tailwind class that a later one sets again is dropped (`cn('p-4 m-2', 'p-2')` keeps `m-2` and `p-2`; a variant (`hover:p-2`), a breakpoint and `!` each count apart, and `px-4` and `p-2` both stay) and compiles what is left. `cn` is read as `twMerge(clsx(...))`: if your `cn` does something else, give it another name. A `cn()` with a condition or any other argument is left alone, since its result is only known at runtime and the strings in it must reach `twMerge` as written,
37+
- the strings of `cva(base, { variants, compoundVariants })` in a file that imports Devup UI: the base, every variant option and every compound variant `class`/`className` compile where they are written (`cva` only joins them, so each class stays a class). A file without a Devup UI import is not compiled at all, so put `cva` calls in a file that has one.
3538

36-
Other helpers (`cn`, `twMerge`, `cva`) may merge or drop classes by rules the build does not run, so their strings are not compiled. A class that is only known at runtime (a variable, a prop, a computed string) cannot be compiled either. In every case the `className` keeps working as before, as plain text.
39+
A class that is only known at runtime (a prop, a computed string, an imported constant, a `cn()` with a condition) cannot be compiled. In every case the `className` keeps working as before, as plain text.
3740

3841
A plain DOM element (`<div className="p-4" />`) or a component that is not a Devup UI component is **not** touched: its `className` is passed through as written, so Tailwind (its own build) can style it. Use `<Box as="div" className="p-4" />` when you want the class compiled.
3942

@@ -72,7 +75,7 @@ The CSS file of your project that imports `tailwindcss` can define more, as it d
7275
@custom-variant theme-midnight (&:where([data-theme='midnight'] *));
7376
```
7477

75-
Every plugin reads the file when the build starts. The Vite, webpack and Next.js plugins also read it again when it, or a CSS file it imports with a relative path, changes (the Rsbuild and Bun plugins do not watch it, so restart them after a change). So `bg-brand`, `p-gutter`, `3xl:flex`, `animate-wiggle`, `content-auto` and `theme-midnight:bg-black` compile.
78+
Every plugin reads the file when the build starts, and again when it, or a CSS file it imports with a relative path, changes: Vite, webpack, Next.js and Rsbuild compile every module again, and the Bun plugin (a runtime plugin, with no rebuild of its own) reads the definitions again, so a module compiles with them the next time Bun loads it. So `bg-brand`, `p-gutter`, `3xl:flex`, `animate-wiggle`, `content-auto` and `theme-midnight:bg-black` compile.
7679

7780
The file is found, in this order: the `tailwind.css` option of `devup.json`, else the first of `src/app/globals.css`, `app/globals.css`, `src/globals.css`, `src/index.css`, `src/styles/globals.css`, `src/styles.css`, `src/style.css`, `src/app.css`, `styles/globals.css`, `app/app.css` that imports `tailwindcss`.
7881

@@ -84,9 +87,15 @@ The file is found, in this order: the `tailwind.css` option of `devup.json`, els
8487

8588
Use `"css": false` to read none.
8689

87-
What is read: `@theme` variables of the color, spacing (`--spacing`, `--spacing-*`), shadow, blur, radius, ease, animate, `default-transition-*` and breakpoint namespaces (and `@keyframes` inside `@theme`), resets such as `--color-*: initial` and `--*: initial`, `@utility` with plain declarations, and `@custom-variant` as a selector list, or a block holding `@slot;` in a selector, `@media` or `@supports`. A `@custom-variant` with the name of a built-in variant (`dark`) replaces it.
90+
What is read:
8891

89-
What is not read keeps its classes as written: functional `@utility` (`tab-*` with `--value()`), nested rules and `@apply` in `@utility`, `@plugin`, `@config`, values using `theme()`, and a file whose braces do not balance. If the project changes the values of a namespace the build does not read (`--text-*`, `--font-*`, `--leading-*`, `--tracking-*`, `--container-*`, `--perspective-*`, `--aspect-*`), the classes that read it (`text-lg`, `font-bold`, `max-w-sm` ...) also stay as written, instead of compiling to a default the project replaced.
92+
- `@theme` variables of the color, spacing (`--spacing`, `--spacing-*`), shadow, inset-shadow, text-shadow, drop-shadow, blur, radius, ease, animate, perspective, container (`@sm:`), `default-transition-*` and breakpoint namespaces (and `@keyframes` inside `@theme`), and resets such as `--color-*: initial` and `--*: initial`.
93+
- `@utility name { ... }` with declarations and `@apply` lines. `@apply` writes out the utilities it names (`@apply px-4 py-2!`), also through other `@utility` that apply utilities; a utility with a variant (`hover:p-4`), one that styles children (`space-x-4`), an unknown one, or a loop leaves the whole `@utility` as written.
94+
- Functional `@utility name-* { ... }` whose declarations read `--value(...)` and `--modifier(...)`. An argument is `integer`, `number`, `percentage`, a literal (`'inherit'`), a theme namespace (`--tab-size-*`, `--color-*`, `--spacing-*`) or an arbitrary type (`[length]`, `[color]`, `[number]`, `[url]`); the first argument the value fits is used. A declaration that reads a `--modifier()` that is not given is dropped; a `--value()` no argument fits makes the class stay as written; `--spacing()`, `--alpha()` and the other functions of a utility are not run, so a utility using them stays as written.
95+
- `@custom-variant` as a selector list, or a block holding `@slot;` in a selector, `@media` or `@supports`. A `@custom-variant` with the name of a built-in variant (`dark`) replaces it.
96+
- `theme(--name)` in arbitrary values (`p-[theme(--spacing-gutter)]`, `bg-[theme(--color-brand)]`, `w-[calc(theme(--spacing)*2)]`): a theme variable of the project, a color, or a spacing step. A call for a variable the build cannot read (including the v3 form `theme(colors.red.500)`) makes the class stay as written.
97+
98+
What is not read keeps its classes as written: `@plugin` and `@config` (they run JavaScript, whose output the build does not know: the classes a plugin such as `@tailwindcss/typography` makes, like `prose`, stay in the `className` for Tailwind's own build), nested rules in `@utility`, `theme()` in CSS (outside arbitrary values), `--theme()`, and a file whose braces do not balance. If the project changes the values of a namespace the build does not read (`--text-*`, `--font-*`, `--leading-*`, `--tracking-*`, `--container-*`, `--perspective-*`, `--aspect-*`), the classes that read it (`text-lg`, `font-bold`, `max-w-sm` ...) also stay as written, instead of compiling to a default the project replaced.
9099

91100
## Classes and style props on one element
92101

@@ -98,4 +107,4 @@ Tailwind classes and Devup UI props both become one atomic class per property in
98107

99108
## What stays as written
100109

101-
Utilities this build does not compile stay in the `className`: container (`container`, `@container` queries), the masks (`mask-*`), `text-shadow-*`, `inset-shadow-*`, composed font-variant-numeric (`tabular-nums`), 3D transform steps (`rotate-x-*`, `translate-z-*`, `scale-z-*`), `contain-*`, `outline-hidden`, and the variants and values that are not listed in Tailwind CSS v4.
110+
Utilities this build does not compile stay in the `className`: `container` (it sets a width per breakpoint, which one class cannot hold), the gradient masks (`mask-linear-*`, `mask-radial-*`, `mask-conic-*`, `mask-t|r|b|l|x|y-from|to-*`), and the variants and values that are not listed in Tailwind CSS v4. Container queries (`@sm:`, `@max-md:`, `@min-[400px]:`, `@sm/main:`) and the `@container` utilities, the other masks (`mask-none`, `mask-[...]`, sizes, positions, repeats, clips, origins, composites, modes), `text-shadow-*`, `inset-shadow-*`, `font-variant-numeric` (`tabular-nums` and the like, composing), 3D transforms (`rotate-x|y|z-*`, `translate-z-*`, `scale-z-*`, `translate-3d`, `scale-3d`, `transform-3d`, `backface-*`, `perspective-*`), `contain-*` and `outline-hidden` compile as Tailwind CSS v4 writes them.

‎libs/css/src/tailwind_definitions.rs‎

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,26 @@ pub struct CustomVariant {
1717
pub at_rule: Option<(AtRuleKind, String)>,
1818
}
1919

20+
/// One line of the body of an `@utility`
21+
#[derive(Debug, Clone, PartialEq, Eq)]
22+
pub enum UtilityItem {
23+
/// `property: value`
24+
Declaration(String, String),
25+
/// `@apply a b c`: the utilities that are applied
26+
Apply(Vec<String>),
27+
}
28+
2029
#[derive(Debug, Default)]
2130
struct Definitions {
2231
theme: BTreeMap<String, String>,
2332
/// Namespaces `--ns-*: initial` empties, such as `--color-`
2433
resets: BTreeSet<String>,
2534
/// `--*: initial` empties every namespace
2635
reset_all: bool,
27-
utilities: BTreeMap<String, Vec<(String, String)>>,
36+
utilities: BTreeMap<String, Vec<UtilityItem>>,
37+
/// `@utility name-*`, by `name`: declarations that read `--value()` and
38+
/// `--modifier()`
39+
functional: BTreeMap<String, Vec<(String, String)>>,
2840
variants: BTreeMap<String, CustomVariant>,
2941
keyframes: BTreeMap<String, String>,
3042
}
@@ -137,6 +149,25 @@ fn compact(value: &str) -> String {
137149
out
138150
}
139151

152+
/// The declarations and `@apply` lines of `body`, `None` when it holds anything
153+
/// else
154+
fn utility_items(body: &str) -> Option<Vec<UtilityItem>> {
155+
parse_items(body)?
156+
.into_iter()
157+
.map(|item| match item {
158+
Item::Statement(statement) => match statement.strip_prefix("@apply") {
159+
Some(tokens) if tokens.starts_with(char::is_whitespace) => Some(
160+
UtilityItem::Apply(tokens.split_whitespace().map(str::to_string).collect()),
161+
),
162+
Some(_) => None,
163+
None => declaration(&statement)
164+
.map(|(property, value)| UtilityItem::Declaration(property, value)),
165+
},
166+
Item::Block(..) => None,
167+
})
168+
.collect()
169+
}
170+
140171
/// `name: value` as the pair it declares
141172
fn declaration(statement: &str) -> Option<(String, String)> {
142173
let (name, value) = statement.split_once(':')?;
@@ -201,14 +232,24 @@ impl Definitions {
201232
}
202233

203234
fn read_utility(&mut self, name: &str, body: &str) {
204-
if name.is_empty() || name.contains('*') || name.contains(char::is_whitespace) {
235+
if name.is_empty() || name.contains(char::is_whitespace) {
205236
return;
206237
}
207-
if let Some(declarations) = declarations(body) {
208-
self.utilities.insert(name.to_string(), declarations);
238+
if let Some(root) = name.strip_suffix("-*") {
239+
if let Some(declarations) =
240+
declarations(body).filter(|_| !root.is_empty() && !root.contains('*'))
241+
{
242+
self.functional.insert(root.to_string(), declarations);
243+
}
244+
return;
245+
}
246+
if name.contains('*') {
247+
return;
248+
}
249+
if let Some(items) = utility_items(body) {
250+
self.utilities.insert(name.to_string(), items);
209251
}
210252
}
211-
212253
fn read_custom_variant(&mut self, rest: &str, body: Option<&str>) {
213254
let rest = rest.trim();
214255
let (name, definition) = rest.split_once(char::is_whitespace).unwrap_or((rest, ""));
@@ -388,12 +429,18 @@ pub fn theme_keys(namespace: &str) -> Vec<String> {
388429
.unwrap_or_default()
389430
}
390431

391-
/// The declarations of the `@utility` named `name`
432+
/// The body of the `@utility` named `name`
392433
#[must_use]
393-
pub fn custom_utility(name: &str) -> Option<Vec<(String, String)>> {
434+
pub fn custom_utility(name: &str) -> Option<Vec<UtilityItem>> {
394435
with(|definitions| definitions.utilities.get(name).cloned())
395436
}
396437

438+
/// The declarations of the functional `@utility` named `<root>-*`
439+
#[must_use]
440+
pub fn functional_utility(root: &str) -> Option<Vec<(String, String)>> {
441+
with(|definitions| definitions.functional.get(root).cloned())
442+
}
443+
397444
/// The `@custom-variant` named `name`
398445
#[must_use]
399446
pub fn custom_variant(name: &str) -> Option<CustomVariant> {
@@ -475,19 +522,41 @@ mod tests {
475522
#[serial]
476523
fn utilities_are_static_declarations() {
477524
set_tailwind_css(
478-
"@layer base { a { color: red } } @utility content-auto { content-visibility: auto; contain-intrinsic-size: 0 500px } @utility tab-* { tab-size: --value(integer); } @utility nested { &:hover { color: red } } @utility empty { } @utility two words { a: b }",
525+
"@layer base { a { color: red } } @utility content-auto { content-visibility: auto; contain-intrinsic-size: 0 500px } @utility tab-* { tab-size: --value(integer); } @utility nested { &:hover { color: red } } @utility empty { } @utility two words { a: b } @utility btn { @apply px-4 py-2; color: red; } @utility bad-apply { @applyx a; } @utility any-* x { a: b } @utility -* { a: b } @utility a*b { a: b } @utility fn-* { a: b; &:hover { c: d } }",
479526
);
480527
assert_eq!(
481528
custom_utility("content-auto"),
482529
Some(vec![
483-
("content-visibility".to_string(), "auto".to_string()),
484-
("contain-intrinsic-size".to_string(), "0 500px".to_string()),
530+
UtilityItem::Declaration("content-visibility".to_string(), "auto".to_string()),
531+
UtilityItem::Declaration(
532+
"contain-intrinsic-size".to_string(),
533+
"0 500px".to_string()
534+
),
485535
])
486536
);
487537
assert_eq!(custom_utility("tab-*"), None);
538+
assert_eq!(
539+
functional_utility("tab"),
540+
Some(vec![(
541+
"tab-size".to_string(),
542+
"--value(integer)".to_string()
543+
)])
544+
);
488545
assert_eq!(custom_utility("nested"), None);
489546
assert_eq!(custom_utility("empty"), Some(vec![]));
490547
assert_eq!(custom_utility("two"), None);
548+
assert_eq!(
549+
custom_utility("btn"),
550+
Some(vec![
551+
UtilityItem::Apply(vec!["px-4".to_string(), "py-2".to_string()]),
552+
UtilityItem::Declaration("color".to_string(), "red".to_string()),
553+
])
554+
);
555+
assert_eq!(custom_utility("bad-apply"), None);
556+
assert_eq!(functional_utility("any"), None);
557+
assert_eq!(functional_utility("fn"), None);
558+
assert_eq!(functional_utility(""), None);
559+
assert_eq!(custom_utility("a*b"), None);
491560
set_tailwind_css("");
492561
}
493562

0 commit comments

Comments
 (0)