Skip to content

Conversation

thecrypticace
Copy link
Contributor

Fixes #18178

When someone writes a utility like after:content-['foo'] it'll produce duplicate content: var(--tw-content) declarations. I thought about special casing these but we already have an optimization pass where we perform a full walk of the AST, flattening some rules (with the & selector), analyzing declarations, etc… We can utilize that existing spot in core to analyze and remove duplicate declarations within rules across the AST.

The implementation does this by keeping track of declarations within a style rule and keeps the last one for any exact duplicate which is a tuple of (property, value, important). This does require some additional loops but preseving the last declaration is important for correctness with regards to CSS nesting.

For example take this nested CSS:

.foo {
  color: red;
  & .bar {
    color: green;
  }
  color: red;
}

It expands to this:

.foo {
  color: red;
}
.foo.bar {
  color: green;
}
.foo {
  color: red;
}

If you remove the last rule then a <div class="foo bar">…</div> will have green text when its supposed to be red. Since that would affect behavior we have to always preserve the last declaration for a given property.

We could go further and eliminate multiple declarations for the same property but this presents a problem: every property and value must be understood and combined with browser targets to understand whether or not that property may act as a "fallback" or whether definitely overwrites its previous value in all cases. This is a much more complicated task that is much more suited to something light Lighting CSS.

Nested rules with an `&` are already flattened by this point so their content is also considered to be part of the same rule
@thecrypticace thecrypticace marked this pull request as ready for review August 28, 2025 15:08
@thecrypticace thecrypticace requested a review from a team as a code owner August 28, 2025 15:08
@thecrypticace thecrypticace changed the title Optimize exact duplicate declarations Drop exact duplicate declarations from output CSS Aug 28, 2025
@thecrypticace thecrypticace changed the title Drop exact duplicate declarations from output CSS Drop exact duplicate declarations from output CSS within a style rule Aug 28, 2025
RobinMalfait
RobinMalfait approved these changes Aug 29, 2025
Comment on lines +370 to +371
seen[key] ??= []
seen[key].push(child)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jk 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean basically lol

@thecrypticace thecrypticace merged commit 5e2a160 into main Aug 29, 2025
7 checks passed
@thecrypticace thecrypticace deleted the feat/optimize-duplicate-decls branch August 29, 2025 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use a content utility with before/after variants adds the content CSS property twice

2 participants