Skip to content
Closed
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
3 changes: 3 additions & 0 deletions .changelog/postcss-keyframe-sources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Fix Vite's missing `from` warning when `@tailwindcss/postcss` emits keyframes defined in a JavaScript config
54 changes: 54 additions & 0 deletions packages/@tailwindcss-postcss/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }),
Expand Down
11 changes: 6 additions & 5 deletions packages/tailwindcss/src/compat/apply-compat-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions packages/tailwindcss/src/compat/apply-keyframes-to-theme.ts
Original file line number Diff line number Diff line change
@@ -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<ResolvedConfig, 'theme'>,
sources?: ReadonlyMap<string, SourceLocation | undefined>,
) {
for (let rule of keyframesToRules(resolvedConfig)) {
let src = sources?.get(rule.params)
if (src) {
walk([rule], (node) => {
node.src = src
})
}
designSystem.theme.addKeyframes(rule)
}
}
Expand Down
56 changes: 43 additions & 13 deletions packages/tailwindcss/src/compat/config/resolve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export interface ConfigFile {

interface ResolutionContext {
design: DesignSystem
configs: UserConfig[]
configs: Pick<ConfigFile, 'config' | 'src'>[]
plugins: PluginWithConfig[]
content: ResolvedContentConfig
theme: Record<string, ThemeValue>
extend: Record<string, ThemeValue[]>
extend: Record<string, { value: ThemeValue; src: SourceLocation | undefined }[]>
result: ResolvedConfig
}

Expand All @@ -47,7 +47,11 @@ let minimal: ResolvedConfig = {
export function resolveConfig(
design: DesignSystem,
files: ConfigFile[],
): { resolvedConfig: ResolvedConfig; replacedThemeKeys: Set<string> } {
): {
resolvedConfig: ResolvedConfig
replacedThemeKeys: Set<string>
keyframeSources: Map<string, SourceLocation | undefined>
} {
let ctx: ResolutionContext = {
design,
configs: [],
Expand All @@ -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
}
Expand All @@ -86,7 +90,7 @@ export function resolveConfig(
}

// Merge themes
let replacedThemeKeys = mergeTheme(ctx)
let { replacedThemeKeys, keyframeSources } = mergeTheme(ctx)

return {
resolvedConfig: {
Expand All @@ -96,6 +100,7 @@ export function resolveConfig(
plugins: ctx.plugins,
},
replacedThemeKeys,
keyframeSources,
}
}

Expand Down Expand Up @@ -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<string> {
function mergeTheme(ctx: ResolutionContext) {
let replacedThemeKeys: Set<string> = new Set()
let keyframeSources = new Map<string, SourceLocation | undefined>()
let baseKeyframesSource: SourceLocation | undefined

let themeFn = createThemeFn(ctx.design, () => ctx.theme, resolveValue)
let theme = Object.assign(themeFn, {
Expand All @@ -212,7 +219,13 @@ function mergeTheme(ctx: ResolutionContext): Set<string> {
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 ?? {}

Expand All @@ -226,11 +239,14 @@ function mergeTheme(ctx: ResolutionContext): Set<string> {

// 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 })
}
}

Expand All @@ -240,18 +256,32 @@ function mergeTheme(ctx: ResolutionContext): Set<string> {

// 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
}
}

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
Expand All @@ -269,5 +299,5 @@ function mergeTheme(ctx: ResolutionContext): Set<string> {
}
}

return replacedThemeKeys
return { replacedThemeKeys, keyframeSources }
}