Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Ensure `@tailwindcss/postcss` rewrites relative `url(…)`s of stylesheets that are inlined before it runs (e.g. `@import`s in Vite) in optimized builds ([#20576](https://github.com/tailwindlabs/tailwindcss/pull/20576))
- Drop nested rules that put a combinator after a pseudo-element when using pseudo-element variants (e.g. `selection:*` on a utility with a nested `& > *` rule) ([#20575](https://github.com/tailwindlabs/tailwindcss/pull/20575))
- Don't generate CSS for arbitrary values with unbalanced parentheses or brackets (e.g. `w-[calc(1px]` or `[--x:a[b]`) ([#20577](https://github.com/tailwindlabs/tailwindcss/pull/20577))
- Canonicalization: prevent `collapse: true` from taking exponentially longer when classes are linked through a utility that sets many properties (e.g. `sr-only`) ([#20558](https://github.com/tailwindlabs/tailwindcss/pull/20558))

## [4.3.3] - 2026-07-16

Expand Down
70 changes: 70 additions & 0 deletions packages/tailwindcss/src/canonicalize-candidates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,76 @@ describe('regressions', () => {
})
}

// https://github.com/tailwindlabs/tailwindcss/issues/20557
test(
'collapse does not try every combination of candidates linked by a utility that sets many properties',
{ timeout },
async () => {
let designSystem = await designSystems.get(__dirname).get(css`
@import 'tailwindcss';

@utility pill {
position: relative;
display: inline-flex;
align-items: center;
gap: 0.5rem;
height: 2rem;
padding: 0.25rem 0.75rem;
margin: 0;
border-radius: 9999px;
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 500;
color: white;
background-color: black;
white-space: nowrap;
overflow: hidden;
cursor: pointer;
}
`)

// Every candidate sets a property that `sr-only`, `not-sr-only` or
// `pill` also sets, so they used to end up in one group of linked
// candidates. Trying every combination of 2^n candidates of that group
// took tens of seconds.
let candidates = [
'relative',
'mt-1',
'mr-2',
'mb-3',
'ml-4',
'pt-1',
'pr-2',
'pb-3',
'pl-4',
'w-64',
'h-10',
'overflow-x-auto',
'overflow-y-hidden',
'whitespace-nowrap',
'flex',
'items-center',
'gap-2.5',
'rounded-full',
'bg-white',
'cursor-pointer',
'font-normal',
'text-sm',
'leading-5',
]

let start = performance.now()
let result = designSystem.canonicalizeCandidates(candidates, DEFAULT_CANONICALIZATION_OPTIONS)
let duration = performance.now() - start

expect(result).toEqual([
...candidates.filter((candidate) => candidate !== 'text-sm' && candidate !== 'leading-5'),
'text-sm/5',
])
expect(duration).toBeLessThan(5_000)
},
)

// https://github.com/tailwindlabs/tailwindcss/pull/19727
test(
'collapse does not crash when utilities with no standard properties are present',
Expand Down
64 changes: 64 additions & 0 deletions packages/tailwindcss/src/canonicalize-candidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,66 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st
return result ?? new Set<string>()
})

// A replacement can only ever have the same signature as a combination of
// candidates if every property it sets is also set by one of the
// candidates in that combination. Each candidate in such a combination
// also lists that replacement in its `otherUtilities`, so a replacement
// whose properties are not covered by all the candidates that list it can
// never be used.
//
// Dropping those replacements up front is important for performance: some
// utilities set a lot of properties (e.g.: `sr-only`, or a project
// `@utility` used as a component class). Without this step they link
// almost every candidate to every other candidate, and since we try every
// combination of linked candidates, the work doubles with every candidate
// in such a group.
//
// E.g.: `relative` and `mt-1` are both covered by `sr-only` (it sets
// `position` and `margin-top`), but `sr-only` also sets properties like
// `clip-path` that none of the candidates set, so it can never be a valid
// replacement and should not link them.
//
// A replacement that is listed by a single candidate can't link candidates
// or replace a combination either, so we can drop those without computing
// their coverage. This keeps the common case of unrelated candidates fast.
let seen = new Set<string>()
let shared = new Set<string>()
for (let replacements of otherUtilities) {
for (let replacement of replacements) {
if (seen.has(replacement)) shared.add(replacement)
else seen.add(replacement)
}
}

let coveredProperties = new DefaultMap((_replacement: string) => new Set<string>())
for (let [idx, replacements] of otherUtilities.entries()) {
for (let replacement of replacements) {
if (!shared.has(replacement)) continue
let covered = coveredProperties.get(replacement)
for (let property of candidatePropertiesValues[idx].keys()) {
covered.add(property)
}
}
}

let isPossibleReplacement = new DefaultMap((replacement: string) => {
let covered = coveredProperties.get(replacement)
for (let property of computeUtilitiesPropertiesLookup.get(replacement).keys()) {
if (!covered.has(property)) return false
}
return true
})

otherUtilities = otherUtilities.map((replacements) => {
let result = new Set<string>()
for (let replacement of replacements) {
if (shared.has(replacement) && isPossibleReplacement.get(replacement)) {
result.add(replacement)
}
}
return result
})

// Link each candidate that could be linked via another utility
// (intersection). This way we can reduce the amount of required combinations.
//
Expand Down Expand Up @@ -475,6 +535,10 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st

let potentialReplacements = combo.flatMap((idx) => otherUtilities[idx]).reduce(intersection)

// Computing the signature of the combination is expensive, so skip it
// when there is nothing to compare it against.
if (potentialReplacements.size === 0) continue

let collapsed = combo
.map((idx) => candidates[idx])
.sort((a, z) => a.localeCompare(z)) // Sort to increase cache hits
Expand Down