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
  e5cfde3f_0bc0_3155_68b2_7635f9cbb320["createCanonicalizeCandidateCache()"]
  7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e["canonicalize-candidates.ts"]
  e5cfde3f_0bc0_3155_68b2_7635f9cbb320 -->|defined in| 7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e
  81ec8f98_1fb2_d7c3_dd65_3a9e51b3be39["prepareDesignSystemStorage()"]
  81ec8f98_1fb2_d7c3_dd65_3a9e51b3be39 -->|calls| e5cfde3f_0bc0_3155_68b2_7635f9cbb320
  ef9ad758_33f4_0d23_fff8_8eeaf71d00d8["parseCandidate()"]
  e5cfde3f_0bc0_3155_68b2_7635f9cbb320 -->|calls| ef9ad758_33f4_0d23_fff8_8eeaf71d00d8
  87ba7eab_3a52_d53e_dfd4_e507e9763b55["printCandidate()"]
  e5cfde3f_0bc0_3155_68b2_7635f9cbb320 -->|calls| 87ba7eab_3a52_d53e_dfd4_e507e9763b55
  02a5a469_a54f_7532_8b33_d407c3c7f34a["printVariant()"]
  e5cfde3f_0bc0_3155_68b2_7635f9cbb320 -->|calls| 02a5a469_a54f_7532_8b33_d407c3c7f34a
  5bcf4886_1230_a8ff_7302_a26cc5a9a525["get()"]
  e5cfde3f_0bc0_3155_68b2_7635f9cbb320 -->|calls| 5bcf4886_1230_a8ff_7302_a26cc5a9a525
  style e5cfde3f_0bc0_3155_68b2_7635f9cbb320 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, defined in packages/tailwindcss/src/canonicalize-candidates.ts.
Where is createCanonicalizeCandidateCache() defined?
createCanonicalizeCandidateCache() is defined in packages/tailwindcss/src/canonicalize-candidates.ts at line 420.
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