Home / Function/ createVariantSignatureCache() — tailwindcss Function Reference

createVariantSignatureCache() — tailwindcss Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  5db4b61f_a5d4_16b1_b2ec_707bde46324a["createVariantSignatureCache()"]
  7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e["canonicalize-candidates.ts"]
  5db4b61f_a5d4_16b1_b2ec_707bde46324a -->|defined in| 7d350d81_5de1_f9f3_5b2c_19ec8fd3c37e
  81ec8f98_1fb2_d7c3_dd65_3a9e51b3be39["prepareDesignSystemStorage()"]
  81ec8f98_1fb2_d7c3_dd65_3a9e51b3be39 -->|calls| 5db4b61f_a5d4_16b1_b2ec_707bde46324a
  36be1773_d660_31ac_0b0b_88dbd7f6f7a8["styleRule()"]
  5db4b61f_a5d4_16b1_b2ec_707bde46324a -->|calls| 36be1773_d660_31ac_0b0b_88dbd7f6f7a8
  f9b19679_c1f0_28d6_4d1a_31a10c52e42d["atRule()"]
  5db4b61f_a5d4_16b1_b2ec_707bde46324a -->|calls| f9b19679_c1f0_28d6_4d1a_31a10c52e42d
  96876152_5423_5f9b_9f88_1db666070351["substituteAtApply()"]
  5db4b61f_a5d4_16b1_b2ec_707bde46324a -->|calls| 96876152_5423_5f9b_9f88_1db666070351
  ed78da58_8727_ad98_120c_61f35cea357a["walk()"]
  5db4b61f_a5d4_16b1_b2ec_707bde46324a -->|calls| ed78da58_8727_ad98_120c_61f35cea357a
  b9517e77_a36f_4751_899c_27d813f3dbb3["parse()"]
  5db4b61f_a5d4_16b1_b2ec_707bde46324a -->|calls| b9517e77_a36f_4751_899c_27d813f3dbb3
  2da63033_d079_7b37_5cfb_3877674a70b9["toCss()"]
  5db4b61f_a5d4_16b1_b2ec_707bde46324a -->|calls| 2da63033_d079_7b37_5cfb_3877674a70b9
  style 5db4b61f_a5d4_16b1_b2ec_707bde46324a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/canonicalize-candidates.ts lines 2395–2485

function createVariantSignatureCache(
  designSystem: DesignSystem,
): DesignSystem['storage'][typeof VARIANT_SIGNATURE_KEY] {
  return new DefaultMap<string, string | Symbol>((variant) => {
    try {
      // Ensure the prefix is added to the utility if it is not already present.
      variant =
        designSystem.theme.prefix && !variant.startsWith(designSystem.theme.prefix)
          ? `${designSystem.theme.prefix}:${variant}`
          : variant

      // Use `@apply` to normalize the selector to `.x`
      let ast: AstNode[] = [styleRule('.x', [atRule('@apply', `${variant}:flex`)])]
      substituteAtApply(ast, designSystem)

      // Canonicalize selectors to their minimal form
      walk(ast, (node) => {
        // At-rules
        if (node.kind === 'at-rule' && node.params.includes(' ')) {
          node.params = node.params.replaceAll(' ', '')
        }

        // Style rules
        else if (node.kind === 'rule') {
          let selectorAst = SelectorParser.parse(node.selector)
          let changed = false
          walk(selectorAst, (node) => {
            if (node.kind === 'separator' && node.value !== ' ') {
              node.value = node.value.trim()
              changed = true
            }

            // Remove unnecessary `:is(…)` selectors
            else if (node.kind === 'function' && node.value === ':is') {
              // A single selector inside of `:is(…)` can be replaced with the
              // selector itself.
              //
              // E.g.: `:is(.foo)` → `.foo`
              if (node.nodes.length === 1) {
                changed = true
                return WalkAction.Replace(node.nodes)
              }

              // A selector with the universal selector `*` followed by a pseudo
              // class, can be replaced with the pseudo class itself.
              else if (
                node.nodes.length === 2 &&
                node.nodes[0].kind === 'selector' &&
                node.nodes[0].value === '*' &&
                node.nodes[1].kind === 'selector' &&
                node.nodes[1].value[0] === ':'
              ) {
                changed = true
                return WalkAction.Replace(node.nodes[1])
              }
            }

            // Ensure `*` exists before pseudo selectors inside of `:not(…)`,
            // `:where(…)`, …
            //
            // E.g.:
            //
            // `:not(:first-child)` → `:not(*:first-child)`
            //
            else if (
              node.kind === 'function' &&
              node.value[0] === ':' &&
              node.nodes[0]?.kind === 'selector' &&
              node.nodes[0]?.value[0] === ':'
            ) {
              changed = true
              node.nodes.unshift({ kind: 'selector', value: '*' })
            }
          })

          if (changed) {
            node.selector = SelectorParser.toCss(selectorAst)
          }
        }
      })

Subdomains

Frequently Asked Questions

What does createVariantSignatureCache() do?
createVariantSignatureCache() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/canonicalize-candidates.ts.
Where is createVariantSignatureCache() defined?
createVariantSignatureCache() is defined in packages/tailwindcss/src/canonicalize-candidates.ts at line 2395.
What does createVariantSignatureCache() call?
createVariantSignatureCache() calls 6 function(s): atRule, parse, styleRule, substituteAtApply, toCss, walk.
What calls createVariantSignatureCache()?
createVariantSignatureCache() 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