Skip to content

Commit 5542340

Browse files
Eliminate irrelevant rules when applying variants (#12113)
* Eliminate irrelevant rules when applying variants * Update changelog
1 parent 457d2b1 commit 5542340

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Skip over classes inside `:not(…)` when nested in an at-rule ([#12105](https://github.com/tailwindlabs/tailwindcss/pull/12105))
2323
- Update types to work with `Node16` module resolution ([#12097](https://github.com/tailwindlabs/tailwindcss/pull/12097))
2424
- Don’t crash when important and parent selectors are equal in `@apply` ([#12112](https://github.com/tailwindlabs/tailwindcss/pull/12112))
25+
- Eliminate irrelevant rules when applying variants ([#12113](https://github.com/tailwindlabs/tailwindcss/pull/12113))
2526

2627
### Added
2728

src/lib/generateRules.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,10 +815,19 @@ function applyFinalFormat(match, { context, candidate }) {
815815
}
816816

817817
try {
818-
rule.selector = finalizeSelector(rule.selector, finalFormat, {
818+
let selector = finalizeSelector(rule.selector, finalFormat, {
819819
candidate,
820820
context,
821821
})
822+
823+
// Finalize Selector determined that this candidate is irrelevant
824+
// TODO: This elimination should happen earlier so this never happens
825+
if (selector === null) {
826+
rule.remove()
827+
return
828+
}
829+
830+
rule.selector = selector
822831
} catch {
823832
// If this selector is invalid we also want to skip it
824833
// But it's likely that being invalid here means there's a bug in a plugin rather than too loosely matching content

src/util/formatVariantSelector.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ export function finalizeSelector(current, formats, { context, candidate, base })
186186
// Remove extraneous selectors that do not include the base candidate
187187
selector.each((sel) => eliminateIrrelevantSelectors(sel, base))
188188

189+
// If ffter eliminating irrelevant selectors, we end up with nothing
190+
// Then the whole "rule" this is associated with does not need to exist
191+
// We use `null` as a marker value for that case
192+
if (selector.length === 0) {
193+
return null
194+
}
195+
189196
// If there are no formats that means there were no variants added to the candidate
190197
// so we can just return the selector as-is
191198
let formatAst = Array.isArray(formats)

tests/basic-usage.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,3 +792,46 @@ test('Skips classes inside :not() when nested inside an at-rule', async () => {
792792

793793
expect(result.css).toMatchFormattedCss(css``)
794794
})
795+
796+
test('Irrelevant rules are removed when applying variants', async () => {
797+
let config = {
798+
content: [
799+
{
800+
raw: html` <div class="md:w-full"></div> `,
801+
},
802+
],
803+
corePlugins: { preflight: false },
804+
plugins: [
805+
function ({ addUtilities }) {
806+
addUtilities({
807+
'@supports (foo: bar)': {
808+
// This doesn't contain `w-full` so it should not exist in the output
809+
'.outer': { color: 'red' },
810+
'.outer:is(.w-full)': { color: 'green' },
811+
},
812+
})
813+
},
814+
],
815+
}
816+
817+
let input = css`
818+
@tailwind utilities;
819+
`
820+
821+
// We didn't find the hand class therefore
822+
// nothing should be generated
823+
let result = await run(input, config)
824+
825+
expect(result.css).toMatchFormattedCss(css`
826+
@media (min-width: 768px) {
827+
.md\:w-full {
828+
width: 100%;
829+
}
830+
@supports (foo: bar) {
831+
.outer.md\:w-full {
832+
color: green;
833+
}
834+
}
835+
}
836+
`)
837+
})

0 commit comments

Comments
 (0)