|
1 | 1 | import { optimizeAst, toCss } from './ast'
|
2 | 2 | import { parseCandidate, parseVariant, type Candidate, type Variant } from './candidate'
|
3 | 3 | import { compileAstNodes, compileCandidates } from './compile'
|
| 4 | +import { substituteFunctions } from './css-functions' |
4 | 5 | import { getClassList, getVariants, type ClassEntry, type VariantEntry } from './intellisense'
|
5 | 6 | import { getClassOrder } from './sort'
|
6 | 7 | import type { Theme, ThemeKey } from './theme'
|
@@ -41,9 +42,27 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
|
41 | 42 | let parsedCandidates = new DefaultMap((candidate) =>
|
42 | 43 | Array.from(parseCandidate(candidate, designSystem)),
|
43 | 44 | )
|
44 |
| - let compiledAstNodes = new DefaultMap<Candidate>((candidate) => |
45 |
| - compileAstNodes(candidate, designSystem), |
46 |
| - ) |
| 45 | + let compiledAstNodes = new DefaultMap<Candidate>((candidate) => { |
| 46 | + let ast = compileAstNodes(candidate, designSystem) |
| 47 | + |
| 48 | + // Arbitrary values (`text-[theme(--color-red-500)]`) and arbitrary |
| 49 | + // properties (`[--my-var:theme(--color-red-500)]`) can contain function |
| 50 | + // calls so we need evaluate any functions we find there that weren't in |
| 51 | + // the source CSS. |
| 52 | + try { |
| 53 | + substituteFunctions( |
| 54 | + ast.map(({ node }) => node), |
| 55 | + designSystem, |
| 56 | + ) |
| 57 | + } catch (err) { |
| 58 | + // If substitution fails then the candidate likely contains a call to |
| 59 | + // `theme()` that is invalid which may be because of incorrect usage, |
| 60 | + // invalid arguments, or a theme key that does not exist. |
| 61 | + return [] |
| 62 | + } |
| 63 | + |
| 64 | + return ast |
| 65 | + }) |
47 | 66 |
|
48 | 67 | let designSystem: DesignSystem = {
|
49 | 68 | theme,
|
|
0 commit comments