Home / File/ migrate-theme-to-var.ts — tailwindcss Source File

migrate-theme-to-var.ts — tailwindcss Source File

Architecture documentation for migrate-theme-to-var.ts, a typescript file in the tailwindcss codebase. 14 imports, 1 dependents.

File typescript UpgradeToolkit Codemods 14 imports 1 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  de8dd9be_8c47_4694_db3b_393c549a926a["migrate-theme-to-var.ts"]
  669e6a28_c71f_3c5e_9c53_915cede7da78["candidate.ts"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> 669e6a28_c71f_3c5e_9c53_915cede7da78
  245c850a_c551_a2cf_854e_bba95b5a1339["apply-config-to-theme.ts"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> 245c850a_c551_a2cf_854e_bba95b5a1339
  ae20c4fb_29d7_ca79_3c49_ca02c45d5369["keyPathToCssProperty"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> ae20c4fb_29d7_ca79_3c49_ca02c45d5369
  7fd72d4c_e95c_d849_1002_1e1c9d8aca1a["design-system.ts"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> 7fd72d4c_e95c_d849_1002_1e1c9d8aca1a
  516809a4_c70e_60c3_bbb2_8de4c4572510["infer-data-type.ts"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> 516809a4_c70e_60c3_bbb2_8de4c4572510
  a15b3c4a_76ff_0090_fc86_bac24f0a4135["isValidSpacingMultiplier"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> a15b3c4a_76ff_0090_fc86_bac24f0a4135
  ef204000_8998_5a6c_5455_324b37624713["segment.ts"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> ef204000_8998_5a6c_5455_324b37624713
  f712ed47_45d4_4e5a_dd73_fdefa1da71da["segment"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> f712ed47_45d4_4e5a_dd73_fdefa1da71da
  e3e0c6b9_7cc3_bd6c_ce01_14f8efdc5a0d["to-key-path.ts"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> e3e0c6b9_7cc3_bd6c_ce01_14f8efdc5a0d
  1b250eae_0bea_d404_ca9e_42da26c56b45["toKeyPath"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> 1b250eae_0bea_d404_ca9e_42da26c56b45
  1d3f1613_f144_938f_08f7_49039a46ad49["value-parser.ts"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> 1d3f1613_f144_938f_08f7_49039a46ad49
  d1b39b63_c9d5_6c28_0206_0ddc8b895876["walk.ts"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> d1b39b63_c9d5_6c28_0206_0ddc8b895876
  ed78da58_8727_ad98_120c_61f35cea357a["walk"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> ed78da58_8727_ad98_120c_61f35cea357a
  7b34c369_d799_30f1_b751_6e3fd5349f6b["WalkAction"]
  de8dd9be_8c47_4694_db3b_393c549a926a --> 7b34c369_d799_30f1_b751_6e3fd5349f6b
  style de8dd9be_8c47_4694_db3b_393c549a926a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { type CandidateModifier } from '../../../../tailwindcss/src/candidate'
import { keyPathToCssProperty } from '../../../../tailwindcss/src/compat/apply-config-to-theme'
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
import { isValidSpacingMultiplier } from '../../../../tailwindcss/src/utils/infer-data-type'
import { segment } from '../../../../tailwindcss/src/utils/segment'
import { toKeyPath } from '../../../../tailwindcss/src/utils/to-key-path'
import * as ValueParser from '../../../../tailwindcss/src/value-parser'
import { walk, WalkAction } from '../../../../tailwindcss/src/walk'

export const enum Convert {
  All = 0,
  MigrateModifier = 1 << 0,
  MigrateThemeOnly = 1 << 1,
}

export function createConverter(designSystem: DesignSystem, { prettyPrint = false } = {}) {
  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(…)`
// ... (211 more lines)

Subdomains

Types

Frequently Asked Questions

What does migrate-theme-to-var.ts do?
migrate-theme-to-var.ts is a source file in the tailwindcss codebase, written in typescript. It belongs to the UpgradeToolkit domain, Codemods subdomain.
What functions are defined in migrate-theme-to-var.ts?
migrate-theme-to-var.ts defines 3 function(s): createConverter, eventuallyUnquote, substituteFunctionsInValue.
What does migrate-theme-to-var.ts depend on?
migrate-theme-to-var.ts imports 14 module(s): WalkAction, apply-config-to-theme.ts, candidate.ts, design-system.ts, infer-data-type.ts, isValidSpacingMultiplier, keyPathToCssProperty, segment, and 6 more.
What files import migrate-theme-to-var.ts?
migrate-theme-to-var.ts is imported by 1 file(s): migrate-theme-to-var.ts.
Where is migrate-theme-to-var.ts in the architecture?
migrate-theme-to-var.ts is located at packages/@tailwindcss-upgrade/src/codemods/template/migrate-theme-to-var.ts (domain: UpgradeToolkit, subdomain: Codemods, directory: packages/@tailwindcss-upgrade/src/codemods/template).

Analyze Your Own Codebase

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

Try Supermodel Free