Skip to content

Commit eec1bf2

Browse files
Fix --theme(…) function when legacy JS plugins are used (#17458)
Closes #17346 This PR fixes an issue that caused the `--theme(…)` function to behave differently after a legacy JS plugin or config was configured. The issue was that the compatibility layer would patch the theme value resolver to always inline the value. This, however, is only expected to happen if the path does not look like a CSS variable in which case this legacy code path should not be run. To fix this, I'm now keeping a reference to the regular theme resolution function and call into it if the path starts with `--`. ## Test plan - Tested with the repro in #17346 by adding pnpm overrides and confirmed that this fixes the issue - Added a unit test to the `--theme(…)` resolution tests
1 parent c32b608 commit eec1bf2

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3535
- Extract special `@("@")md:…` syntax in Razor files ([#17427](https://github.com/tailwindlabs/tailwindcss/pull/17427))
3636
- Disallow arbitrary values with top-level braces and semicolons as well as unbalanced parentheses and brackets ([#17361](https://github.com/tailwindlabs/tailwindcss/pull/17361))
3737
- Extract used CSS variables from `.css` files ([#17433](https://github.com/tailwindlabs/tailwindcss/pull/17433))
38+
- Ensure the `--theme(…)` function still resolves to the CSS variables even when legacy JS plugins are enabled
3839

3940
### Changed
4041

packages/tailwindcss/src/compat/apply-compat-hooks.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,13 @@ function upgradeToFullPluginSupport({
260260
// config files are actually being used. In the future we may want to optimize
261261
// this further by only doing this if plugins or config files _actually_
262262
// registered JS config objects.
263+
let defaultResolveThemeValue = designSystem.resolveThemeValue
263264
designSystem.resolveThemeValue = function resolveThemeValue(path: string, forceInline?: boolean) {
264-
let resolvedValue = pluginApi.theme(path, forceInline)
265+
if (path[0] === '-' && path[1] === '-') {
266+
return defaultResolveThemeValue(path, forceInline)
267+
}
268+
269+
let resolvedValue = pluginApi.theme(path, undefined)
265270

266271
if (Array.isArray(resolvedValue) && resolvedValue.length === 2) {
267272
// When a tuple is returned, return the first element

packages/tailwindcss/src/css-functions.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,48 @@ describe('--theme(…)', () => {
384384
`[Error: Could not resolve value for theme function: \`theme(--color-green-500)\`. Consider checking if the variable name is correct or provide a fallback value to silence this error.]`,
385385
)
386386
})
387+
388+
test('--theme(…) function still works as expected, even when a plugin is imported', async () => {
389+
expect(
390+
await compileCss(
391+
css`
392+
@layer base {
393+
html,
394+
:host {
395+
font-family: --theme(--default-font-family, system-ui);
396+
}
397+
}
398+
@layer theme {
399+
@theme {
400+
--font-sans: sans-serif;
401+
--default-font-family: --theme(--font-sans, initial);
402+
}
403+
}
404+
@plugin "my-plugin.js";
405+
`,
406+
[],
407+
{
408+
loadModule: async () => ({
409+
module: () => {},
410+
base: '/root',
411+
}),
412+
},
413+
),
414+
).toMatchInlineSnapshot(`
415+
"@layer base {
416+
html, :host {
417+
font-family: var(--default-font-family, system-ui);
418+
}
419+
}
420+
421+
@layer theme {
422+
:root, :host {
423+
--font-sans: sans-serif;
424+
--default-font-family: var(--font-sans);
425+
}
426+
}"
427+
`)
428+
})
387429
})
388430

389431
describe('theme(…)', () => {

0 commit comments

Comments
 (0)