Home / Function/ compileAst() — tailwindcss Function Reference

compileAst() — tailwindcss Function Reference

Architecture documentation for the compileAst() function in index.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  f5e443d2_a934_36af_23f2_b1c002aaa585["compileAst()"]
  4df9cc7a_3b26_a27d_dd17_823de0561e69["compileAst()"]
  4df9cc7a_3b26_a27d_dd17_823de0561e69 -->|calls| f5e443d2_a934_36af_23f2_b1c002aaa585
  38bfe51e_e2b6_fcde_5ec2_9e78b758e748["compile()"]
  38bfe51e_e2b6_fcde_5ec2_9e78b758e748 -->|calls| f5e443d2_a934_36af_23f2_b1c002aaa585
  95cb326e_6b59_0903_0c96_d221fca5c2b1["parseCss()"]
  f5e443d2_a934_36af_23f2_b1c002aaa585 -->|calls| 95cb326e_6b59_0903_0c96_d221fca5c2b1
  f4c1885c_39a0_4343_f87c_7a8b1145a3bc["comment()"]
  f5e443d2_a934_36af_23f2_b1c002aaa585 -->|calls| f4c1885c_39a0_4343_f87c_7a8b1145a3bc
  24d95be4_356f_a1f9_9702_2a4f413db0f5["add()"]
  f5e443d2_a934_36af_23f2_b1c002aaa585 -->|calls| 24d95be4_356f_a1f9_9702_2a4f413db0f5
  a6e11c3d_c962_0a65_d91f_6fbe955cf4f0["optimizeAst()"]
  f5e443d2_a934_36af_23f2_b1c002aaa585 -->|calls| a6e11c3d_c962_0a65_d91f_6fbe955cf4f0
  7b01a3a8_8970_30d2_152f_715f03247ba2["markUsedVariable()"]
  f5e443d2_a934_36af_23f2_b1c002aaa585 -->|calls| 7b01a3a8_8970_30d2_152f_715f03247ba2
  19c89ebd_512f_276a_754b_c043c41d7bd4["compileCandidates()"]
  f5e443d2_a934_36af_23f2_b1c002aaa585 -->|calls| 19c89ebd_512f_276a_754b_c043c41d7bd4
  a32bba76_f60d_883f_1ff1_276a0bb9db9f["walk()"]
  f5e443d2_a934_36af_23f2_b1c002aaa585 -->|calls| a32bba76_f60d_883f_1ff1_276a0bb9db9f
  bd2fd3ca_e76d_ae00_2925_6f523cc4a134["has()"]
  f5e443d2_a934_36af_23f2_b1c002aaa585 -->|calls| bd2fd3ca_e76d_ae00_2925_6f523cc4a134
  style f5e443d2_a934_36af_23f2_b1c002aaa585 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/index.ts lines 709–815

export async function compileAst(
  input: AstNode[],
  opts: CompileOptions = {},
): Promise<{
  sources: { base: string; pattern: string; negated: boolean }[]
  root: Root
  features: Features
  build(candidates: string[]): AstNode[]
}> {
  let { designSystem, ast, sources, root, utilitiesNode, features, inlineCandidates } =
    await parseCss(input, opts)

  if (process.env.NODE_ENV !== 'test') {
    ast.unshift(comment(`! tailwindcss v${version} | MIT License | https://tailwindcss.com `))
  }

  // Track all invalid candidates
  function onInvalidCandidate(candidate: string) {
    designSystem.invalidCandidates.add(candidate)
  }

  // Track all valid candidates, these are the incoming `rawCandidate` that
  // resulted in a generated AST Node. All the other `rawCandidates` are invalid
  // and should be ignored.
  let allValidCandidates = new Set<string>()
  let compiled = null as AstNode[] | null
  let previousAstNodeCount = 0
  let defaultDidChange = false

  for (let candidate of inlineCandidates) {
    if (!designSystem.invalidCandidates.has(candidate)) {
      allValidCandidates.add(candidate)
      defaultDidChange = true
    }
  }

  return {
    sources,
    root,
    features,
    build(newRawCandidates: string[]) {
      if (features === Features.None) {
        return input
      }

      if (!utilitiesNode) {
        compiled ??= optimizeAst(ast, designSystem, opts.polyfills)
        return compiled
      }

      let didChange = defaultDidChange
      let didAddExternalVariable = false
      defaultDidChange = false

      // Add all new candidates unless we know that they are invalid.
      let prevSize = allValidCandidates.size
      for (let candidate of newRawCandidates) {
        if (!designSystem.invalidCandidates.has(candidate)) {
          if (candidate[0] === '-' && candidate[1] === '-') {
            let didMarkVariableAsUsed = designSystem.theme.markUsedVariable(candidate)
            didChange ||= didMarkVariableAsUsed
            didAddExternalVariable ||= didMarkVariableAsUsed
          } else {
            allValidCandidates.add(candidate)
            didChange ||= allValidCandidates.size !== prevSize
          }
        }
      }

      // If no new candidates were added, we can return the original CSS. This
      // currently assumes that we only add new candidates and never remove any.
      if (!didChange) {
        compiled ??= optimizeAst(ast, designSystem, opts.polyfills)
        return compiled
      }

      let newNodes = compileCandidates(allValidCandidates, designSystem, {
        onInvalidCandidate,
      }).astNodes

      if (opts.from) {
        walk(newNodes, (node) => {
          // We do this conditionally to preserve source locations from both
          // `@utility` and `@custom-variant`. Even though generated nodes are
          // cached this should be fine because `utilitiesNode.src` should not
          // change without a full rebuild which destroys the cache.
          node.src ??= utilitiesNode.src
        })
      }

      // If no new ast nodes were generated, then we can return the original
      // CSS. This currently assumes that we only add new ast nodes and never
      // remove any.
      if (!didAddExternalVariable && previousAstNodeCount === newNodes.length) {
        compiled ??= optimizeAst(ast, designSystem, opts.polyfills)
        return compiled
      }

      previousAstNodeCount = newNodes.length

      utilitiesNode.nodes = newNodes

      compiled = optimizeAst(ast, designSystem, opts.polyfills)
      return compiled
    },
  }
}

Domain

Subdomains

Frequently Asked Questions

What does compileAst() do?
compileAst() is a function in the tailwindcss codebase.
What does compileAst() call?
compileAst() calls 8 function(s): add, comment, compileCandidates, has, markUsedVariable, optimizeAst, parseCss, walk.
What calls compileAst()?
compileAst() is called by 2 function(s): compile, compileAst.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free