substituteAtApply() — tailwindcss Function Reference
Architecture documentation for the substituteAtApply() function in apply.ts from the tailwindcss codebase.
Entity Profile
Dependency Diagram
graph TD 7ad9d996_c0ff_47f5_d131_ab2ead06506e["substituteAtApply()"] 1721da0c_9e1d_5bee_ab0a_a192cfa6640d["apply.ts"] 7ad9d996_c0ff_47f5_d131_ab2ead06506e -->|defined in| 1721da0c_9e1d_5bee_ab0a_a192cfa6640d 6e657054_f39b_c09e_18b9_3bc9c9e15d42["createUtilitySignatureCache()"] 6e657054_f39b_c09e_18b9_3bc9c9e15d42 -->|calls| 7ad9d996_c0ff_47f5_d131_ab2ead06506e 975d8b8a_4b03_4408_cd48_2cea8a20646c["createVariantSignatureCache()"] 975d8b8a_4b03_4408_cd48_2cea8a20646c -->|calls| 7ad9d996_c0ff_47f5_d131_ab2ead06506e 31653e23_464a_3652_4a48_0c82332a92c4["buildPluginApi()"] 31653e23_464a_3652_4a48_0c82332a92c4 -->|calls| 7ad9d996_c0ff_47f5_d131_ab2ead06506e f7f9b3da_5977_1aa6_3bcb_bfc607af4e8f["parseCss()"] f7f9b3da_5977_1aa6_3bcb_bfc607af4e8f -->|calls| 7ad9d996_c0ff_47f5_d131_ab2ead06506e 085cf56e_8188_afb1_04da_5ccd0fb7babc["rule()"] 7ad9d996_c0ff_47f5_d131_ab2ead06506e -->|calls| 085cf56e_8188_afb1_04da_5ccd0fb7babc 4982d9ce_98d4_85d9_44af_7cc47b93c482["walk()"] 7ad9d996_c0ff_47f5_d131_ab2ead06506e -->|calls| 4982d9ce_98d4_85d9_44af_7cc47b93c482 2820372c_b982_9e06_fc23_f8f4ac308d00["get()"] 7ad9d996_c0ff_47f5_d131_ab2ead06506e -->|calls| 2820372c_b982_9e06_fc23_f8f4ac308d00 4753b67f_383f_d178_f10f_3bf30b958d9c["resolveApplyDependencies()"] 7ad9d996_c0ff_47f5_d131_ab2ead06506e -->|calls| 4753b67f_383f_d178_f10f_3bf30b958d9c 9b49f3c6_0c8d_5c62_965c_30a1db6499f8["toCss()"] 7ad9d996_c0ff_47f5_d131_ab2ead06506e -->|calls| 9b49f3c6_0c8d_5c62_965c_30a1db6499f8 ad08c258_e5c2_4cd4_c935_0925a940458e["compileCandidates()"] 7ad9d996_c0ff_47f5_d131_ab2ead06506e -->|calls| ad08c258_e5c2_4cd4_c935_0925a940458e c58cbb33_f3cc_0b4f_844a_15bf66a1dc61["segment()"] 7ad9d996_c0ff_47f5_d131_ab2ead06506e -->|calls| c58cbb33_f3cc_0b4f_844a_15bf66a1dc61 dbd80fa4_4b36_fb8e_fb6e_f2aec789aa6b["cloneAstNode()"] 7ad9d996_c0ff_47f5_d131_ab2ead06506e -->|calls| dbd80fa4_4b36_fb8e_fb6e_f2aec789aa6b style 7ad9d996_c0ff_47f5_d131_ab2ead06506e fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/tailwindcss/src/apply.ts lines 10–300
export function substituteAtApply(ast: AstNode[], designSystem: DesignSystem) {
let features = Features.None
// Wrap the whole AST in a root rule to make sure there is always a parent
// available for `@apply` at-rules. In some cases, the incoming `ast` just
// contains `@apply` at-rules which means that there is no proper parent to
// rely on.
let root = rule('&', ast)
// Track all nodes containing `@apply`
let parents = new Set<AstNode>()
// Track all the dependencies of an `AstNode`
let dependencies = new DefaultMap<AstNode, Set<string>>(() => new Set<string>())
// Track all `@utility` definitions by its root (name)
let definitions = new DefaultMap(() => new Set<AstNode>())
// Collect all new `@utility` definitions and all `@apply` rules first
walk([root], (node, ctx) => {
if (node.kind !== 'at-rule') return
// Do not allow `@apply` rules inside `@keyframes` rules.
if (node.name === '@keyframes') {
walk(node.nodes, (child) => {
if (child.kind === 'at-rule' && child.name === '@apply') {
throw new Error(`You cannot use \`@apply\` inside \`@keyframes\`.`)
}
})
return WalkAction.Skip
}
// `@utility` defines a utility, which is important information in order to
// do a correct topological sort later on.
if (node.name === '@utility') {
let name = node.params.replace(/-\*$/, '')
definitions.get(name).add(node)
// In case `@apply` rules are used inside `@utility` rules.
walk(node.nodes, (child) => {
if (child.kind !== 'at-rule' || child.name !== '@apply') return
parents.add(node)
for (let dependency of resolveApplyDependencies(child, designSystem)) {
dependencies.get(node).add(dependency)
}
})
return
}
// Any other `@apply` node.
if (node.name === '@apply') {
// `@apply` cannot be top-level, so we need to have a parent such that we
// can replace the `@apply` node with the actual utility classes later.
if (ctx.parent === null) return
features |= Features.AtApply
parents.add(ctx.parent)
for (let dependency of resolveApplyDependencies(node, designSystem)) {
// Mark every parent in the path as having a dependency to that utility.
for (let parent of ctx.path()) {
if (!parents.has(parent)) continue
dependencies.get(parent).add(dependency)
}
}
}
})
// Topological sort before substituting `@apply`
let seen = new Set<AstNode>()
let sorted: AstNode[] = []
let wip = new Set<AstNode>()
function visit(node: AstNode, path: AstNode[] = []) {
if (seen.has(node)) {
return
}
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does substituteAtApply() do?
substituteAtApply() is a function in the tailwindcss codebase, defined in packages/tailwindcss/src/apply.ts.
Where is substituteAtApply() defined?
substituteAtApply() is defined in packages/tailwindcss/src/apply.ts at line 10.
What does substituteAtApply() call?
substituteAtApply() calls 8 function(s): cloneAstNode, compileCandidates, get, resolveApplyDependencies, rule, segment, toCss, walk.
What calls substituteAtApply()?
substituteAtApply() is called by 4 function(s): buildPluginApi, createUtilitySignatureCache, createVariantSignatureCache, parseCss.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free