( ast: AstNode[], designSystem: DesignSystem, polyfills: Polyfills = Polyfills.All, )
| 220 | // Optimize the AST for printing where all the special nodes that require custom |
| 221 | // handling are handled such that the printing is a 1-to-1 transformation. |
| 222 | export function optimizeAst( |
| 223 | ast: AstNode[], |
| 224 | designSystem: DesignSystem, |
| 225 | polyfills: Polyfills = Polyfills.All, |
| 226 | ) { |
| 227 | let atRoots: AstNode[] = [] |
| 228 | let seenAtProperties = new Set<string>() |
| 229 | let cssThemeVariables = new DefaultMap<AstNode[], Set<Declaration>>(() => new Set()) |
| 230 | let colorMixDeclarations = new DefaultMap<AstNode[], Set<Declaration>>(() => new Set()) |
| 231 | let keyframes = new Set<AtRule>() |
| 232 | let usedKeyframeNames = new Set() |
| 233 | |
| 234 | let propertyFallbacksRoot: Declaration[] = [] |
| 235 | let propertyFallbacksUniversal: Declaration[] = [] |
| 236 | |
| 237 | let variableDependencies = new DefaultMap<string, Set<string>>(() => new Set()) |
| 238 | |
| 239 | function transform( |
| 240 | node: AstNode, |
| 241 | parent: AstNode[], |
| 242 | context: Record<string, string | boolean> = {}, |
| 243 | depth = 0, |
| 244 | ) { |
| 245 | // Declaration |
| 246 | if (node.kind === 'declaration') { |
| 247 | if (node.property === '--tw-sort' || node.value === undefined || node.value === null) { |
| 248 | return |
| 249 | } |
| 250 | |
| 251 | // Track variables defined in `@theme` |
| 252 | if (context.theme && node.property[0] === '-' && node.property[1] === '-') { |
| 253 | // Variables that resolve to `initial` should never be emitted. This can happen because of |
| 254 | // the `--theme(…)` being used and evaluated lazily |
| 255 | if (node.value === 'initial') { |
| 256 | node.value = undefined |
| 257 | return |
| 258 | } |
| 259 | |
| 260 | if (!context.keyframes) { |
| 261 | cssThemeVariables.get(parent).add(node) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Track used CSS variables |
| 266 | if (node.value.includes('var(')) { |
| 267 | // Declaring another variable does not count as usage. Instead, we mark |
| 268 | // the relationship |
| 269 | if (context.theme && node.property[0] === '-' && node.property[1] === '-') { |
| 270 | for (let variable of extractUsedVariables(node.value)) { |
| 271 | variableDependencies.get(variable).add(node.property) |
| 272 | } |
| 273 | } else { |
| 274 | designSystem.trackUsedVariables(node.value) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // Track used animation names |
| 279 | if (node.property === 'animation') { |
no test coverage detected