Skip to content

Commit 0b58b52

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

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

Diff for: 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

@@ -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)

Diff for: packages/tailwindcss/src/design-system.ts

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

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

33+
trackUsedVariables(raw: string): void
34+
3235
// Used by IntelliSense
3336
candidatesToCss(classes: string[]): (string | null)[]
3437
}
@@ -44,6 +47,21 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
4447
let compiledAstNodes = new DefaultMap<Candidate>((candidate) =>
4548
compileAstNodes(candidate, designSystem),
4649
)
50+
let trackUsedVariables = new DefaultMap((raw) => {
51+
ValueParser.walk(ValueParser.parse(raw), (node) => {
52+
if (node.kind !== 'function' || node.value !== 'var') return
53+
54+
ValueParser.walk(node.nodes, (child) => {
55+
if (child.kind !== 'word' || child.value[0] !== '-' || child.value[1] !== '-') return
56+
57+
theme.markUsedVariable(child.value)
58+
})
59+
60+
return ValueParser.ValueWalkAction.Skip
61+
})
62+
63+
return true
64+
})
4765

4866
let designSystem: DesignSystem = {
4967
theme,
@@ -140,6 +158,10 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
140158

141159
return themeValue
142160
},
161+
162+
trackUsedVariables(raw: string) {
163+
trackUsedVariables.get(raw)
164+
},
143165
}
144166

145167
return designSystem

0 commit comments

Comments
 (0)