Skip to content

Commit b0e1f47

Browse files
committed
Be more direct about variant group extractions
Also adds a previously failing but now passing test case
1 parent 770900b commit b0e1f47

File tree

2 files changed

+85
-40
lines changed

2 files changed

+85
-40
lines changed

src/lib/defaultExtractor.js

+50-40
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,47 @@ function* buildRegExps(context) {
2323
let separator = context.tailwindConfig.separator
2424
let variantGroupingEnabled = flagEnabled(context.tailwindConfig, 'variantGrouping')
2525

26+
let utility = regex.any([
27+
// Arbitrary properties
28+
/\[[^\s:'"]+:[^\s\]]+\]/,
29+
30+
// Utilities
31+
regex.pattern([
32+
// Utility Name / Group Name
33+
/-?(?:\w+)/,
34+
35+
// Normal/Arbitrary values
36+
regex.optional(
37+
regex.any([
38+
regex.pattern([
39+
// Arbitrary values
40+
/-\[[^\s:]+\]/,
41+
42+
// Not immediately followed by an `{[(`
43+
/(?![{([]])/,
44+
45+
// optionally followed by an opacity modifier
46+
/(?:\/[^\s'"\\$]*)?/,
47+
]),
48+
49+
regex.pattern([
50+
// Arbitrary values
51+
/-\[[^\s]+\]/,
52+
53+
// Not immediately followed by an `{[(`
54+
/(?![{([]])/,
55+
56+
// optionally followed by an opacity modifier
57+
/(?:\/[^\s'"\\$]*)?/,
58+
]),
59+
60+
// Normal values w/o quotes — may include an opacity modifier
61+
/[-\/][^\s'"\\$={]*/,
62+
])
63+
),
64+
]),
65+
])
66+
2667
yield regex.pattern([
2768
// Variants
2869
'((?=((',
@@ -38,46 +79,15 @@ function* buildRegExps(context) {
3879
// Important (optional)
3980
/!?/,
4081

41-
regex.any([
42-
// Arbitrary properties
43-
/\[[^\s:'"]+:[^\s\]]+\]/,
44-
45-
// Utilities
46-
regex.pattern([
47-
// Utility Name / Group Name
48-
variantGroupingEnabled ? /-?(?:[\w,()]+)/ : /-?(?:\w+)/,
49-
50-
// Normal/Arbitrary values
51-
regex.optional(
52-
regex.any([
53-
regex.pattern([
54-
// Arbitrary values
55-
/-\[[^\s:]+\]/,
56-
57-
// Not immediately followed by an `{[(`
58-
/(?![{([]])/,
59-
60-
// optionally followed by an opacity modifier
61-
/(?:\/[^\s'"\\$]*)?/,
62-
]),
63-
64-
regex.pattern([
65-
// Arbitrary values
66-
/-\[[^\s]+\]/,
67-
68-
// Not immediately followed by an `{[(`
69-
/(?![{([]])/,
70-
71-
// optionally followed by an opacity modifier
72-
/(?:\/[^\s'"\\$]*)?/,
73-
]),
74-
75-
// Normal values w/o quotes — may include an opacity modifier
76-
/[-\/][^\s'"\\$={]*/,
77-
])
78-
),
79-
]),
80-
]),
82+
variantGroupingEnabled
83+
? regex.any([
84+
// Or any of those things but grouped separated by commas
85+
regex.pattern([/\(/, utility, regex.zeroOrMore([/,/, utility]), /\)/]),
86+
87+
// Arbitrary properties, constrained utilities, arbitrary values, etc…
88+
utility,
89+
])
90+
: utility,
8191
])
8292

8393
// 5. Inner matches

tests/variant-grouping.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,41 @@ it('should be possible to group variants', () => {
4949
})
5050
})
5151

52+
it('should be possible to group using constrained and arbitrary variants together', () => {
53+
let config = {
54+
experimental: 'all',
55+
content: [
56+
{
57+
raw: html`<div
58+
class="dark:[@supports(hover:hover)]:hover:[&>*]:([--potato:baked],bg-[#0088cc])"
59+
></div>`,
60+
},
61+
],
62+
corePlugins: { preflight: false },
63+
plugins: [],
64+
}
65+
66+
let input = css`
67+
@tailwind utilities;
68+
`
69+
70+
return run(input, config).then((result) => {
71+
expect(result.css).toMatchFormattedCss(css`
72+
@media (prefers-color-scheme: dark) {
73+
@supports (hover: hover) {
74+
.dark\:\[\@supports\(hover\:hover\)\]\:hover\:\[\&\>\*\]\:\(\[--potato\:baked\]\2c
75+
bg-\[\#0088cc\]\)
76+
> *:hover {
77+
--tw-bg-opacity: 1;
78+
background-color: rgb(0 136 204 / var(--tw-bg-opacity));
79+
--potato: baked;
80+
}
81+
}
82+
}
83+
`)
84+
})
85+
})
86+
5287
it('should be possible to group multiple variants', () => {
5388
let config = {
5489
experimental: 'all',

0 commit comments

Comments
 (0)