Skip to content

Commit bdb1778

Browse files
committed
cache tracking of variables
This makes sure that the `optimizeAst` goes from ~11ms back to ~5ms
1 parent 4d5cdf9 commit bdb1778

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

packages/tailwindcss/src/ast.ts

+1-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { parseAtRule } from './css-parser'
22
import type { DesignSystem } from './design-system'
33
import { ThemeOptions } from './theme'
44
import { DefaultMap } from './utils/default-map'
5-
import * as ValueParser from './value-parser'
65

76
const AT_SIGN = 0x40
87

@@ -277,17 +276,7 @@ export function optimizeAst(ast: AstNode[], designSystem: DesignSystem) {
277276

278277
// Track used CSS variables
279278
if (node.value.includes('var(')) {
280-
ValueParser.walk(ValueParser.parse(node.value), (node) => {
281-
if (node.kind !== 'function' || node.value !== 'var') return
282-
283-
ValueParser.walk(node.nodes, (child) => {
284-
if (child.kind !== 'word' || child.value[0] !== '-' || child.value[1] !== '-') return
285-
286-
designSystem.theme.markUsedVariable(child.value)
287-
})
288-
289-
return ValueParser.ValueWalkAction.Skip
290-
})
279+
designSystem.trackUsedVariables(node.value)
291280
}
292281

293282
parent.push(node)

packages/tailwindcss/src/design-system.ts

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getClassOrder } from './sort'
77
import type { Theme, ThemeKey } from './theme'
88
import { Utilities, createUtilities, withAlpha } from './utilities'
99
import { DefaultMap } from './utils/default-map'
10+
import * as ValueParser from './value-parser'
1011
import { Variants, createVariants } from './variants'
1112

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

34+
trackUsedVariables(raw: string): void
35+
3336
// Used by IntelliSense
3437
candidatesToCss(classes: string[]): (string | null)[]
3538
}
@@ -42,6 +45,7 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
4245
let parsedCandidates = new DefaultMap((candidate) =>
4346
Array.from(parseCandidate(candidate, designSystem)),
4447
)
48+
4549
let compiledAstNodes = new DefaultMap<Candidate>((candidate) => {
4650
let ast = compileAstNodes(candidate, designSystem)
4751

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

71+
let trackUsedVariables = new DefaultMap((raw) => {
72+
ValueParser.walk(ValueParser.parse(raw), (node) => {
73+
if (node.kind !== 'function' || node.value !== 'var') return
74+
75+
ValueParser.walk(node.nodes, (child) => {
76+
if (child.kind !== 'word' || child.value[0] !== '-' || child.value[1] !== '-') return
77+
78+
theme.markUsedVariable(child.value)
79+
})
80+
81+
return ValueParser.ValueWalkAction.Skip
82+
})
83+
84+
return true
85+
})
86+
6787
let designSystem: DesignSystem = {
6888
theme,
6989
utilities,
@@ -159,6 +179,10 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
159179

160180
return themeValue
161181
},
182+
183+
trackUsedVariables(raw: string) {
184+
trackUsedVariables.get(raw)
185+
},
162186
}
163187

164188
return designSystem

0 commit comments

Comments
 (0)