From 49bb4b8800c84c0daf97ddb576674b4a2ab1e5e8 Mon Sep 17 00:00:00 2001 From: Darren Fernandes Date: Thu, 12 Nov 2026 12:31:18 +0530 Subject: [PATCH 1/2] 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 } } From 55af614a7e6ef7a40f5aba106b7cc4c9b28d3cbb Mon Sep 17 00:00:00 2001 From: Darren Fernandes Date: Thu, 12 Nov 2026 12:42:49 +0530 Subject: [PATCH 2/2] Preserve individual declaration sources when merging keyframes --- .../@tailwindcss-postcss/src/index.test.ts | 62 +++++++++++++++++++ .../src/compat/apply-keyframes-to-theme.ts | 23 +++---- .../src/compat/config/resolve-config.ts | 46 ++++++++++---- packages/tailwindcss/src/compat/plugin-api.ts | 22 ++++--- 4 files changed, 119 insertions(+), 34 deletions(-) diff --git a/packages/@tailwindcss-postcss/src/index.test.ts b/packages/@tailwindcss-postcss/src/index.test.ts index ad485abcdbe5..854cf1894a3d 100644 --- a/packages/@tailwindcss-postcss/src/index.test.ts +++ b/packages/@tailwindcss-postcss/src/index.test.ts @@ -320,6 +320,68 @@ test('keyframes from JavaScript configs retain their PostCSS source', async ({ ]) }) +test('merged keyframe declarations retain their individual source files', async ({ + onTestFinished, +}) => { + let dir = await realpath(await mkdtemp(path.join(tmpdir(), 'tw-postcss'))) + onTestFinished(() => rm(dir, { recursive: true, force: true })) + await mkdir(path.join(dir, 'nested')) + + await writeFile( + path.join(dir, 'base.cjs'), + `module.exports = { + theme: { + animation: { fade: 'fade 1s' }, + keyframes: { + fade: { from: { backgroundImage: 'url(./image.svg)', opacity: '0', display: ['block', 'flex'] } }, + }, + }, + }`, + ) + await writeFile( + path.join(dir, 'nested/extension.cjs'), + `module.exports = { + theme: { + extend: { + keyframes: () => ({ + fade: { from: { opacity: '1' }, to: { backgroundImage: 'url(./other.svg)' } }, + }), + }, + }, + }`, + ) + await writeFile(path.join(dir, 'nested/extra.css'), '@config "./extension.cjs";') + + let from = path.join(dir, 'input.css') + let extension = path.join(dir, 'nested/extra.css') + let result = await postcss([tailwindcss({ base: dir, optimize: false })]).process( + css` + @import './nested/extra.css'; + @config './base.cjs'; + @theme { + --color-example: red; + } + @tailwind utilities source(none); + @source inline('animate-fade'); + `, + { from }, + ) + + let declarations: [string, string, string | undefined][] = [] + result.root.walkAtRules('keyframes', (rule) => { + rule.walkDecls((decl) => { + declarations.push([decl.prop, decl.value, decl.source?.input.file]) + }) + }) + expect(declarations).toEqual([ + ['background-image', 'url(./image.svg)', from], + ['opacity', '1', extension], + ['display', 'block', from], + ['display', 'flex', from], + ['background-image', 'url(./other.svg)', extension], + ]) +}) + 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-keyframes-to-theme.ts b/packages/tailwindcss/src/compat/apply-keyframes-to-theme.ts index d083a39a9535..3574f8f80c90 100644 --- a/packages/tailwindcss/src/compat/apply-keyframes-to-theme.ts +++ b/packages/tailwindcss/src/compat/apply-keyframes-to-theme.ts @@ -1,31 +1,28 @@ 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' +import { objectToAst, type CssInJsSources } from './plugin-api' export function applyKeyframesToTheme( designSystem: DesignSystem, resolvedConfig: Pick, - sources?: ReadonlyMap, + sources?: CssInJsSources, ) { - for (let rule of keyframesToRules(resolvedConfig)) { - let src = sources?.get(rule.params) - if (src) { - walk([rule], (node) => { - node.src = src - }) - } + for (let rule of keyframesToRules(resolvedConfig, sources)) { designSystem.theme.addKeyframes(rule) } } -export function keyframesToRules(resolvedConfig: Pick): AtRule[] { +export function keyframesToRules( + resolvedConfig: Pick, + sources?: CssInJsSources, +): AtRule[] { let rules: AtRule[] = [] if ('keyframes' in resolvedConfig.theme) { for (let [name, keyframe] of Object.entries(resolvedConfig.theme.keyframes)) { - rules.push(atRule('@keyframes', name, objectToAst(keyframe as any))) + let rule = atRule('@keyframes', name, objectToAst(keyframe as any, sources)) + rule.src = sources?.get(resolvedConfig.theme.keyframes)?.get(name) + rules.push(rule) } } return rules diff --git a/packages/tailwindcss/src/compat/config/resolve-config.ts b/packages/tailwindcss/src/compat/config/resolve-config.ts index 252e16ebfeb0..3acc932717d9 100644 --- a/packages/tailwindcss/src/compat/config/resolve-config.ts +++ b/packages/tailwindcss/src/compat/config/resolve-config.ts @@ -1,7 +1,7 @@ import type { DesignSystem } from '../../design-system' import type { SourceLocation } from '../../source-maps/source' import colors from '../colors' -import type { PluginWithConfig } from '../plugin-api' +import type { CssInJsSources, PluginWithConfig } from '../plugin-api' import { createThemeFn } from '../plugin-functions' import { deepMerge, isPlainObject } from './deep-merge' import { @@ -50,7 +50,7 @@ export function resolveConfig( ): { resolvedConfig: ResolvedConfig replacedThemeKeys: Set - keyframeSources: Map + keyframeSources: CssInJsSources } { let ctx: ResolutionContext = { design, @@ -202,7 +202,7 @@ function extractConfigs( function mergeTheme(ctx: ResolutionContext) { let replacedThemeKeys: Set = new Set() - let keyframeSources = new Map() + let keyframeSources: CssInJsSources = new WeakMap() let baseKeyframesSource: SourceLocation | undefined let themeFn = createThemeFn(ctx.design, () => ctx.theme, resolveValue) @@ -219,10 +219,31 @@ function mergeTheme(ctx: ResolutionContext) { return value ?? null } - function recordKeyframeSources(keyframes: ResolvedThemeValue, src: SourceLocation | undefined) { - for (let name of Object.keys(keyframes ?? {})) { - keyframeSources.set(name, src) + // Configs can share objects, so clone before associating them with a source. + function recordKeyframeSources(value: any, src: SourceLocation | undefined): any { + if (Array.isArray(value)) { + return value.map((item) => recordKeyframeSources(item, src)) } + if (!isPlainObject(value)) return value + + let copy = Object.fromEntries( + Object.entries(value).map(([key, value]) => [key, recordKeyframeSources(value, src)]), + ) + keyframeSources.set(copy, new Map(Object.keys(copy).map((key) => [key, src]))) + return copy + } + + function mergeKeyframes(a: any, b: any): any { + let merged = mergeThemeExtension(a, b) + if (merged !== undefined) return merged + if (!isPlainObject(a) || !isPlainObject(b)) return b + + let result = deepMerge({}, [a, b], mergeKeyframes) + keyframeSources.set( + result, + new Map([...(keyframeSources.get(a) ?? []), ...(keyframeSources.get(b) ?? [])]), + ) + return result } for (let { config, src } of ctx.configs) { @@ -262,13 +283,12 @@ function mergeTheme(ctx: ResolutionContext) { ] ctx.theme[key] = () => { - let v = values.map(({ value }) => resolveValue(value)) + let v = values.map(({ value, src }) => { + let resolved = resolveValue(value) + return key === 'keyframes' ? recordKeyframeSources(resolved, src) : resolved + }) 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) - } + return v.reduce((a, b) => mergeKeyframes(a, b ?? {}), {}) } let result = deepMerge({}, v, mergeThemeExtension) @@ -280,7 +300,7 @@ function mergeTheme(ctx: ResolutionContext) { let value = resolveValue(ctx.theme[key]) ctx.theme[key] = value if (key === 'keyframes' && !ctx.extend.keyframes) { - recordKeyframeSources(value, baseKeyframesSource) + ctx.theme[key] = recordKeyframeSources(value, baseKeyframesSource) } } diff --git a/packages/tailwindcss/src/compat/plugin-api.ts b/packages/tailwindcss/src/compat/plugin-api.ts index 11878fb304cd..5f6649241a64 100644 --- a/packages/tailwindcss/src/compat/plugin-api.ts +++ b/packages/tailwindcss/src/compat/plugin-api.ts @@ -562,14 +562,20 @@ export function buildPluginApi({ export type CssInJs = { [key: string]: string | string[] | CssInJs | CssInJs[] } -export function objectToAst(rules: CssInJs | CssInJs[]): AstNode[] { +export type CssInJsSources = WeakMap> + +export function objectToAst(rules: CssInJs | CssInJs[], sources?: CssInJsSources): AstNode[] { let ast: AstNode[] = [] rules = Array.isArray(rules) ? rules : [rules] - let entries = rules.flatMap((rule) => Object.entries(rule)) + let entries = rules.flatMap((rule) => + Object.entries(rule).map( + ([name, value]) => [name, value, sources?.get(rule)?.get(name)] as const, + ), + ) - for (let [name, value] of entries) { + for (let [name, value, src] of entries) { if (value === null || value === undefined) continue // @ts-expect-error @@ -580,7 +586,7 @@ export function objectToAst(rules: CssInJs | CssInJs[]): AstNode[] { if (typeof value !== 'object') { if (!name.startsWith('--')) { if (value === '@slot') { - ast.push(rule(name, [atRule('@slot')])) + ast.push({ ...rule(name, [atRule('@slot')]), src }) continue } @@ -589,17 +595,17 @@ export function objectToAst(rules: CssInJs | CssInJs[]): AstNode[] { name = name.replace(/([A-Z])/g, '-$1').toLowerCase() } - ast.push(decl(name, String(value))) + ast.push({ ...decl(name, String(value)), src }) } else if (Array.isArray(value)) { for (let item of value) { if (typeof item === 'string') { - ast.push(decl(name, item)) + ast.push({ ...decl(name, item), src }) } else { - ast.push(rule(name, objectToAst(item))) + ast.push({ ...rule(name, objectToAst(item, sources)), src }) } } } else { - ast.push(rule(name, objectToAst(value))) + ast.push({ ...rule(name, objectToAst(value, sources)), src }) } }