constantFoldDeclaration() — tailwindcss Function Reference
Architecture documentation for the constantFoldDeclaration() function in constant-fold-declaration.ts from the tailwindcss codebase.
Entity Profile
Dependency Diagram
graph TD 9df707f7_aef8_37b0_5f90_edacde047f5b["constantFoldDeclaration()"] 6a318dee_41f9_3a09_3876_c73653eb7c29["createSpacingCache()"] 6a318dee_41f9_3a09_3876_c73653eb7c29 -->|calls| 9df707f7_aef8_37b0_5f90_edacde047f5b 84dbd847_e2eb_8fba_afa7_47012ee6086f["canonicalizeAst()"] 84dbd847_e2eb_8fba_afa7_47012ee6086f -->|calls| 9df707f7_aef8_37b0_5f90_edacde047f5b 0638028e_f2f7_8e77_2f19_1bd2ce4b2d6a["parse()"] 9df707f7_aef8_37b0_5f90_edacde047f5b -->|calls| 0638028e_f2f7_8e77_2f19_1bd2ce4b2d6a e9d556bc_f22d_356c_1bd2_27442c34b5c7["walk()"] 9df707f7_aef8_37b0_5f90_edacde047f5b -->|calls| e9d556bc_f22d_356c_1bd2_27442c34b5c7 c941f6f8_779c_d328_ce6e_6b8d73133036["canonicalizeDimension()"] 9df707f7_aef8_37b0_5f90_edacde047f5b -->|calls| c941f6f8_779c_d328_ce6e_6b8d73133036 13630325_e89c_c092_2a8e_e2e15f2e4735["word()"] 9df707f7_aef8_37b0_5f90_edacde047f5b -->|calls| 13630325_e89c_c092_2a8e_e2e15f2e4735 f66dddcc_be1c_d082_4eb0_ec82ce3bf380["toCss()"] 9df707f7_aef8_37b0_5f90_edacde047f5b -->|calls| f66dddcc_be1c_d082_4eb0_ec82ce3bf380 style 9df707f7_aef8_37b0_5f90_edacde047f5b fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/tailwindcss/src/constant-fold-declaration.ts lines 8–115
export function constantFoldDeclaration(input: string, rem: number | null = null): string {
let folded = false
let valueAst = ValueParser.parse(input)
walk(valueAst, {
exit(valueNode) {
// Canonicalize dimensions to their simplest form. This includes:
// - Convert `-0`, `+0`, `0.0`, … to `0`
// - Convert `-0px`, `+0em`, `0.0rem`, … to `0`
// - Convert units to an equivalent unit
if (
valueNode.kind === 'word' &&
valueNode.value !== '0' // Already `0`, nothing to do
) {
let canonical = canonicalizeDimension(valueNode.value, rem)
if (canonical === null) return // Couldn't be canonicalized, nothing to do
if (canonical === valueNode.value) return // Already in canonical form, nothing to do
folded = true
return WalkAction.ReplaceSkip(ValueParser.word(canonical))
}
// Constant fold `calc()` expressions with two operands and one operator
else if (
valueNode.kind === 'function' &&
(valueNode.value === 'calc' || valueNode.value === '')
) {
// [
// { kind: 'word', value: '0.25rem' }, 0
// { kind: 'separator', value: ' ' }, 1
// { kind: 'word', value: '*' }, 2
// { kind: 'separator', value: ' ' }, 3
// { kind: 'word', value: '256' } 4
// ]
if (valueNode.nodes.length !== 5) return
let lhs = dimensions.get(valueNode.nodes[0].value)
let operator = valueNode.nodes[2].value
let rhs = dimensions.get(valueNode.nodes[4].value)
// Nullify entire expression when multiplying by `0`, e.g.: `calc(0 * 100vw)` -> `0`
//
// TODO: Ensure it's safe to do so based on the data types?
if (
operator === '*' &&
((lhs?.[0] === 0 && lhs?.[1] === null) || // 0 * something
(rhs?.[0] === 0 && rhs?.[1] === null)) // something * 0
) {
folded = true
return WalkAction.ReplaceSkip(ValueParser.word('0'))
}
// We're not dealing with dimensions, so we can't fold this
if (lhs === null || rhs === null) {
return
}
switch (operator) {
case '*': {
if (
lhs[1] === rhs[1] || // Same Units, e.g.: `1rem * 2rem`, `8 * 6`
(lhs[1] === null && rhs[1] !== null) || // Unitless * Unit, e.g.: `2 * 1rem`
(lhs[1] !== null && rhs[1] === null) // Unit * Unitless, e.g.: `1rem * 2`
) {
folded = true
return WalkAction.ReplaceSkip(ValueParser.word(`${lhs[0] * rhs[0]}${lhs[1] ?? ''}`))
}
break
}
case '+': {
if (
lhs[1] === rhs[1] // Same unit or unitless, e.g.: `1rem + 2rem`, `8 + 6`
) {
folded = true
return WalkAction.ReplaceSkip(ValueParser.word(`${lhs[0] + rhs[0]}${lhs[1] ?? ''}`))
}
break
}
case '-': {
if (
lhs[1] === rhs[1] // Same unit or unitless, e.g.: `2rem - 1rem`, `8 - 6`
) {
folded = true
return WalkAction.ReplaceSkip(ValueParser.word(`${lhs[0] - rhs[0]}${lhs[1] ?? ''}`))
}
break
}
case '/': {
if (
rhs[0] !== 0 && // Don't divide by zero
((lhs[1] === null && rhs[1] === null) || // Unitless / Unitless, e.g.: `8 / 2`
(lhs[1] !== null && rhs[1] === null)) // Unit / Unitless, e.g.: `1rem / 2`
) {
folded = true
return WalkAction.ReplaceSkip(ValueParser.word(`${lhs[0] / rhs[0]}${lhs[1] ?? ''}`))
}
break
}
}
}
},
})
return folded ? ValueParser.toCss(valueAst) : input
}
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does constantFoldDeclaration() do?
constantFoldDeclaration() is a function in the tailwindcss codebase.
What does constantFoldDeclaration() call?
constantFoldDeclaration() calls 5 function(s): canonicalizeDimension, parse, toCss, walk, word.
What calls constantFoldDeclaration()?
constantFoldDeclaration() is called by 2 function(s): canonicalizeAst, createSpacingCache.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free