| 68 | } |
| 69 | |
| 70 | export function buildDesignSystem( |
| 71 | theme: Theme, |
| 72 | utilitiesSrc?: SourceLocation | undefined, |
| 73 | ): DesignSystem { |
| 74 | let utilities = createUtilities(theme) |
| 75 | let variants = createVariants(theme) |
| 76 | |
| 77 | let parsedVariants = new DefaultMap((variant) => parseVariant(variant, designSystem)) |
| 78 | let parsedCandidates = new DefaultMap((candidate) => |
| 79 | Array.from(parseCandidate(candidate, designSystem)), |
| 80 | ) |
| 81 | |
| 82 | let compiledAstNodes = new DefaultMap<number>((flags) => { |
| 83 | return new DefaultMap<Candidate>((candidate) => { |
| 84 | let ast = compileAstNodes(candidate, designSystem, flags) |
| 85 | |
| 86 | try { |
| 87 | let nodes = ast.map((value) => value.node) |
| 88 | |
| 89 | class="cm">// Arbitrary values (`text-[theme(--color-red-500)]`) and arbitrary |
| 90 | class="cm">// properties (`[--my-var:theme(--color-red-500)]`) can contain function |
| 91 | class="cm">// calls so we need evaluate any functions we find there that weren't in |
| 92 | class="cm">// the source CSS. |
| 93 | substituteFunctions(nodes, designSystem) |
| 94 | |
| 95 | class="cm">// JS plugins might contain an `@variant` inside a generated utility |
| 96 | substituteAtVariant(nodes, designSystem) |
| 97 | } catch (err) { |
| 98 | class="cm">// If substitution fails then the candidate likely contains a call to |
| 99 | class="cm">// `theme()` that is invalid which may be because of incorrect usage, |
| 100 | class="cm">// invalid arguments, or a theme key that does not exist. |
| 101 | return [] |
| 102 | } |
| 103 | |
| 104 | return ast |
| 105 | }) |
| 106 | }) |
| 107 | |
| 108 | let trackUsedVariables = new DefaultMap((raw) => { |
| 109 | for (let variable of extractUsedVariables(raw)) { |
| 110 | theme.markUsedVariable(variable) |
| 111 | } |
| 112 | }) |
| 113 | |
| 114 | function candidatesToAst(classes: string[]): AstNode[][] { |
| 115 | let result: AstNode[][] = [] |
| 116 | |
| 117 | for (let className of classes) { |
| 118 | let wasValid = true |
| 119 | |
| 120 | let { astNodes } = compileCandidates([className], designSystem, { |
| 121 | onInvalidCandidate() { |
| 122 | wasValid = false |
| 123 | }, |
| 124 | }) |
| 125 | |
| 126 | if (utilitiesSrc) { |
| 127 | walk(astNodes, (node) => { |