optimizeAst() — tailwindcss Function Reference
Architecture documentation for the optimizeAst() function in ast.ts from the tailwindcss codebase.
Entity Profile
Dependency Diagram
graph TD 47b4c875_7e44_6ff9_fb06_16ecf9254223["optimizeAst()"] 42640952_ea63_55f1_1ff1_00816e2980ae["ast.ts"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|defined in| 42640952_ea63_55f1_1ff1_00816e2980ae 9b965fd7_d8e9_0b43_cd5d_c9294ab598ed["buildDesignSystem()"] 9b965fd7_d8e9_0b43_cd5d_c9294ab598ed -->|calls| 47b4c875_7e44_6ff9_fb06_16ecf9254223 ae5a4f96_ffbe_5d6f_324b_4caa358fe1fb["compileAst()"] ae5a4f96_ffbe_5d6f_324b_4caa358fe1fb -->|calls| 47b4c875_7e44_6ff9_fb06_16ecf9254223 06ed9408_12cf_7ddd_a435_8cdd942de1d4["add()"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|calls| 06ed9408_12cf_7ddd_a435_8cdd942de1d4 c74af9b2_8c4e_9508_1d10_dd910b7bf65e["extractUsedVariables()"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|calls| c74af9b2_8c4e_9508_1d10_dd910b7bf65e 6b1d90c6_2845_669d_b953_0aec26b7050b["extractKeyframeNames()"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|calls| 6b1d90c6_2845_669d_b953_0aec26b7050b c203f636_607a_d332_b4c5_6a40c108f778["decl()"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|calls| c203f636_607a_d332_b4c5_6a40c108f778 f6e5c34f_5fff_b7c4_6b4e_54ead4f7899e["isVariableUsed()"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|calls| f6e5c34f_5fff_b7c4_6b4e_54ead4f7899e 21a2c14f_0801_6edc_2695_0f20586060e1["prefixKey()"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|calls| 21a2c14f_0801_6edc_2695_0f20586060e1 5daefc59_7deb_2ccc_ec71_c41b8211de08["findNode()"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|calls| 5daefc59_7deb_2ccc_ec71_c41b8211de08 ed78da58_8727_ad98_120c_61f35cea357a["walk()"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|calls| ed78da58_8727_ad98_120c_61f35cea357a c16f096b_35b2_0695_2909_f85c627abd1e["resolveValue()"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|calls| c16f096b_35b2_0695_2909_f85c627abd1e 2da63033_d079_7b37_5cfb_3877674a70b9["toCss()"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|calls| 2da63033_d079_7b37_5cfb_3877674a70b9 66319c06_7c38_f9ea_4bf0_2a0e18bac1a4["rule()"] 47b4c875_7e44_6ff9_fb06_16ecf9254223 -->|calls| 66319c06_7c38_f9ea_4bf0_2a0e18bac1a4 style 47b4c875_7e44_6ff9_fb06_16ecf9254223 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/tailwindcss/src/ast.ts lines 220–680
export function optimizeAst(
ast: AstNode[],
designSystem: DesignSystem,
polyfills: Polyfills = Polyfills.All,
) {
let atRoots: AstNode[] = []
let seenAtProperties = new Set<string>()
let cssThemeVariables = new DefaultMap<AstNode[], Set<Declaration>>(() => new Set())
let colorMixDeclarations = new DefaultMap<AstNode[], Set<Declaration>>(() => new Set())
let keyframes = new Set<AtRule>()
let usedKeyframeNames = new Set()
let propertyFallbacksRoot: Declaration[] = []
let propertyFallbacksUniversal: Declaration[] = []
let variableDependencies = new DefaultMap<string, Set<string>>(() => new Set())
function transform(
node: AstNode,
parent: AstNode[],
context: Record<string, string | boolean> = {},
depth = 0,
) {
// Declaration
if (node.kind === 'declaration') {
if (node.property === '--tw-sort' || node.value === undefined || node.value === null) {
return
}
// Track variables defined in `@theme`
if (context.theme && node.property[0] === '-' && node.property[1] === '-') {
// Variables that resolve to `initial` should never be emitted. This can happen because of
// the `--theme(…)` being used and evaluated lazily
if (node.value === 'initial') {
node.value = undefined
return
}
if (!context.keyframes) {
cssThemeVariables.get(parent).add(node)
}
}
// Track used CSS variables
if (node.value.includes('var(')) {
// Declaring another variable does not count as usage. Instead, we mark
// the relationship
if (context.theme && node.property[0] === '-' && node.property[1] === '-') {
for (let variable of extractUsedVariables(node.value)) {
variableDependencies.get(variable).add(node.property)
}
} else {
designSystem.trackUsedVariables(node.value)
}
}
// Track used animation names
if (node.property === 'animation') {
for (let keyframeName of extractKeyframeNames(node.value)) {
usedKeyframeNames.add(keyframeName)
}
}
// Create fallback values for usages of the `color-mix(…)` function that reference variables
// found in the theme config.
if (
polyfills & Polyfills.ColorMix &&
node.value.includes('color-mix(') &&
!context.supportsColorMix &&
!context.keyframes
) {
colorMixDeclarations.get(parent).add(node)
}
parent.push(node)
}
// Rule
else if (node.kind === 'rule') {
let nodes: AstNode[] = []
Domain
Subdomains
Defined In
Calls
Called By
Source
Frequently Asked Questions
What does optimizeAst() do?
optimizeAst() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/ast.ts.
Where is optimizeAst() defined?
optimizeAst() is defined in packages/tailwindcss/src/ast.ts at line 220.
What does optimizeAst() call?
optimizeAst() calls 14 function(s): add, atRule, decl, extractKeyframeNames, extractUsedVariables, findNode, get, isVariableUsed, and 6 more.
What calls optimizeAst()?
optimizeAst() is called by 2 function(s): buildDesignSystem, compileAst.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free