expose methods instead of raw DefaultMaps

This commit is contained in:
Robin Malfait 2024-03-08 11:42:08 +01:00
parent ce38fcde3e
commit e05e5ece43
No known key found for this signature in database
3 changed files with 14 additions and 9 deletions

View file

@ -31,7 +31,7 @@ export function compileCandidates(
}
// Sort the variants
let variants = Array.from(designSystem.parsedVariants.values()).sort((a, z) => {
let variants = designSystem.getUsedVariants().sort((a, z) => {
return designSystem.variants.compare(a, z)
})

View file

@ -22,9 +22,8 @@ export type DesignSystem = {
parseVariant(variant: string): ReturnType<typeof parseVariant>
parseAstNode(candidate: string): ReturnType<typeof parseAstNode>
parsedCandidates: DefaultMap<string, ReturnType<typeof parseCandidate>>
parsedVariants: DefaultMap<string, ReturnType<typeof parseVariant>>
parsedAstNodes: DefaultMap<string, ReturnType<typeof parseAstNode>>
getUsedVariants(): ReturnType<typeof parseVariant>[]
getAstNodeSize(): number
invalidRawCandidates: Set<string>
}
@ -42,9 +41,6 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
theme,
utilities,
variants,
parsedCandidates,
parsedVariants,
parsedAstNodes,
invalidRawCandidates,
candidatesToCss(classes: string[]) {
@ -81,6 +77,12 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
parseAstNode(candidate: string) {
return parsedAstNodes.get(candidate)
},
getUsedVariants() {
return Array.from(parsedVariants.values())
},
getAstNodeSize() {
return parsedAstNodes.size
},
}
return designSystem

View file

@ -174,6 +174,9 @@ export function compile(
})
}
// Track all valid candidates, these are the incoming `rawCandidate` that
// resulted in a generated AST Node. All the other `rawCandidates` are invalid
// and should be ignored.
let allValidCandidates = new Set<string>()
for (let rawCandidate of rawCandidates) {
if (!designSystem.invalidRawCandidates.has(rawCandidate)) {
@ -199,13 +202,13 @@ export function compile(
}
if (tailwindUtilitiesNode) {
let previousAstNodeCount = designSystem.parsedAstNodes.size
let previousAstNodeCount = designSystem.getAstNodeSize()
let newNodes = compileCandidates(allValidCandidates, designSystem).astNodes
// If no new ast nodes were generated, then we can return the original
// CSS. This currently assumes that we only add new ast nodes and never
// remove any.
if (previousAstNodeCount === designSystem.parsedAstNodes.size) {
if (previousAstNodeCount === designSystem.getAstNodeSize()) {
return compiledCss
}