Skip to content

Commit 3f313b4

Browse files
Ensure that the CSS file rebuilds if a new CSS variable is used from templates (#17301)
Fixes #17288 This PR fixes an issue where changes in template files that _only mark a new CSS variable as used_ was not causing the CSS file to rebuilds. This also fixes a flaky integration test that was caused by a missing `await` line on the final assertion (causing it to sometimes run the assertion in time while the test runner was still running). ## Test plan Turns out we already had an integration test for this but it wasn't correctly running due to the missing await.
1 parent ca7b10e commit 3f313b4

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3737
- Fix class extraction followed by `(` in Slim ([#17278](https://github.com/tailwindlabs/tailwindcss/pull/17278))
3838
- Export `PluginUtils` from `tailwindcss/plugin` for compatibility with v3 ([#17299](https://github.com/tailwindlabs/tailwindcss/pull/17299))
3939
- Increase Standalone hardware compatibility on macOS x64 builds ([#17267](https://github.com/tailwindlabs/tailwindcss/pull/17267))
40+
- Ensure that the CSS file rebuilds if a new CSS variable is used from templates ([#17301](https://github.com/tailwindlabs/tailwindcss/pull/17301))
4041

4142
## [4.0.14] - 2025-03-13
4243

integrations/cli/index.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1303,11 +1303,11 @@ test(
13031303
`,
13041304
)
13051305

1306-
fs.expectFileToContain(
1306+
// prettier-ignore
1307+
await fs.expectFileToContain(
13071308
'./dist/out.css',
13081309
css`
1309-
:root,
1310-
:host {
1310+
:root, :host {
13111311
--color-blue-500: blue;
13121312
}
13131313
`,

packages/tailwindcss/src/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -696,18 +696,21 @@ export async function compileAst(
696696
}
697697

698698
let didChange = defaultDidChange
699+
let didAddExternalVariable = false
699700
defaultDidChange = false
700701

701702
// Add all new candidates unless we know that they are invalid.
702703
let prevSize = allValidCandidates.size
703704
for (let candidate of newRawCandidates) {
704705
if (!designSystem.invalidCandidates.has(candidate)) {
705706
if (candidate[0] === '-' && candidate[1] === '-') {
706-
designSystem.theme.markUsedVariable(candidate)
707+
let didMarkVariableAsUsed = designSystem.theme.markUsedVariable(candidate)
708+
didChange ||= didMarkVariableAsUsed
709+
didAddExternalVariable ||= didMarkVariableAsUsed
707710
} else {
708711
allValidCandidates.add(candidate)
712+
didChange ||= allValidCandidates.size !== prevSize
709713
}
710-
didChange ||= allValidCandidates.size !== prevSize
711714
}
712715
}
713716

@@ -725,7 +728,7 @@ export async function compileAst(
725728
// If no new ast nodes were generated, then we can return the original
726729
// CSS. This currently assumes that we only add new ast nodes and never
727730
// remove any.
728-
if (previousAstNodeCount === newNodes.length) {
731+
if (!didAddExternalVariable && previousAstNodeCount === newNodes.length) {
729732
compiled ??= optimizeAst(ast, designSystem)
730733
return compiled
731734
}

packages/tailwindcss/src/theme.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,13 @@ export class Theme {
193193
return `var(${escape(this.prefixKey(themeKey))}${fallback ? `, ${fallback}` : ''})`
194194
}
195195

196-
markUsedVariable(themeKey: string) {
196+
markUsedVariable(themeKey: string): boolean {
197197
let key = unescape(this.#unprefixKey(themeKey))
198198
let value = this.values.get(key)
199-
if (!value) return
199+
if (!value) return false
200+
let isUsed = value.options & ThemeOptions.USED
200201
value.options |= ThemeOptions.USED
202+
return !isUsed
201203
}
202204

203205
resolve(

0 commit comments

Comments
 (0)