Home / Function/ compileCandidates() — tailwindcss Function Reference

compileCandidates() — tailwindcss Function Reference

Architecture documentation for the compileCandidates() function in compile.ts from the tailwindcss codebase.

Function typescript OxideCore Scanner calls 6 called by 4

Entity Profile

Dependency Diagram

graph TD
  19c89ebd_512f_276a_754b_c043c41d7bd4["compileCandidates()"]
  556f6ab2_af7d_ed90_84f0_6b49e632571a["substituteAtApply()"]
  556f6ab2_af7d_ed90_84f0_6b49e632571a -->|calls| 19c89ebd_512f_276a_754b_c043c41d7bd4
  c58d3214_88d6_f4fc_257f_8e84def5b24f["buildDesignSystem()"]
  c58d3214_88d6_f4fc_257f_8e84def5b24f -->|calls| 19c89ebd_512f_276a_754b_c043c41d7bd4
  f5e443d2_a934_36af_23f2_b1c002aaa585["compileAst()"]
  f5e443d2_a934_36af_23f2_b1c002aaa585 -->|calls| 19c89ebd_512f_276a_754b_c043c41d7bd4
  781cdb66_7551_475b_1463_fec48331a096["getClassOrder()"]
  781cdb66_7551_475b_1463_fec48331a096 -->|calls| 19c89ebd_512f_276a_754b_c043c41d7bd4
  0bec5ca9_74c8_dcc7_ec12_6404fb6493bd["parseCandidate()"]
  19c89ebd_512f_276a_754b_c043c41d7bd4 -->|calls| 0bec5ca9_74c8_dcc7_ec12_6404fb6493bd
  34278c80_0fb5_dbdf_08de_b896b9703f2c["set()"]
  19c89ebd_512f_276a_754b_c043c41d7bd4 -->|calls| 34278c80_0fb5_dbdf_08de_b896b9703f2c
  70b7448d_fd76_0d4b_e635_18f49f8ba1d6["compileAstNodes()"]
  19c89ebd_512f_276a_754b_c043c41d7bd4 -->|calls| 70b7448d_fd76_0d4b_e635_18f49f8ba1d6
  ce3ada3e_5086_2254_5fa3_7039035fce28["has()"]
  19c89ebd_512f_276a_754b_c043c41d7bd4 -->|calls| ce3ada3e_5086_2254_5fa3_7039035fce28
  143002c2_0d0e_8f60_a309_097512cbff95["get()"]
  19c89ebd_512f_276a_754b_c043c41d7bd4 -->|calls| 143002c2_0d0e_8f60_a309_097512cbff95
  2554e693_690d_f3c8_02f3_a6124d291bbd["compare()"]
  19c89ebd_512f_276a_754b_c043c41d7bd4 -->|calls| 2554e693_690d_f3c8_02f3_a6124d291bbd
  style 19c89ebd_512f_276a_754b_c043c41d7bd4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/compile.ts lines 11–121

export function compileCandidates(
  rawCandidates: Iterable<string>,
  designSystem: DesignSystem,
  {
    onInvalidCandidate,
    respectImportant,
  }: { onInvalidCandidate?: (candidate: string) => void; respectImportant?: boolean } = {},
) {
  let nodeSorting = new Map<
    AstNode,
    { properties: { order: number[]; count: number }; variants: bigint; candidate: string }
  >()
  let astNodes: AstNode[] = []
  let matches = new Map<string, Candidate[]>()

  // Parse candidates and variants
  for (let rawCandidate of rawCandidates) {
    if (designSystem.invalidCandidates.has(rawCandidate)) {
      onInvalidCandidate?.(rawCandidate)
      continue // Bail, invalid candidate
    }

    let candidates = designSystem.parseCandidate(rawCandidate)
    if (candidates.length === 0) {
      onInvalidCandidate?.(rawCandidate)
      continue // Bail, invalid candidate
    }

    matches.set(rawCandidate, candidates)
  }

  let flags = CompileAstFlags.None

  if (respectImportant ?? true) {
    flags |= CompileAstFlags.RespectImportant
  }

  let variantOrderMap = designSystem.getVariantOrder()

  // Create the AST
  for (let [rawCandidate, candidates] of matches) {
    let found = false

    for (let candidate of candidates) {
      let rules = designSystem.compileAstNodes(candidate, flags)
      if (rules.length === 0) continue

      found = true

      for (let { node, propertySort } of rules) {
        // Track the variant order which is a number with each bit representing a
        // variant. This allows us to sort the rules based on the order of
        // variants used.
        let variantOrder = 0n
        for (let variant of candidate.variants) {
          variantOrder |= 1n << BigInt(variantOrderMap.get(variant)!)
        }

        nodeSorting.set(node, {
          properties: propertySort,
          variants: variantOrder,
          candidate: rawCandidate,
        })
        astNodes.push(node)
      }
    }

    if (!found) {
      onInvalidCandidate?.(rawCandidate)
    }
  }

  astNodes.sort((a, z) => {
    // SAFETY: At this point it is safe to use TypeScript's non-null assertion
    // operator because if the ast nodes didn't exist, we introduced a bug
    // above, but there is no need to re-check just to be sure. If this relied
    // on pure user input, then we would need to check for its existence.
    let aSorting = nodeSorting.get(a)!
    let zSorting = nodeSorting.get(z)!

    // Sort by variant order first
    if (aSorting.variants - zSorting.variants !== 0n) {
      return Number(aSorting.variants - zSorting.variants)
    }

    // Find the first property that is different between the two rules
    let offset = 0
    while (
      offset < aSorting.properties.order.length &&
      offset < zSorting.properties.order.length &&
      aSorting.properties.order[offset] === zSorting.properties.order[offset]
    ) {
      offset += 1
    }

    return (
      // Sort by lowest property index first
      (aSorting.properties.order[offset] ?? Infinity) -
        (zSorting.properties.order[offset] ?? Infinity) ||
      // Sort by most properties first, then by least properties
      zSorting.properties.count - aSorting.properties.count ||
      // Sort alphabetically
      compare(aSorting.candidate, zSorting.candidate)
    )
  })

  return {
    astNodes,
    nodeSorting,
  }
}

Domain

Subdomains

Frequently Asked Questions

What does compileCandidates() do?
compileCandidates() is a function in the tailwindcss codebase.
What does compileCandidates() call?
compileCandidates() calls 6 function(s): compare, compileAstNodes, get, has, parseCandidate, set.
What calls compileCandidates()?
compileCandidates() is called by 4 function(s): buildDesignSystem, compileAst, getClassOrder, substituteAtApply.

Analyze Your Own Codebase

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

Try Supermodel Free