Skip to content

Commit

Permalink
cache tracking of variables
Browse files Browse the repository at this point in the history
This makes sure that the `optimizeAst` goes from ~11ms back to ~5ms
  • Loading branch information
RobinMalfait committed Feb 5, 2025
1 parent ca85ab0 commit a2dd966
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
13 changes: 1 addition & 12 deletions packages/tailwindcss/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { parseAtRule } from './css-parser'
import type { DesignSystem } from './design-system'
import { ThemeOptions } from './theme'
import { DefaultMap } from './utils/default-map'
import * as ValueParser from './value-parser'

const AT_SIGN = 0x40

Expand Down Expand Up @@ -262,17 +261,7 @@ export function optimizeAst(ast: AstNode[], designSystem: DesignSystem) {

// Track used CSS variables
if (node.value.includes('var(')) {
ValueParser.walk(ValueParser.parse(node.value), (node) => {
if (node.kind !== 'function' || node.value !== 'var') return

ValueParser.walk(node.nodes, (child) => {
if (child.kind !== 'word' || child.value[0] !== '-' || child.value[1] !== '-') return

designSystem.theme.markUsedVariable(child.value)
})

return ValueParser.ValueWalkAction.Skip
})
designSystem.trackUsedVariables(node.value)
}

parent.push(node)
Expand Down
24 changes: 24 additions & 0 deletions packages/tailwindcss/src/design-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getClassOrder } from './sort'
import type { Theme, ThemeKey } from './theme'
import { Utilities, createUtilities, withAlpha } from './utilities'
import { DefaultMap } from './utils/default-map'
import * as ValueParser from './value-parser'
import { Variants, createVariants } from './variants'

export type DesignSystem = {
Expand All @@ -30,6 +31,8 @@ export type DesignSystem = {
getVariantOrder(): Map<Variant, number>
resolveThemeValue(path: string): string | undefined

trackUsedVariables(raw: string): void

// Used by IntelliSense
candidatesToCss(classes: string[]): (string | null)[]
}
Expand All @@ -42,6 +45,7 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
let parsedCandidates = new DefaultMap((candidate) =>
Array.from(parseCandidate(candidate, designSystem)),
)

let compiledAstNodes = new DefaultMap<Candidate>((candidate) => {
let ast = compileAstNodes(candidate, designSystem)

Expand All @@ -64,6 +68,22 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
return ast
})

let trackUsedVariables = new DefaultMap((raw) => {
ValueParser.walk(ValueParser.parse(raw), (node) => {
if (node.kind !== 'function' || node.value !== 'var') return

ValueParser.walk(node.nodes, (child) => {
if (child.kind !== 'word' || child.value[0] !== '-' || child.value[1] !== '-') return

theme.markUsedVariable(child.value)
})

return ValueParser.ValueWalkAction.Skip
})

return true
})

let designSystem: DesignSystem = {
theme,
utilities,
Expand Down Expand Up @@ -159,6 +179,10 @@ export function buildDesignSystem(theme: Theme): DesignSystem {

return themeValue
},

trackUsedVariables(raw: string) {
trackUsedVariables.get(raw)
},
}

return designSystem
Expand Down

0 comments on commit a2dd966

Please sign in to comment.