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 f875e425_9644_1071_3601_45e7c7f789d3["constantFoldDeclaration()"] 28f23724_d31a_a8d3_3dd6_07e505ec5b7b["constant-fold-declaration.ts"] f875e425_9644_1071_3601_45e7c7f789d3 -->|defined in| 28f23724_d31a_a8d3_3dd6_07e505ec5b7b ee814b5e_14e2_34f8_20d7_5683eb2bb8eb["createSpacingCache()"] ee814b5e_14e2_34f8_20d7_5683eb2bb8eb -->|calls| f875e425_9644_1071_3601_45e7c7f789d3 e559573b_f2f8_833b_27e7_100efea815c0["canonicalizeAst()"] e559573b_f2f8_833b_27e7_100efea815c0 -->|calls| f875e425_9644_1071_3601_45e7c7f789d3 49a8c506_c50e_ed4b_5a0e_0393edae2b6f["parse()"] f875e425_9644_1071_3601_45e7c7f789d3 -->|calls| 49a8c506_c50e_ed4b_5a0e_0393edae2b6f 4982d9ce_98d4_85d9_44af_7cc47b93c482["walk()"] f875e425_9644_1071_3601_45e7c7f789d3 -->|calls| 4982d9ce_98d4_85d9_44af_7cc47b93c482 7e4f2893_9c53_d837_29a4_e4e87eb8f6e4["canonicalizeDimension()"] f875e425_9644_1071_3601_45e7c7f789d3 -->|calls| 7e4f2893_9c53_d837_29a4_e4e87eb8f6e4 a3899c0b_a2d5_1d97_6729_34991dde1cce["word()"] f875e425_9644_1071_3601_45e7c7f789d3 -->|calls| a3899c0b_a2d5_1d97_6729_34991dde1cce d6cf80a6_8130_7069_e60d_09156f156b67["toCss()"] f875e425_9644_1071_3601_45e7c7f789d3 -->|calls| d6cf80a6_8130_7069_e60d_09156f156b67 style f875e425_9644_1071_3601_45e7c7f789d3 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 '-': {
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does constantFoldDeclaration() do?
constantFoldDeclaration() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/constant-fold-declaration.ts.
Where is constantFoldDeclaration() defined?
constantFoldDeclaration() is defined in packages/tailwindcss/src/constant-fold-declaration.ts at line 8.
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