MCPcopy
hub / github.com/tailwindlabs/tailwindcss / optimizeAst

Function optimizeAst

packages/tailwindcss/src/ast.ts:222–683  ·  packages/tailwindcss/src/ast.ts::optimizeAst
(
  ast: AstNode[],
  designSystem: DesignSystem,
  polyfills: Polyfills = Polyfills.All,
)

Source from the content-addressed store, hash-verified

220class="cm">// Optimize the AST for printing where all the special nodes that require custom
221class="cm">// handling are handled such that the printing is a 1-to-1 transformation.
222export 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 class="cm">// Declaration
246 if (node.kind === class="st">'declaration') {
247 if (node.property === class="st">'--tw-sort' || node.value === undefined || node.value === null) {
248 return
249 }
250
251 class="cm">// Track variables defined in `@theme`
252 if (context.theme && node.property[0] === class="st">'-' && node.property[1] === class="st">'-') {
253 class="cm">// Variables that resolve to `initial` should never be emitted. This can happen because of
254 class="cm">// the `--theme(…)` being used and evaluated lazily
255 if (node.value === class="st">'initial') {
256 node.value = undefined
257 return
258 }
259
260 if (!context.keyframes) {
261 cssThemeVariables.get(parent).add(node)
262 }
263 }
264
265 class="cm">// Track used CSS variables
266 if (node.value.includes(class="st">'var(')) {
267 class="cm">// Declaring another variable does not count as usage. Instead, we mark
268 class="cm">// the relationship
269 if (context.theme && node.property[0] === class="st">'-' && node.property[1] === class="st">'-') {
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 class="cm">// Track used animation names
279 if (node.property === class="st">'animation') {

Callers 3

buildFunction · 0.90
ast.test.tsFile · 0.90
candidatesToAstFunction · 0.90

Calls 11

walkFunction · 0.90
isVariableUsedFunction · 0.85
extractKeyframeNamesFunction · 0.85
findNodeFunction · 0.85
ruleFunction · 0.85
atRuleFunction · 0.85
prefixKeyMethod · 0.80
resolveValueMethod · 0.80
transformFunction · 0.70
addMethod · 0.45
hasMethod · 0.45

Tested by

no test coverage detected