Home / Function/ collapseCandidates() — tailwindcss Function Reference

collapseCandidates() — tailwindcss Function Reference

Architecture documentation for the collapseCandidates() function in canonicalize-candidates.ts from the tailwindcss codebase.

Function typescript Oxide Extractor calls 8 called by 1

Entity Profile

Dependency Diagram

graph TD
  fd4a6387_6427_eb54_6f14_abd89a3baf61["collapseCandidates()"]
  f6c14bbb_2e42_58cc_18f1_c89a243da9c0["canonicalize-candidates.ts"]
  fd4a6387_6427_eb54_6f14_abd89a3baf61 -->|defined in| f6c14bbb_2e42_58cc_18f1_c89a243da9c0
  fc54d77f_8791_78d0_76a3_105cb9c395cf["canonicalizeCandidates()"]
  fc54d77f_8791_78d0_76a3_105cb9c395cf -->|calls| fd4a6387_6427_eb54_6f14_abd89a3baf61
  c58cbb33_f3cc_0b4f_844a_15bf66a1dc61["segment()"]
  fd4a6387_6427_eb54_6f14_abd89a3baf61 -->|calls| c58cbb33_f3cc_0b4f_844a_15bf66a1dc61
  e7db6358_7af5_e4b2_792d_749691a304cc["add()"]
  fd4a6387_6427_eb54_6f14_abd89a3baf61 -->|calls| e7db6358_7af5_e4b2_792d_749691a304cc
  333e8e98_491a_3493_516c_169c024de8d8["entries()"]
  fd4a6387_6427_eb54_6f14_abd89a3baf61 -->|calls| 333e8e98_491a_3493_516c_169c024de8d8
  e92f4f42_1861_fe27_1d37_feb4a5f1e9ad["keysInNamespaces()"]
  fd4a6387_6427_eb54_6f14_abd89a3baf61 -->|calls| e92f4f42_1861_fe27_1d37_feb4a5f1e9ad
  09df19e7_0346_145d_40c4_e93ff14a2ff9["isValidSpacingMultiplier()"]
  fd4a6387_6427_eb54_6f14_abd89a3baf61 -->|calls| 09df19e7_0346_145d_40c4_e93ff14a2ff9
  70636fc3_eb40_39dc_a728_9f5b388db833["intersection()"]
  fd4a6387_6427_eb54_6f14_abd89a3baf61 -->|calls| 70636fc3_eb40_39dc_a728_9f5b388db833
  15ad3cb4_825a_2476_41b2_37c2b18d4b48["combinations()"]
  fd4a6387_6427_eb54_6f14_abd89a3baf61 -->|calls| 15ad3cb4_825a_2476_41b2_37c2b18d4b48
  2820372c_b982_9e06_fc23_f8f4ac308d00["get()"]
  fd4a6387_6427_eb54_6f14_abd89a3baf61 -->|calls| 2820372c_b982_9e06_fc23_f8f4ac308d00
  style fd4a6387_6427_eb54_6f14_abd89a3baf61 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/canonicalize-candidates.ts lines 200–417

function collapseCandidates(options: InternalCanonicalizeOptions, candidates: string[]): string[] {
  if (candidates.length <= 1) return candidates
  let designSystem = options.designSystem

  // To keep things simple, we group candidates such that we only collapse
  // candidates with the same variants and important modifier together.
  let groups = new DefaultMap((_before: string) => {
    return new DefaultMap((_after: string) => {
      return new Set<string>()
    })
  })

  let prefix = options.designSystem.theme.prefix ? `${options.designSystem.theme.prefix}:` : ''

  for (let candidate of candidates) {
    let variants = segment(candidate, ':')
    let utility = variants.pop()!

    let important = utility.endsWith('!')
    if (important) {
      utility = utility.slice(0, -1)
    }

    let before = variants.length > 0 ? `${variants.join(':')}:` : ''
    let after = important ? '!' : ''

    // Group by variants and important flag
    groups.get(before).get(after).add(`${prefix}${utility}`)
  }

  let result = new Set<string>()
  for (let [before, group] of groups.entries()) {
    for (let [after, candidates] of group.entries()) {
      for (let candidate of collapseGroup(Array.from(candidates))) {
        // Drop the prefix if we had one, because the prefix is already there as
        // part of the variants.
        if (prefix && candidate.startsWith(prefix)) {
          candidate = candidate.slice(prefix.length)
        }

        result.add(`${before}${candidate}${after}`)
      }
    }
  }

  return Array.from(result)

  function collapseGroup(candidates: string[]) {
    let signatureOptions = options.signatureOptions
    let computeUtilitiesPropertiesLookup =
      designSystem.storage[UTILITY_PROPERTIES_KEY].get(signatureOptions)
    let staticUtilities = designSystem.storage[STATIC_UTILITIES_KEY].get(signatureOptions)

    // For each candidate, compute the used properties and values. E.g.: `mt-1` → `margin-top` → `0.25rem`
    //
    // NOTE: Currently assuming we are dealing with static utilities only. This
    // will change the moment we have `@utility` for most built-ins.
    let candidatePropertiesValues = candidates.map((candidate) =>
      computeUtilitiesPropertiesLookup.get(candidate),
    )

    // Hard-coded optimization: if any candidate sets `line-height` and another
    // candidate sets `font-size`, we pre-compute the `text-*` utilities with
    // this line-height to try and collapse to those combined values.
    if (candidatePropertiesValues.some((x) => x.has('line-height'))) {
      let fontSizeNames = designSystem.theme.keysInNamespaces(['--text'])
      if (fontSizeNames.length > 0) {
        let interestingLineHeights = new Set<string | number>()
        let seenLineHeights = new Set<string>()
        for (let pairs of candidatePropertiesValues) {
          for (let lineHeight of pairs.get('line-height')) {
            if (seenLineHeights.has(lineHeight)) continue
            seenLineHeights.add(lineHeight)

            let bareValue = designSystem.storage[SPACING_KEY]?.get(lineHeight) ?? null
            if (bareValue !== null) {
              if (isValidSpacingMultiplier(bareValue)) {
                interestingLineHeights.add(bareValue)

                for (let name of fontSizeNames) {
                  computeUtilitiesPropertiesLookup.get(`text-${name}/${bareValue}`)

Domain

Subdomains

Frequently Asked Questions

What does collapseCandidates() do?
collapseCandidates() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/canonicalize-candidates.ts.
Where is collapseCandidates() defined?
collapseCandidates() is defined in packages/tailwindcss/src/canonicalize-candidates.ts at line 200.
What does collapseCandidates() call?
collapseCandidates() calls 8 function(s): add, combinations, entries, get, intersection, isValidSpacingMultiplier, keysInNamespaces, segment.
What calls collapseCandidates()?
collapseCandidates() is called by 1 function(s): canonicalizeCandidates.

Analyze Your Own Codebase

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

Try Supermodel Free