From 4a0ae6e314783235a082ce8065befb9624a40caa Mon Sep 17 00:00:00 2001 From: Darren Fernandes Date: Thu, 12 Nov 2026 12:31:18 +0530 Subject: [PATCH] Preserve source locations for JavaScript keyframes --- .changelog/postcss-keyframe-sources.md | 3 + .../@tailwindcss-postcss/src/index.test.ts | 54 ++++++++++++++++++ .../src/compat/apply-compat-hooks.ts | 11 ++-- .../src/compat/apply-keyframes-to-theme.ts | 9 +++ .../src/compat/config/resolve-config.ts | 56 ++++++++++++++----- 5 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 .changelog/postcss-keyframe-sources.md diff --git a/.changelog/postcss-keyframe-sources.md b/.changelog/postcss-keyframe-sources.md new file mode 100644 index 000000000000..3c5155a39810 --- /dev/null +++ b/.changelog/postcss-keyframe-sources.md @@ -0,0 +1,3 @@ +### Fixed + +- Fix Vite's missing `from` warning when `@tailwindcss/postcss` emits keyframes defined in a JavaScript config diff --git a/packages/@tailwindcss-postcss/src/index.test.ts b/packages/@tailwindcss-postcss/src/index.test.ts index 79b51ab861e8..ad485abcdbe5 100644 --- a/packages/@tailwindcss-postcss/src/index.test.ts +++ b/packages/@tailwindcss-postcss/src/index.test.ts @@ -266,6 +266,60 @@ describe('plugins', () => { }) }) +test('keyframes from JavaScript configs retain their PostCSS source', async ({ + onTestFinished, +}) => { + let dir = await realpath(await mkdtemp(path.join(tmpdir(), 'tw-postcss'))) + onTestFinished(() => rm(dir, { recursive: true, force: true })) + + await writeFile( + path.join(dir, 'base.cjs'), + `module.exports = { + theme: { + animation: { 'fade-in': 'fade-in 1s' }, + keyframes: { 'fade-in': { from: { opacity: '0' } } }, + }, + }`, + ) + await writeFile( + path.join(dir, 'extension.cjs'), + `module.exports = { + theme: { + extend: { + animation: { 'fade-out': 'fade-out 1s' }, + keyframes: () => ({ 'fade-out': { to: { opacity: '0' } } }), + }, + }, + }`, + ) + + let from = path.join(dir, 'input.css') + let result = await postcss([tailwindcss({ base: dir, optimize: false })]).process( + css` + @config './base.cjs'; + @config './extension.cjs'; + @theme { + --color-example: red; + } + @tailwind utilities source(none); + @source inline('animate-fade-in animate-fade-out'); + `, + { from }, + ) + + let sources: [string, string | undefined, number | undefined][] = [] + result.root.walkAtRules('keyframes', (rule) => { + rule.walkDecls((decl) => { + sources.push([rule.params, decl.source?.input.file, decl.source?.start?.line]) + }) + }) + + expect(sources).toEqual([ + ['fade-in', from, 1], + ['fade-out', from, 2], + ]) +}) + test('bail early when Tailwind is not used', async () => { let processor = postcss([ tailwindcss({ base: `${__dirname}/fixtures/example-project`, optimize: { minify: false } }), diff --git a/packages/tailwindcss/src/compat/apply-compat-hooks.ts b/packages/tailwindcss/src/compat/apply-compat-hooks.ts index 5cfc29d563b2..6b4f8fc61907 100644 --- a/packages/tailwindcss/src/compat/apply-compat-hooks.ts +++ b/packages/tailwindcss/src/compat/apply-compat-hooks.ts @@ -284,10 +284,11 @@ function upgradeToFullPluginSupport({ ...userConfig, { config: { plugins: [darkModePlugin] }, base, reference: true, src: undefined }, ]) - let { resolvedConfig: resolvedUserConfig, replacedThemeKeys } = resolveConfig( - designSystem, - userConfig, - ) + let { + resolvedConfig: resolvedUserConfig, + replacedThemeKeys, + keyframeSources, + } = resolveConfig(designSystem, userConfig) let pluginApiConfig = { designSystem, @@ -368,7 +369,7 @@ function upgradeToFullPluginSupport({ // config would otherwise expand into namespaces like `background-color` which // core utilities already read from. applyConfigToTheme(designSystem, resolvedUserConfig, replacedThemeKeys) - applyKeyframesToTheme(designSystem, resolvedUserConfig) + applyKeyframesToTheme(designSystem, resolvedUserConfig, keyframeSources) registerThemeVariantOverrides(resolvedUserConfig, designSystem) registerScreensConfig(resolvedUserConfig, designSystem) diff --git a/packages/tailwindcss/src/compat/apply-keyframes-to-theme.ts b/packages/tailwindcss/src/compat/apply-keyframes-to-theme.ts index d5855545f1c6..d083a39a9535 100644 --- a/packages/tailwindcss/src/compat/apply-keyframes-to-theme.ts +++ b/packages/tailwindcss/src/compat/apply-keyframes-to-theme.ts @@ -1,13 +1,22 @@ import { atRule, type AtRule } from '../ast' import type { DesignSystem } from '../design-system' +import type { SourceLocation } from '../source-maps/source' +import { walk } from '../walk' import type { ResolvedConfig } from './config/types' import { objectToAst } from './plugin-api' export function applyKeyframesToTheme( designSystem: DesignSystem, resolvedConfig: Pick, + sources?: ReadonlyMap, ) { for (let rule of keyframesToRules(resolvedConfig)) { + let src = sources?.get(rule.params) + if (src) { + walk([rule], (node) => { + node.src = src + }) + } designSystem.theme.addKeyframes(rule) } } diff --git a/packages/tailwindcss/src/compat/config/resolve-config.ts b/packages/tailwindcss/src/compat/config/resolve-config.ts index 858f4bbbe14a..252e16ebfeb0 100644 --- a/packages/tailwindcss/src/compat/config/resolve-config.ts +++ b/packages/tailwindcss/src/compat/config/resolve-config.ts @@ -22,11 +22,11 @@ export interface ConfigFile { interface ResolutionContext { design: DesignSystem - configs: UserConfig[] + configs: Pick[] plugins: PluginWithConfig[] content: ResolvedContentConfig theme: Record - extend: Record + extend: Record result: ResolvedConfig } @@ -47,7 +47,11 @@ let minimal: ResolvedConfig = { export function resolveConfig( design: DesignSystem, files: ConfigFile[], -): { resolvedConfig: ResolvedConfig; replacedThemeKeys: Set } { +): { + resolvedConfig: ResolvedConfig + replacedThemeKeys: Set + keyframeSources: Map +} { let ctx: ResolutionContext = { design, configs: [], @@ -67,7 +71,7 @@ export function resolveConfig( } // Merge top level keys - for (let config of ctx.configs) { + for (let { config } of ctx.configs) { if ('darkMode' in config && config.darkMode !== undefined) { ctx.result.darkMode = config.darkMode ?? null } @@ -86,7 +90,7 @@ export function resolveConfig( } // Merge themes - let replacedThemeKeys = mergeTheme(ctx) + let { replacedThemeKeys, keyframeSources } = mergeTheme(ctx) return { resolvedConfig: { @@ -96,6 +100,7 @@ export function resolveConfig( plugins: ctx.plugins, }, replacedThemeKeys, + keyframeSources, } } @@ -192,11 +197,13 @@ function extractConfigs( } // Then apply the "user" config - ctx.configs.push(config) + ctx.configs.push({ config, src }) } -function mergeTheme(ctx: ResolutionContext): Set { +function mergeTheme(ctx: ResolutionContext) { let replacedThemeKeys: Set = new Set() + let keyframeSources = new Map() + let baseKeyframesSource: SourceLocation | undefined let themeFn = createThemeFn(ctx.design, () => ctx.theme, resolveValue) let theme = Object.assign(themeFn, { @@ -212,7 +219,13 @@ function mergeTheme(ctx: ResolutionContext): Set { return value ?? null } - for (let config of ctx.configs) { + function recordKeyframeSources(keyframes: ResolvedThemeValue, src: SourceLocation | undefined) { + for (let name of Object.keys(keyframes ?? {})) { + keyframeSources.set(name, src) + } + } + + for (let { config, src } of ctx.configs) { let theme = config.theme ?? {} let extend = theme.extend ?? {} @@ -226,11 +239,14 @@ function mergeTheme(ctx: ResolutionContext): Set { // Shallow merge themes so latest "group" wins Object.assign(ctx.theme, theme) + if (Object.hasOwn(theme, 'keyframes')) { + baseKeyframesSource = src + } // Collect extensions by key so each group can be lazily deep merged for (let key in extend) { ctx.extend[key] ??= [] - ctx.extend[key].push(extend[key]) + ctx.extend[key].push({ value: extend[key], src }) } } @@ -240,10 +256,20 @@ function mergeTheme(ctx: ResolutionContext): Set { // Deep merge every `extend` key into the theme for (let key in ctx.extend) { - let values = [ctx.theme[key], ...ctx.extend[key]] + let values = [ + { value: ctx.theme[key], src: key === 'keyframes' ? baseKeyframesSource : undefined }, + ...ctx.extend[key], + ] ctx.theme[key] = () => { - let v = values.map(resolveValue) + let v = values.map(({ value }) => resolveValue(value)) + if (key === 'keyframes') { + // Attribute each rule to the last config that contributed to it. + keyframeSources.clear() + for (let i = 0; i < v.length; i++) { + recordKeyframeSources(v[i], values[i].src) + } + } let result = deepMerge({}, v, mergeThemeExtension) return result @@ -251,7 +277,11 @@ function mergeTheme(ctx: ResolutionContext): Set { } for (let key in ctx.theme) { - ctx.theme[key] = resolveValue(ctx.theme[key]) + let value = resolveValue(ctx.theme[key]) + ctx.theme[key] = value + if (key === 'keyframes' && !ctx.extend.keyframes) { + recordKeyframeSources(value, baseKeyframesSource) + } } // Turn {min: '123px'} into '123px' in screens @@ -269,5 +299,5 @@ function mergeTheme(ctx: ResolutionContext): Set { } } - return replacedThemeKeys + return { replacedThemeKeys, keyframeSources } }