From a2dd966d0b42d2972bd8d2d2dc649b6c1ef472c6 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 5 Feb 2025 17:25:10 +0100 Subject: [PATCH] cache tracking of variables This makes sure that the `optimizeAst` goes from ~11ms back to ~5ms --- packages/tailwindcss/src/ast.ts | 13 +----------- packages/tailwindcss/src/design-system.ts | 24 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/tailwindcss/src/ast.ts b/packages/tailwindcss/src/ast.ts index 7920057d4ac5..be862d5b4f19 100644 --- a/packages/tailwindcss/src/ast.ts +++ b/packages/tailwindcss/src/ast.ts @@ -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 @@ -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) diff --git a/packages/tailwindcss/src/design-system.ts b/packages/tailwindcss/src/design-system.ts index fdd5581c94be..cfa02beaf366 100644 --- a/packages/tailwindcss/src/design-system.ts +++ b/packages/tailwindcss/src/design-system.ts @@ -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 = { @@ -30,6 +31,8 @@ export type DesignSystem = { getVariantOrder(): Map resolveThemeValue(path: string): string | undefined + trackUsedVariables(raw: string): void + // Used by IntelliSense candidatesToCss(classes: string[]): (string | null)[] } @@ -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) => { let ast = compileAstNodes(candidate, designSystem) @@ -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, @@ -159,6 +179,10 @@ export function buildDesignSystem(theme: Theme): DesignSystem { return themeValue }, + + trackUsedVariables(raw: string) { + trackUsedVariables.get(raw) + }, } return designSystem