Skip to content

Commit

Permalink
hide implementation details in substituteAtApply itself
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Jan 7, 2025
1 parent 8c94d86 commit 9b80930
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 8 additions & 2 deletions packages/tailwindcss/src/apply.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Features } from '.'
import { toCss, walk, WalkAction, type AstNode } from './ast'
import { rule, toCss, walk, WalkAction, type AstNode } from './ast'
import { compileCandidates } from './compile'
import type { DesignSystem } from './design-system'
import { DefaultMap } from './utils/default-map'

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>()

Expand All @@ -17,7 +23,7 @@ export function substituteAtApply(ast: AstNode[], designSystem: DesignSystem) {
let definitions = new DefaultMap(() => new Set<AstNode>())

// Collect all new `@utility` definitions and all `@apply` rules first
walk(ast, (node, { parent }) => {
walk([root], (node, { parent }) => {
if (node.kind !== 'at-rule') return

// Do not allow `@apply` rules inside `@keyframes` rules.
Expand Down
10 changes: 4 additions & 6 deletions packages/tailwindcss/src/compat/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,8 @@ export function buildPluginApi(

designSystem.utilities.static(className, () => {
let clonedAst = structuredClone(ast)
let parent = rule('&', clonedAst)
featuresRef.current |= substituteAtApply([parent], designSystem)
return parent.nodes
featuresRef.current |= substituteAtApply(clonedAst, designSystem)
return clonedAst
})
}
},
Expand Down Expand Up @@ -391,9 +390,8 @@ export function buildPluginApi(
}

let ast = objectToAst(fn(value, { modifier }))
let parent = rule('&', ast)
featuresRef.current |= substituteAtApply([parent], designSystem)
return parent.nodes
featuresRef.current |= substituteAtApply(ast, designSystem)
return ast
}
}

Expand Down

0 comments on commit 9b80930

Please sign in to comment.