Home / Function/ createCanonicalizeCandidateCache() — tailwindcss Function Reference

createCanonicalizeCandidateCache() — tailwindcss Function Reference

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

Function typescript Oxide Extractor calls 4 called by 1

Entity Profile

Dependency Diagram

graph TD
  8678786b_ce4d_e620_10b5_0449d39487fc["createCanonicalizeCandidateCache()"]
  f6c14bbb_2e42_58cc_18f1_c89a243da9c0["canonicalize-candidates.ts"]
  8678786b_ce4d_e620_10b5_0449d39487fc -->|defined in| f6c14bbb_2e42_58cc_18f1_c89a243da9c0
  26169039_1fd2_e320_1f63_11ec10a6fc52["prepareDesignSystemStorage()"]
  26169039_1fd2_e320_1f63_11ec10a6fc52 -->|calls| 8678786b_ce4d_e620_10b5_0449d39487fc
  bafed062_abf8_7e47_4942_385b21bd9b04["parseCandidate()"]
  8678786b_ce4d_e620_10b5_0449d39487fc -->|calls| bafed062_abf8_7e47_4942_385b21bd9b04
  2de86ba2_90a4_8c2d_db18_154bb1a1564f["printCandidate()"]
  8678786b_ce4d_e620_10b5_0449d39487fc -->|calls| 2de86ba2_90a4_8c2d_db18_154bb1a1564f
  fe10eff5_7df3_1553_1351_7802a9ea48a5["printVariant()"]
  8678786b_ce4d_e620_10b5_0449d39487fc -->|calls| fe10eff5_7df3_1553_1351_7802a9ea48a5
  2820372c_b982_9e06_fc23_f8f4ac308d00["get()"]
  8678786b_ce4d_e620_10b5_0449d39487fc -->|calls| 2820372c_b982_9e06_fc23_f8f4ac308d00
  style 8678786b_ce4d_e620_10b5_0449d39487fc 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
    })
  })
}

Domain

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