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
  6a3a8ab4_d53c_7516_c736_663c060fe979["compileAst()"]
  214d4226_762c_0b6a_7c0b_f917da98dda0["compile()"]
  214d4226_762c_0b6a_7c0b_f917da98dda0 -->|calls| 6a3a8ab4_d53c_7516_c736_663c060fe979
  26086ff1_0d4f_fdb2_3fc4_d0c999f90a8c["parseCss()"]
  6a3a8ab4_d53c_7516_c736_663c060fe979 -->|calls| 26086ff1_0d4f_fdb2_3fc4_d0c999f90a8c
  dbaac8a3_b27b_2942_9a4d_7e9063a9649d["comment()"]
  6a3a8ab4_d53c_7516_c736_663c060fe979 -->|calls| dbaac8a3_b27b_2942_9a4d_7e9063a9649d
  3ba19013_498f_3c9b_5c44_0eb24efc4394["add()"]
  6a3a8ab4_d53c_7516_c736_663c060fe979 -->|calls| 3ba19013_498f_3c9b_5c44_0eb24efc4394
  ec867cf3_916b_0d16_65ec_c715e69fee03["optimizeAst()"]
  6a3a8ab4_d53c_7516_c736_663c060fe979 -->|calls| ec867cf3_916b_0d16_65ec_c715e69fee03
  f4b2481e_23ea_2f5c_fdf6_9834ac412e37["markUsedVariable()"]
  6a3a8ab4_d53c_7516_c736_663c060fe979 -->|calls| f4b2481e_23ea_2f5c_fdf6_9834ac412e37
  cbdeac33_581c_3396_0f10_877935a68176["compileCandidates()"]
  6a3a8ab4_d53c_7516_c736_663c060fe979 -->|calls| cbdeac33_581c_3396_0f10_877935a68176
  e9d556bc_f22d_356c_1bd2_27442c34b5c7["walk()"]
  6a3a8ab4_d53c_7516_c736_663c060fe979 -->|calls| e9d556bc_f22d_356c_1bd2_27442c34b5c7
  c53948a8_17fd_19f4_c907_0e43b245f8e4["has()"]
  6a3a8ab4_d53c_7516_c736_663c060fe979 -->|calls| c53948a8_17fd_19f4_c907_0e43b245f8e4
  style 6a3a8ab4_d53c_7516_c736_663c060fe979 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
    },
  }
}

Subdomains

Called By

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 1 function(s): compile.

Analyze Your Own Codebase

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

Try Supermodel Free