import { Polyfills } from '.'
import { atRule, CACHEABLE, decl, HOISTABLE_AT_RULES, optimizeAst, rule, type AstNode } from './ast'
import type { Candidate, Variant } from './candidate'
import { CompileAstFlags, type DesignSystem } from './design-system'
import { compare } from './utils/compare'
import { walk } from './walk'
interface SortInfo {
node: AstNode
candidate: Candidate
raw: string
order: number[]
count: number
variants: bigint
}
interface CachedUtility {
nodes: AstNode[]
atRoots: AstNode[]
animations: string[]
}
const PLACEHOLDER = '\0tw-utilities'
export function createIncrementalBuild(
ast: AstNode[],
utilitiesNode: AstNode & { nodes: AstNode[] },
designSystem: DesignSystem,
polyfills: Polyfills = Polyfills.All,
onInvalidCandidate: (candidate: string) => void,
trackSources = false,
) {
let found = findWrappingAtRules(ast, utilitiesNode)
if (found === null) return null
const wrappers: [name: string, params: string][] = found
let utilitiesAtRoot = wrappers.length === 0
let sorted: SortInfo[] = []
let variantOrder: Map<Variant, number> | null = null
let cache = new WeakMap<AstNode, CachedUtility>()
let previous: AstNode[] | null = null
function variantBits(candidate: Candidate) {
let bits = 0n
for (let variant of candidate.variants) {
bits |= 1n << BigInt(variantOrder!.get(variant)!)
}
return bits
}
function compareSortInfo(a: SortInfo, z: SortInfo) {
if (a.variants !== z.variants) return a.variants < z.variants ? -1 : 1
let offset = 0
while (
offset < a.order.length &&
offset < z.order.length &&
a.order[offset] === z.order[offset]
) {
offset += 1
}
return (
(a.order[offset] ?? Infinity) - (z.order[offset] ?? Infinity) ||
z.count - a.count ||
compare(a.raw, z.raw)
)
}
function getCachedUtility(node: AstNode): CachedUtility | null {
let cached = cache.get(node)
if (cached) return cached
let atRoots: AstNode[] = []
let animations: string[] = []
if (!collectAtRootsAndAnimations([node], atRoots, animations, false)) return null
let nodes = flattenSimpleUtility(node) ?? flattenUtility(node)
if (nodes === null) return null
markCacheable(nodes)
cached = { nodes, atRoots, animations }
cache.set(node, cached)
return cached
}
function flattenUtility(node: AstNode): AstNode[] | null {
let wrapped: AstNode = node
for (let i = wrappers.length - 1; i >= 0; i--) {
wrapped = atRule(wrappers[i][0], wrappers[i][1], [wrapped])
}
let nodes = optimizeAst([wrapped], designSystem, polyfills, true)
for (let [name, params] of wrappers) {
if (nodes.length === 0) break
let wrapper = nodes[0]
if (
nodes.length !== 1 ||
wrapper.kind !== 'at-rule' ||
wrapper.name !== name ||
wrapper.params !== params
) {
return null
}
nodes = wrapper.nodes
}
return nodes
}
function flattenSimpleUtility(node: AstNode): AstNode[] | null {
if (node.kind !== 'rule' || node.selector.includes('&')) return null
let declarations: AstNode[] = []
let seen = new Set<string>()
for (let child of node.nodes) {
if (child.kind !== 'declaration') return null
if (child.property === '--tw-sort' || child.value == null) continue
if (seen.has(child.property)) return null
seen.add(child.property)
if (polyfills & Polyfills.ColorMix && child.value.includes('color-mix(')) return null
if (child.value.includes('var(')) designSystem.trackUsedVariables(child.value)
declarations.push(child)
}
if (declarations.length === 0) return []
let copy = rule(node.selector, declarations)
if (node.src) copy.src = node.src
if (node.dst) copy.dst = node.dst
return [copy]
}
return function build(candidates: Iterable<string>, force: boolean): AstNode[] | null {
let added: Omit<SortInfo, 'variants'>[] = []
for (let raw of candidates) {
if (designSystem.invalidCandidates.has(raw)) continue
let found = false
for (let candidate of designSystem.parseCandidate(raw)) {
let rules = designSystem.compileAstNodes(candidate, CompileAstFlags.RespectImportant)
for (let { node, propertySort } of rules) {
found = true
if (trackSources) {
walk([node], (node) => {
node.src ??= utilitiesNode.src
})
}
added.push({ node, candidate, raw, ...propertySort })
}
}
if (!found) onInvalidCandidate(raw)
}
let nextVariantOrder = designSystem.getVariantOrder()
if (nextVariantOrder !== variantOrder) {
variantOrder = nextVariantOrder
for (let info of sorted) info.variants = variantBits(info.candidate)
}
if (added.length === 0 && !force && previous !== null) return previous
let infos: SortInfo[] = added.map((info) => ({
...info,
variants: variantBits(info.candidate),
}))
infos.sort(compareSortInfo)
if (infos.length > 0) {
let merged: SortInfo[] = new Array(sorted.length + infos.length)
let i = 0
let j = 0
let k = 0
while (i < sorted.length && j < infos.length) {
merged[k++] = compareSortInfo(sorted[i], infos[j]) <= 0 ? sorted[i++] : infos[j++]
}
while (i < sorted.length) merged[k++] = sorted[i++]
while (j < infos.length) merged[k++] = infos[j++]
sorted = merged
}
let utilities: AstNode[] = []
let atRoots: AstNode[] = []
let animations = new Set<string>()
let merged = new Set<AstNode[]>()
for (let info of sorted) {
let cached = getCachedUtility(info.node)
if (cached === null) return null
for (let node of cached.nodes) mergeInto(utilities, node, merged, utilitiesAtRoot)
for (let node of cached.atRoots) atRoots.push(node)
for (let value of cached.animations) animations.add(value)
}
if (utilities.length === 0) {
utilitiesNode.nodes = sorted.map((info) => info.node)
previous = optimizeAst(ast, designSystem, polyfills)
return previous
}
let placeholder = rule(PLACEHOLDER, [decl('--tw-placeholder', '1')])
for (let value of animations) placeholder.nodes.push(decl('animation', value))
utilitiesNode.nodes = [placeholder, ...atRoots]
let result = optimizeAst(ast, designSystem, polyfills)
let parent = findParentOfPlaceholder(result)
if (parent === null) return null
let idx = parent.findIndex((node) => node.kind === 'rule' && node.selector === PLACEHOLDER)
let isRoot = parent === result
let nodes = parent.slice(0, idx)
for (let node of utilities) mergeInto(nodes, node, merged, isRoot)
for (let i = idx + 1; i < parent.length; i++) mergeInto(nodes, parent[i], merged, isRoot)
parent.length = 0
for (let node of nodes) parent.push(node)
for (let nodes of merged) dedupeDeclarations(nodes)
previous = result
return result
}
}
function mergeInto(list: AstNode[], node: AstNode, merged: Set<AstNode[]>, isRoot: boolean) {
let last = list[list.length - 1]
if (!last) {
list.push(node)
return
}
if (
isRoot &&
last.kind === 'at-rule' &&
node.kind === 'at-rule' &&
last.nodes.length === 0 &&
node.nodes.length === 0 &&
last.name === node.name &&
last.params === node.params
) {
return
}
if (
last.kind === 'at-rule' &&
node.kind === 'at-rule' &&
HOISTABLE_AT_RULES.has(node.name) &&
last.name === node.name &&
last.params === node.params &&
last.nodes.length > 0 &&
node.nodes.length > 0
) {
if (CACHEABLE.has(last)) {
last = { ...last, nodes: last.nodes.slice() }
list[list.length - 1] = last
}
for (let child of node.nodes) mergeInto(last.nodes, child, merged, false)
return
}
if (last.kind === 'rule' && node.kind === 'rule' && last.selector === node.selector) {
if (CACHEABLE.has(last)) {
last = { ...last, nodes: last.nodes.slice() }
list[list.length - 1] = last
}
last.nodes.push(...node.nodes)
merged.add(last.nodes)
return
}
list.push(node)
}
function dedupeDeclarations(nodes: AstNode[]) {
let seen = new Set<string>()
for (let i = nodes.length - 1; i >= 0; --i) {
let node = nodes[i]
if (node.kind !== 'declaration') continue
let id = `${node.property}\0${node.value}\0${node.important}`
if (seen.has(id)) nodes.splice(i, 1)
else seen.add(id)
}
}
function markCacheable(nodes: AstNode[]) {
for (let node of nodes) {
if (node.kind === 'rule' || node.kind === 'at-rule') {
CACHEABLE.add(node)
markCacheable(node.nodes)
}
}
}
function collectAtRootsAndAnimations(
nodes: AstNode[],
atRoots: AstNode[],
animations: string[],
inSpecialContext: boolean,
): boolean {
for (let node of nodes) {
if (node.kind === 'declaration') {
if (node.property === 'animation' && node.value != null) animations.push(node.value)
} else if (node.kind === 'at-root') {
if (inSpecialContext) return false
atRoots.push(node)
} else if (node.kind === 'context') {
if (node.context.reference) continue
if (node.context.theme || node.context.keyframes) return false
if (!collectAtRootsAndAnimations(node.nodes, atRoots, animations, inSpecialContext)) {
return false
}
} else if (node.kind === 'rule' || node.kind === 'at-rule') {
let special =
inSpecialContext ||
(node.kind === 'at-rule' &&
(node.name === '@keyframes' ||
(node.name === '@supports' && node.params.includes('color-mix('))))
if (!collectAtRootsAndAnimations(node.nodes, atRoots, animations, special)) return false
}
}
return true
}
function findWrappingAtRules(ast: AstNode[], target: AstNode): [string, string][] | null {
let found: [string, string][] | null = null
function visit(nodes: AstNode[], wrappers: [string, string][] | null): boolean {
for (let node of nodes) {
if (node === target) {
found = wrappers
return true
}
if (node.kind === 'at-rule') {
let supported =
wrappers !== null &&
HOISTABLE_AT_RULES.has(node.name) &&
node.name !== '@scope' &&
!node.params.includes('color-mix(')
let next: [string, string][] | null = supported
? [...wrappers!, [node.name, node.params]]
: null
if (visit(node.nodes, next)) return true
} else if (node.kind === 'context') {
let { theme, keyframes, reference, source } = node.context
let supported = wrappers !== null && !theme && !keyframes && !reference && !source
if (visit(node.nodes, supported ? wrappers : null)) return true
} else if (node.kind === 'rule' || node.kind === 'at-root') {
if (visit(node.nodes, null)) return true
}
}
return false
}
visit(ast, [])
return found
}
function findParentOfPlaceholder(nodes: AstNode[]): AstNode[] | null {
for (let node of nodes) {
if (node.kind === 'rule' && node.selector === PLACEHOLDER) return nodes
if (node.kind === 'rule' || node.kind === 'at-rule') {
let parent = findParentOfPlaceholder(node.nodes)
if (parent) return parent
}
}
return null
}