Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove feature flag
  • Loading branch information
philipp-spiess committed Feb 20, 2025
commit 2e4430f191e3bd3f5dc87ee5524c93b4a48883b6
89 changes: 43 additions & 46 deletions packages/tailwindcss/src/ast.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { parseAtRule } from './css-parser'
import type { DesignSystem } from './design-system'
import { enableRemoveUnusedThemeVariables } from './feature-flags'
import { Theme, ThemeOptions } from './theme'
import { DefaultMap } from './utils/default-map'
import { extractUsedVariables } from './utils/variables'
Expand Down Expand Up @@ -435,62 +434,60 @@ export function optimizeAst(
for (let node of ast) {
transform(node, newAst, {}, 0)
}
// Remove unused theme variables
if (enableRemoveUnusedThemeVariables) {
// Remove unused theme variables
next: for (let [parent, declarations] of cssThemeVariables) {
for (let declaration of declarations) {
// Find out if a variable is either used directly or if any of the
// variables referencing it is used.
let variableUsed = isVariableUsed(
declaration.property,
designSystem.theme,
variableDependencies,
)
if (variableUsed) {
if (declaration.property.startsWith('--animate-')) {
let parts = declaration.value!.split(/\s+/)
for (let part of parts) usedKeyframeNames.add(part)
}

continue
// Remove unused theme variables
next: for (let [parent, declarations] of cssThemeVariables) {
for (let declaration of declarations) {
// Find out if a variable is either used directly or if any of the
// variables referencing it is used.
let variableUsed = isVariableUsed(
declaration.property,
designSystem.theme,
variableDependencies,
)
if (variableUsed) {
if (declaration.property.startsWith('--animate-')) {
let parts = declaration.value!.split(/\s+/)
for (let part of parts) usedKeyframeNames.add(part)
}

// Remove the declaration (from its parent)
let idx = parent.indexOf(declaration)
parent.splice(idx, 1)
continue
}

// If the parent is now empty, remove it and any `@layer` rules above it
// from the AST
if (parent.length === 0) {
let path = findNode(newAst, (node) => node.kind === 'rule' && node.nodes === parent)
// Remove the declaration (from its parent)
let idx = parent.indexOf(declaration)
parent.splice(idx, 1)

if (!path || path.length === 0) continue next
// If the parent is now empty, remove it and any `@layer` rules above it
// from the AST
if (parent.length === 0) {
let path = findNode(newAst, (node) => node.kind === 'rule' && node.nodes === parent)

// Add the root of the AST so we can delete from the top-level
path.unshift({
kind: 'at-root',
nodes: newAst,
})
if (!path || path.length === 0) continue next

// Remove nodes from the parent as long as the parent is empty
// otherwise and consist of only @layer rules
do {
let nodeToRemove = path.pop()
if (!nodeToRemove) break
// Add the root of the AST so we can delete from the top-level
path.unshift({
kind: 'at-root',
nodes: newAst,
})

let removeFrom = path[path.length - 1]
if (!removeFrom) break
if (removeFrom.kind !== 'at-root' && removeFrom.kind !== 'at-rule') break
// Remove nodes from the parent as long as the parent is empty
// otherwise and consist of only @layer rules
do {
let nodeToRemove = path.pop()
if (!nodeToRemove) break

let idx = removeFrom.nodes.indexOf(nodeToRemove)
if (idx === -1) break
let removeFrom = path[path.length - 1]
if (!removeFrom) break
if (removeFrom.kind !== 'at-root' && removeFrom.kind !== 'at-rule') break

removeFrom.nodes.splice(idx, 1)
} while (true)
let idx = removeFrom.nodes.indexOf(nodeToRemove)
if (idx === -1) break

continue next
}
removeFrom.nodes.splice(idx, 1)
} while (true)

continue next
}
}

Expand Down
2 changes: 0 additions & 2 deletions packages/tailwindcss/src/feature-flags.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export const enableRemoveUnusedThemeVariables = process.env.FEATURES_ENV !== 'stable'

export const enableUserValid = process.env.FEATURES_ENV !== 'stable'