From 3143eb6e7ad176470ebf9e1387951aaebfc8b83c Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Wed, 16 Apr 2025 17:26:37 -0400 Subject: [PATCH] Track theme variables --- packages/tailwindcss/src/index.ts | 4 +++- packages/tailwindcss/src/theme.ts | 15 +++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/tailwindcss/src/index.ts b/packages/tailwindcss/src/index.ts index 6adb8fffc..2e1968d40 100644 --- a/packages/tailwindcss/src/index.ts +++ b/packages/tailwindcss/src/index.ts @@ -541,7 +541,7 @@ async function parseCss( if (child.kind === 'comment') return if (child.kind === 'declaration' && child.property.startsWith('--')) { - theme.add(unescape(child.property), child.value ?? '', themeOptions) + theme.add(unescape(child.property), child.value ?? '', themeOptions, child.src) return } @@ -559,6 +559,7 @@ async function parseCss( // theme later, and delete any other `@theme` rules. if (!firstThemeRule) { firstThemeRule = styleRule(':root, :host', []) + firstThemeRule.src = node.src replaceWith([firstThemeRule]) } else { replaceWith([]) @@ -607,6 +608,7 @@ async function parseCss( for (let [key, value] of designSystem.theme.entries()) { if (value.options & ThemeOptions.REFERENCE) continue let node = decl(escape(key), value.value) + node.src = value.src nodes.push(node) } diff --git a/packages/tailwindcss/src/theme.ts b/packages/tailwindcss/src/theme.ts index 74375b6f7..2928afd3b 100644 --- a/packages/tailwindcss/src/theme.ts +++ b/packages/tailwindcss/src/theme.ts @@ -1,4 +1,4 @@ -import { type AtRule } from './ast' +import { type AtRule, type Declaration } from './ast' import { escape, unescape } from './utils/escape' export const enum ThemeOptions { @@ -40,11 +40,18 @@ export class Theme { public prefix: string | null = null constructor( - private values = new Map(), + private values = new Map< + string, + { + value: string + options: ThemeOptions + src: Declaration['src'] + } + >(), private keyframes = new Set([]), ) {} - add(key: string, value: string, options = ThemeOptions.NONE): void { + add(key: string, value: string, options = ThemeOptions.NONE, src?: Declaration['src']): void { if (key.endsWith('-*')) { if (value !== 'initial') { throw new Error(`Invalid theme value \`${value}\` for namespace \`${key}\``) @@ -68,7 +75,7 @@ export class Theme { if (value === 'initial') { this.values.delete(key) } else { - this.values.set(key, { value, options }) + this.values.set(key, { value, options, src }) } }