Skip to content

Commit 7bece4d

Browse files
Re-enable: Only expose used CSS variables (#16676)
This PR re-enables the changes necessary to remove unused theme variables and keyframes form your CSS. This change was initially landed as #16211 and then later reverted in #16403 because we found some unexpected interactions with using `@apply` and CSS variables in multi-root setups like CSS modules or Vue inline `<style>` blocks that were no longer seeing their required variables defined. This issue is fixed by now ensuring that theme variables that are defined within an `@reference "…"` boundary will still be emitted in the generated CSS when used (as this would otherwise not generate a valid stylesheet). So given the following input CSS: ```css @reference "tailwindcss"; .text-red { @apply text-red-500; } ``` We will now compile this to: ```css @layer theme { :root, :host { --text-red-500: oklch(0.637 0.237 25.331); } } .text-red { color: var(--text-red-500); } ``` This PR also improves the initial implementation to not mark theme variables as used if they are only used to define other theme variables. For example: ```css @theme { --font-sans: ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'; --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace; --default-font-family: var(--font-sans); --default-mono-font-family: var(--font-mono); } .default-font-family { font-family: var(--default-font-family); } ``` This would be reduced to the following now as `--font-mono` is only used to define another variable and never used outside the theme block: ```css :root, :host { --font-sans: ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'; --default-font-family: var(--font-sans); } .default-font-family { font-family: var(--default-font-family); } ``` ## Test plan - See updated unit and integration tests - Validated it works end-to-end by using a SvelteKit example
1 parent dd7d8fd commit 7bece4d

21 files changed

+516
-1562
lines changed

CHANGELOG.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- _Experimental_: Add `user-valid` and `user-invalid` variants ([#12370](https://github.com/tailwindlabs/tailwindcss/pull/12370))
1313

14+
### Changed
15+
16+
- Don't include theme variables that aren't used in compiled CSS ([#16211](https://github.com/tailwindlabs/tailwindcss/pull/16211), [#16676](https://github.com/tailwindlabs/tailwindcss/pull/16676))
17+
1418
### Fixed
1519

1620
- Remove invalid `!important` on CSS variable declarations ([#16668](https://github.com/tailwindlabs/tailwindcss/pull/16668))
@@ -44,9 +48,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4448
### Fixed
4549

4650
- Revert change to no longer include theme variables that aren't used in compiled CSS ([#16403](https://github.com/tailwindlabs/tailwindcss/pull/16403))
47-
48-
### Fixed
49-
5051
- Upgrade: Don't migrate `blur` to `blur-sm` when used with Next.js `<Image placeholder="blur" />` ([#16405](https://github.com/tailwindlabs/tailwindcss/pull/16405))
5152

5253
## [4.0.5] - 2025-02-08

integrations/cli/index.test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1289,9 +1289,7 @@ test(
12891289
expect(await fs.dumpFiles('./dist/*.css')).toMatchInlineSnapshot(`
12901290
"
12911291
--- ./dist/out.css ---
1292-
:root, :host {
1293-
--color-blue-500: blue;
1294-
}
1292+
<EMPTY>
12951293
"
12961294
`)
12971295

packages/@tailwindcss-postcss/src/__snapshots__/index.test.ts.snap

-383
Large diffs are not rendered by default.

packages/@tailwindcss-postcss/src/index.test.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,7 @@ test('runs `Once` plugins in the right order', async () => {
330330
)
331331

332332
expect(result.css.trim()).toMatchInlineSnapshot(`
333-
":root, :host {
334-
--color-red-500: red;
335-
}
336-
337-
.custom-css {
333+
".custom-css {
338334
color: red;
339335
}"
340336
`)
@@ -347,11 +343,7 @@ test('runs `Once` plugins in the right order', async () => {
347343
}"
348344
`)
349345
expect(after).toMatchInlineSnapshot(`
350-
":root, :host {
351-
--color-red-500: red;
352-
}
353-
354-
.custom-css {
346+
".custom-css {
355347
color: red;
356348
}"
357349
`)

0 commit comments

Comments
 (0)