createConverter() — tailwindcss Function Reference
Architecture documentation for the createConverter() function in migrate-theme-to-var.ts from the tailwindcss codebase.
Entity Profile
Dependency Diagram
graph TD da84d4b5_fc28_e826_1855_2340e6df074e["createConverter()"] b5d4b5a0_3713_0a6e_7e28_5aabce2dc697["migrateThemeToVar()"] b5d4b5a0_3713_0a6e_7e28_5aabce2dc697 -->|calls| da84d4b5_fc28_e826_1855_2340e6df074e 0638028e_f2f7_8e77_2f19_1bd2ce4b2d6a["parse()"] da84d4b5_fc28_e826_1855_2340e6df074e -->|calls| 0638028e_f2f7_8e77_2f19_1bd2ce4b2d6a 360848c5_bd2e_d4dd_ad1b_074434129232["substituteFunctionsInValue()"] da84d4b5_fc28_e826_1855_2340e6df074e -->|calls| 360848c5_bd2e_d4dd_ad1b_074434129232 2a20ea29_c850_cd61_5600_aeebbe3dda66["segment()"] da84d4b5_fc28_e826_1855_2340e6df074e -->|calls| 2a20ea29_c850_cd61_5600_aeebbe3dda66 971f7bc7_6ac2_a554_ba52_47038ff41b13["keyPathToCssProperty()"] da84d4b5_fc28_e826_1855_2340e6df074e -->|calls| 971f7bc7_6ac2_a554_ba52_47038ff41b13 f1189154_e89f_8988_3e83_53458fbcad21["toKeyPath()"] da84d4b5_fc28_e826_1855_2340e6df074e -->|calls| f1189154_e89f_8988_3e83_53458fbcad21 c12acffb_80f9_0b95_b1a6_e663fbaca197["isValidSpacingMultiplier()"] da84d4b5_fc28_e826_1855_2340e6df074e -->|calls| c12acffb_80f9_0b95_b1a6_e663fbaca197 style da84d4b5_fc28_e826_1855_2340e6df074e fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/@tailwindcss-upgrade/src/codemods/template/migrate-theme-to-var.ts lines 16–170
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(…)`
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.
//
// If there is more than one node in the AST though, `theme(…)` must not
// be the whole value so it's not safe to use a modifier instead.
//
// E.g.: `inset 0px 1px theme(colors.red.500/50%)` is a shadow, not a color.
if (ast.length === 1 && parts.length === 2 && options & Convert.MigrateModifier) {
let [pathPart, modifierPart] = parts
// 50% -> /50
if (/^\d+%$/.test(modifierPart)) {
modifier = { kind: 'named', value: modifierPart.slice(0, -1) }
}
// .12 -> /12
// .12345 -> /[12.345]
else if (/^0?\.\d+$/.test(modifierPart)) {
let value = Number(modifierPart) * 100
modifier = {
kind: Number.isInteger(value) ? 'named' : 'arbitrary',
value: value.toString(),
}
}
// Anything else becomes arbitrary
else {
modifier = { kind: 'arbitrary', value: modifierPart }
}
// Update path to be the first part
path = pathPart
}
return toVar(path, fallback) || toTheme(path, fallback)
})
return [result, modifier]
}
function pathToVariableName(path: string) {
let variable = `--${keyPathToCssProperty(toKeyPath(path))}` as const
if (!designSystem.theme.get([variable])) return null
if (designSystem.theme.prefix) {
return `--${designSystem.theme.prefix}-${variable.slice(2)}`
}
return variable
}
function toVar(path: string, fallback?: string) {
let variable = pathToVariableName(path)
if (variable) return fallback ? `var(${variable}, ${fallback})` : `var(${variable})`
let keyPath = toKeyPath(path)
if (keyPath[0] === 'spacing' && designSystem.theme.get(['--spacing'])) {
let multiplier = keyPath[1]
if (!isValidSpacingMultiplier(multiplier)) return null
return `--spacing(${multiplier})`
}
return null
}
function toTheme(path: string, fallback?: string) {
let parts = segment(path, '/').map((part) => part.trim())
path = parts.shift()!
let variable = pathToVariableName(path)
if (!variable) return null
let modifier =
parts.length > 0 ? (prettyPrint ? ` / ${parts.join(' / ')}` : `/${parts.join('/')}`) : ''
return fallback
? `--theme(${variable}${modifier}, ${fallback})`
: `--theme(${variable}${modifier})`
}
return convert
}
Domain
Subdomains
Calls
Called By
Source
Frequently Asked Questions
What does createConverter() do?
createConverter() is a function in the tailwindcss codebase.
What does createConverter() call?
createConverter() calls 6 function(s): isValidSpacingMultiplier, keyPathToCssProperty, parse, segment, substituteFunctionsInValue, toKeyPath.
What calls createConverter()?
createConverter() is called by 1 function(s): migrateThemeToVar.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free