Skip to content

Commit a2dd966

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

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

packages/tailwindcss/src/ast.ts

Lines changed: 1 addition & 12 deletions
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

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

263262
// Track used CSS variables
264263
if (node.value.includes('var(')) {
265-
ValueParser.walk(ValueParser.parse(node.value), (node) => {
266-
if (node.kind !== 'function' || node.value !== 'var') return
267-
268-
ValueParser.walk(node.nodes, (child) => {
269-
if (child.kind !== 'word' || child.value[0] !== '-' || child.value[1] !== '-') return
270-
271-
designSystem.theme.markUsedVariable(child.value)
272-
})
273-
274-
return ValueParser.ValueWalkAction.Skip
275-
})
264+
designSystem.trackUsedVariables(node.value)
276265
}
277266

278267
parent.push(node)

packages/tailwindcss/src/design-system.ts

Lines changed: 24 additions & 0 deletions
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)