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
Next Next commit
Don’t break the build when encountering candidates with invalid `them…
…e()` calls

Co-authored-by: Adam Wathan <adam.wathan@gmail.com>
  • Loading branch information
thecrypticace and adamwathan committed Sep 17, 2024
commit fd2a122238bb79e5901e050d1c0d41e83b9d9637
17 changes: 17 additions & 0 deletions packages/tailwindcss/src/compile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { decl, rule, walk, WalkAction, type AstNode, type Rule } from './ast'
import { type Candidate, type Variant } from './candidate'
import { substituteFunctions } from './css-functions'
import { type DesignSystem } from './design-system'
import GLOBAL_PROPERTY_ORDER from './property-order'
import { asColor, type Utility } from './utilities'
Expand Down Expand Up @@ -40,6 +41,22 @@ export function compileCandidates(
let rules = designSystem.compileAstNodes(candidate)
if (rules.length === 0) continue

// Arbitrary values (`text-[theme(--color-red-500)]`) and arbitrary
// properties (`[--my-var:theme(--color-red-500)]`) can contain function
// calls so we need evaluate any functions we find there that weren't in
// the source CSS.
try {
substituteFunctions(
rules.map(({ node }) => node),
designSystem.resolveThemeValue,
)
} catch (err) {
// If substitution fails then the candidate likely contains a call to
// `theme()` that is invalid which may be because of incorrect usage,
// invalid arguments, or a theme key that does not exist.
continue
}

found = true

for (let { node, propertySort } of rules) {
Expand Down
11 changes: 11 additions & 0 deletions packages/tailwindcss/src/css-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,17 @@ describe('theme function', () => {
}"
`)
})

test('values that dont exist dont produce candidates', async () => {
expect(
await compileCss(
css`
@tailwind utilities;
`,
['rounded-[theme(i.do.not.exist)]', 'rounded-[theme(--i-do-not-exist)]'],
),
).toEqual('')
})
})

describe('in @media queries', () => {
Expand Down
6 changes: 0 additions & 6 deletions packages/tailwindcss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,6 @@ export async function compile(
return compiledCss
}

// Arbitrary values (`text-[theme(--color-red-500)]`) and arbitrary
// properties (`[--my-var:theme(--color-red-500)]`) can contain function
// calls so we need evaluate any functions we find there that weren't in
// the source CSS.
substituteFunctions(newNodes, designSystem.resolveThemeValue)

previousAstNodeCount = newNodes.length

tailwindUtilitiesNode.nodes = newNodes
Expand Down