Skip to content

Commit ced5c3a

Browse files
committed
Substitute @variant inside utilities
1 parent 5483f6d commit ced5c3a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

packages/tailwindcss/src/design-system.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { Theme, ThemeOptions, type ThemeKey } from './theme'
2323
import { Utilities, createUtilities, withAlpha } from './utilities'
2424
import { DefaultMap } from './utils/default-map'
2525
import { extractUsedVariables } from './utils/variables'
26-
import { Variants, createVariants } from './variants'
26+
import { Variants, createVariants, substituteAtVariant } from './variants'
2727

2828
export const enum CompileAstFlags {
2929
None = 0,
@@ -86,6 +86,12 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
8686
ast.map(({ node }) => node),
8787
designSystem,
8888
)
89+
90+
// JS plugins might contain an `@variant` inside a generated utility
91+
substituteAtVariant(
92+
ast.map(({ node }) => node),
93+
designSystem,
94+
)
8995
} catch (err) {
9096
// If substitution fails then the candidate likely contains a call to
9197
// `theme()` that is invalid which may be because of incorrect usage,

packages/tailwindcss/src/index.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4678,14 +4678,19 @@ test('JS APIs support @variant', async () => {
46784678
loadModule: async () => ({
46794679
path: '',
46804680
base: '/root',
4681-
module: ({ addBase }: PluginAPI) => {
4681+
module: ({ addBase, addUtilities, matchUtilities }: PluginAPI) => {
46824682
addBase({ body: { '@variant dark': { color: 'red' } } })
4683+
addUtilities({ '.foo': { '@variant dark': { '--foo': 'foo' } } })
4684+
matchUtilities(
4685+
{ bar: (value) => ({ '@variant dark': { '--bar': value } }) },
4686+
{ values: { one: '1' } },
4687+
)
46834688
},
46844689
}),
46854690
},
46864691
)
46874692

4688-
let compiled = build(['underline'])
4693+
let compiled = build(['underline', 'foo', 'bar-one'])
46894694

46904695
expect(optimizeCss(compiled).trim()).toMatchInlineSnapshot(`
46914696
"@layer base {
@@ -4700,6 +4705,16 @@ test('JS APIs support @variant', async () => {
47004705
.underline {
47014706
text-decoration-line: underline;
47024707
}
4708+
4709+
@media (prefers-color-scheme: dark) {
4710+
.bar-one {
4711+
--bar: 1;
4712+
}
4713+
4714+
.foo {
4715+
--foo: foo;
4716+
}
4717+
}
47034718
}"
47044719
`)
47054720
})

0 commit comments

Comments
 (0)