Home / Function/ createConverterCache() — tailwindcss Function Reference

createConverterCache() — tailwindcss Function Reference

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

Function typescript Oxide Extractor calls 8 called by 1

Entity Profile

Dependency Diagram

graph TD
  7f0ac26e_d200_2f14_4236_46ff9eed21ef["createConverterCache()"]
  f6c14bbb_2e42_58cc_18f1_c89a243da9c0["canonicalize-candidates.ts"]
  7f0ac26e_d200_2f14_4236_46ff9eed21ef -->|defined in| f6c14bbb_2e42_58cc_18f1_c89a243da9c0
  26169039_1fd2_e320_1f63_11ec10a6fc52["prepareDesignSystemStorage()"]
  26169039_1fd2_e320_1f63_11ec10a6fc52 -->|calls| 7f0ac26e_d200_2f14_4236_46ff9eed21ef
  a4ba0e6f_8062_6435_8f6d_7ec692b4801d["substituteFunctionsInValue()"]
  7f0ac26e_d200_2f14_4236_46ff9eed21ef -->|calls| a4ba0e6f_8062_6435_8f6d_7ec692b4801d
  c58cbb33_f3cc_0b4f_844a_15bf66a1dc61["segment()"]
  7f0ac26e_d200_2f14_4236_46ff9eed21ef -->|calls| c58cbb33_f3cc_0b4f_844a_15bf66a1dc61
  1767219e_c38a_227c_492b_5d47634d54a4["keyPathToCssProperty()"]
  7f0ac26e_d200_2f14_4236_46ff9eed21ef -->|calls| 1767219e_c38a_227c_492b_5d47634d54a4
  05d44181_6631_c1fa_4a63_96653e28f99c["toKeyPath()"]
  7f0ac26e_d200_2f14_4236_46ff9eed21ef -->|calls| 05d44181_6631_c1fa_4a63_96653e28f99c
  09df19e7_0346_145d_40c4_e93ff14a2ff9["isValidSpacingMultiplier()"]
  7f0ac26e_d200_2f14_4236_46ff9eed21ef -->|calls| 09df19e7_0346_145d_40c4_e93ff14a2ff9
  4982d9ce_98d4_85d9_44af_7cc47b93c482["walk()"]
  7f0ac26e_d200_2f14_4236_46ff9eed21ef -->|calls| 4982d9ce_98d4_85d9_44af_7cc47b93c482
  2820372c_b982_9e06_fc23_f8f4ac308d00["get()"]
  7f0ac26e_d200_2f14_4236_46ff9eed21ef -->|calls| 2820372c_b982_9e06_fc23_f8f4ac308d00
  117bca28_0677_ab3f_6eec_6019671b392d["parse()"]
  7f0ac26e_d200_2f14_4236_46ff9eed21ef -->|calls| 117bca28_0677_ab3f_6eec_6019671b392d
  style 7f0ac26e_d200_2f14_4236_46ff9eed21ef fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/canonicalize-candidates.ts lines 640–799

function createConverterCache(
  designSystem: DesignSystem,
): DesignSystem['storage'][typeof CONVERTER_KEY] {
  return createConverter(designSystem)

  function createConverter(designSystem: DesignSystem) {
    function convert(input: string, options = Convert.All): [string, CandidateModifier | null] {
      let ast = ValueParser.parse(input)

      // In some scenarios (e.g.: variants), we can't migrate to `var(…)` if it
      // ends up in the `@media (…)` part. In this case we only have to migrate to
      // the new `theme(…)` notation.
      if (options & Convert.MigrateThemeOnly) {
        return [substituteFunctionsInValue(ast, toTheme), null]
      }

      let themeUsageCount = 0
      let themeModifierCount = 0

      // Analyze AST
      walk(ast, (node) => {
        if (node.kind !== 'function') return
        if (node.value !== 'theme') return

        // We are only interested in the `theme` function
        themeUsageCount += 1

        // Figure out if a modifier is used
        walk(node.nodes, (child) => {
          // If we see a `,`, it means that we have a fallback value
          if (child.kind === 'separator' && child.value.includes(',')) {
            return WalkAction.Stop
          }

          // If we see a `/`, we have a modifier
          else if (child.kind === 'word' && child.value === '/') {
            themeModifierCount += 1
            return WalkAction.Stop
          }

          return WalkAction.Skip
        })
      })

      // No `theme(…)` calls, nothing to do
      if (themeUsageCount === 0) {
        return [input, null]
      }

      // No `theme(…)` with modifiers, we can migrate to `var(…)`
      if (themeModifierCount === 0) {
        return [substituteFunctionsInValue(ast, toVar), null]
      }

      // Multiple modifiers which means that there are multiple `theme(…/…)`
      // values. In this case, we can't convert the modifier to a candidate
      // modifier.
      //
      // We also can't migrate to `var(…)` because that would lose the modifier.
      //
      // Try to convert each `theme(…)` call to the modern syntax.
      if (themeModifierCount > 1) {
        return [substituteFunctionsInValue(ast, toTheme), null]
      }

      // Only a single `theme(…)` with a modifier left, that modifier will be
      // migrated to a candidate modifier.
      let modifier: CandidateModifier | null = null
      let result = substituteFunctionsInValue(ast, (path, fallback) => {
        let parts = segment(path, '/').map((part) => part.trim())

        // Multiple `/` separators, which makes this an invalid path
        if (parts.length > 2) return null

        // The path contains a `/`, which means that there is a modifier such as
        // `theme(colors.red.500/50%)`.
        //
        // Currently, we are assuming that this is only being used for colors,
        // which means that we can typically convert them to a modifier on the
        // candidate itself.
        //

Domain

Subdomains

Frequently Asked Questions

What does createConverterCache() do?
createConverterCache() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/canonicalize-candidates.ts.
Where is createConverterCache() defined?
createConverterCache() is defined in packages/tailwindcss/src/canonicalize-candidates.ts at line 640.
What does createConverterCache() call?
createConverterCache() calls 8 function(s): get, isValidSpacingMultiplier, keyPathToCssProperty, parse, segment, substituteFunctionsInValue, toKeyPath, walk.
What calls createConverterCache()?
createConverterCache() 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