Home / Function/ createCanonicalizeCandidateCache() — tailwindcss Function Reference

createCanonicalizeCandidateCache() — tailwindcss Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  045602b5_3428_1ab2_4022_86cb43c8ffe7["createCanonicalizeCandidateCache()"]
  20406e7c_6ee8_4626_dbef_5b37708f4d30["prepareDesignSystemStorage()"]
  20406e7c_6ee8_4626_dbef_5b37708f4d30 -->|calls| 045602b5_3428_1ab2_4022_86cb43c8ffe7
  74157cfe_4a6c_d75a_a5ac_16fa6909752f["parseCandidate()"]
  045602b5_3428_1ab2_4022_86cb43c8ffe7 -->|calls| 74157cfe_4a6c_d75a_a5ac_16fa6909752f
  ec55634f_f6e4_3b8b_1267_0b251c4dade1["printCandidate()"]
  045602b5_3428_1ab2_4022_86cb43c8ffe7 -->|calls| ec55634f_f6e4_3b8b_1267_0b251c4dade1
  49c38785_1012_962e_d353_b5b1d6858132["printVariant()"]
  045602b5_3428_1ab2_4022_86cb43c8ffe7 -->|calls| 49c38785_1012_962e_d353_b5b1d6858132
  4cd99e59_ac1e_2a1f_0946_33cc1afd2532["get()"]
  045602b5_3428_1ab2_4022_86cb43c8ffe7 -->|calls| 4cd99e59_ac1e_2a1f_0946_33cc1afd2532
  style 045602b5_3428_1ab2_4022_86cb43c8ffe7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/canonicalize-candidates.ts lines 420–481

function createCanonicalizeCandidateCache(): DesignSystem['storage'][typeof CANONICALIZE_CANDIDATE_KEY] {
  return new DefaultMap((options: InternalCanonicalizeOptions) => {
    let ds = options.designSystem
    let prefix = ds.theme.prefix ? `${ds.theme.prefix}:` : ''
    let variantCache = ds.storage[CANONICALIZE_VARIANT_KEY].get(options)
    let utilityCache = ds.storage[CANONICALIZE_UTILITY_KEY].get(options)

    return new DefaultMap<string, string>((rawCandidate: string, self) => {
      for (let candidate of ds.parseCandidate(rawCandidate)) {
        let variants = candidate.variants
          .slice()
          .reverse()
          .flatMap((variant) => variantCache.get(variant))
        let important = candidate.important

        // Canonicalize the base candidate (utility), and re-attach the variants
        // and important flag afterwards. This way we can maximize cache hits for
        // the base candidate and each individual variant.
        if (important || variants.length > 0) {
          let canonicalizedUtility = self.get(
            ds.printCandidate({ ...candidate, variants: [], important: false }),
          )

          // Rebuild the final candidate
          let result = canonicalizedUtility

          // Remove the prefix if there are variants, because the variants exist
          // between the prefix and the base candidate.
          if (ds.theme.prefix !== null && variants.length > 0) {
            result = result.slice(prefix.length)
          }

          // Re-attach the variants
          if (variants.length > 0) {
            result = `${variants.map((v) => ds.printVariant(v)).join(':')}:${result}`
          }

          // Re-attach the important flag
          if (important) {
            result += '!'
          }

          // Re-attach the prefix if there were variants
          if (ds.theme.prefix !== null && variants.length > 0) {
            result = `${prefix}${result}`
          }

          return result
        }

        // We are guaranteed to have no variants and no important flag, just the
        // base candidate left to canonicalize.
        let result = utilityCache.get(rawCandidate)
        if (result !== rawCandidate) {
          return result
        }
      }

      return rawCandidate
    })
  })
}

Subdomains

Frequently Asked Questions

What does createCanonicalizeCandidateCache() do?
createCanonicalizeCandidateCache() is a function in the tailwindcss codebase.
What does createCanonicalizeCandidateCache() call?
createCanonicalizeCandidateCache() calls 4 function(s): get, parseCandidate, printCandidate, printVariant.
What calls createCanonicalizeCandidateCache()?
createCanonicalizeCandidateCache() is called by 1 function(s): prepareDesignSystemStorage.

Analyze Your Own Codebase

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

Try Supermodel Free