Skip to content

Do not emit @keyframes in @theme reference #16120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 31, 2025
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 @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Vite: Ensure hot-reloading works with SolidStart setups ([#16052](https://github.com/tailwindlabs/tailwindcss/pull/16052))
- Vite: Fix a crash when starting the development server in SolidStart setups ([#16052](https://github.com/tailwindlabs/tailwindcss/pull/16052))
- Prevent camelCasing CSS custom properties added by JavaScript plugins ([#16103](https://github.com/tailwindlabs/tailwindcss/pull/16103))
- Do not emit `@keyframes` in `@theme reference` ([#16120](https://github.com/tailwindlabs/tailwindcss/pull/16120))

## [4.0.1] - 2025-01-29

Expand Down
62 changes: 62 additions & 0 deletions integrations/cli/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1196,3 +1196,65 @@ test(
`)
},
)

test(
'@theme reference should never emit values',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/cli": "workspace:^"
}
}
`,
'src/index.css': css`
@reference "tailwindcss";

.keep-me {
color: red;
}
`,
},
},
async ({ fs, spawn, expect }) => {
let process = await spawn(
`pnpm tailwindcss --input src/index.css --output dist/out.css --watch`,
)
await process.onStderr((m) => m.includes('Done in'))

expect(await fs.dumpFiles('./dist/*.css')).toMatchInlineSnapshot(`
"
--- ./dist/out.css ---
.keep-me {
color: red;
}
"
`)

await fs.write(
'./src/index.css',
css`
@reference "tailwindcss";

/* Not a reference! */
@theme {
--color-pink: pink;
}

.keep-me {
color: red;
}
`,
)
expect(await fs.dumpFiles('./dist/*.css')).toMatchInlineSnapshot(`
"
--- ./dist/out.css ---
.keep-me {
color: red;
}
"
`)
},
)
69 changes: 69 additions & 0 deletions packages/tailwindcss/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,75 @@ describe('Parsing themes values from CSS', () => {
`)
})

test('`@keyframes` added in `@theme reference` should not be emitted', async () => {
return expect(
await compileCss(
css`
@theme reference {
--animate-foo: foo 1s infinite;

@keyframes foo {
0%,
100% {
color: red;
}
50% {
color: blue;
}
}
}
@tailwind utilities;
`,
['animate-foo'],
),
).toMatchInlineSnapshot(`
".animate-foo {
animation: var(--animate-foo);
}"
`)
})

test('`@keyframes` added in `@theme reference` should not be emitted, even if another `@theme` block exists', async () => {
return expect(
await compileCss(
css`
@theme reference {
--animate-foo: foo 1s infinite;

@keyframes foo {
0%,
100% {
color: red;
}
50% {
color: blue;
}
}
}

@theme {
--color-pink: pink;
}

@tailwind utilities;
`,
['bg-pink', 'animate-foo'],
),
).toMatchInlineSnapshot(`
":root, :host {
--color-pink: pink;
}

.animate-foo {
animation: var(--animate-foo);
}

.bg-pink {
background-color: var(--color-pink);
}"
`)
})

test('theme values added as reference that override existing theme value suppress the output of the original theme value as a variable', async () => {
expect(
await compileCss(
Expand Down
6 changes: 6 additions & 0 deletions packages/tailwindcss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,12 @@ async function parseCss(
// Collect `@keyframes` rules to re-insert with theme variables later,
// since the `@theme` rule itself will be removed.
if (child.kind === 'at-rule' && child.name === '@keyframes') {
// Do not track/emit `@keyframes`, if they are part of a `@theme reference`.
if (themeOptions & ThemeOptions.REFERENCE) {
replaceWith([])
return WalkAction.Skip
}

Comment on lines +457 to +462
Copy link
Member

Choose a reason for hiding this comment

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

Hmm my initial thought was that we might want this in the Theme just like the --animate-* variables and just also add the themeOption for bookkeeping but we don't really do any validations for that anymore afterwards so if this works, it should be fine too 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah this just never really tracks it in Theme. It does mean that animate-foo won't emit the @keyframes, but that sounds correct to me because the idea is that this can be used in <style> blocks, assuming the @keyframes exist somewhere else already.

Just looking into why the other integration test fails. 😅

theme.addKeyframes(child)
replaceWith([])
return WalkAction.Skip
Expand Down