From f9e4dcb76addbb72ae716f6e34aceb88bbae67aa Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 25 Nov 2026 15:45:32 +0100 Subject: [PATCH 1/2] Keep the `--` when canonicalization drops the theme prefix `resolveVariablesInValue` drops the theme prefix from a variable before it looks up its value. The slice also dropped the leading `--`, so with `@theme prefix(tw)` it looked up `spacing` instead of `--spacing`. A path without `--` is a legacy path for the compat layer, so the first such lookup switched the design system to full plugin support. That replaces `resolveThemeValue` with a slower version that goes through `theme()` for each lookup after it. The output stayed the same, but the first canonicalization of `tw:p-4` on a fresh design system with the default theme took ~2.4s instead of ~0.77s. The slice came in with 551bbeda6 "add signatures", in https://github.com/tailwindlabs/tailwindcss/pull/17831. --- .../src/canonicalize-candidates.test.ts | 34 +++++++++++++++++++ .../src/canonicalize-candidates.ts | 9 +++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/packages/tailwindcss/src/canonicalize-candidates.test.ts b/packages/tailwindcss/src/canonicalize-candidates.test.ts index 80c9676d16f8..d857f328fe8a 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.test.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.test.ts @@ -2379,6 +2379,40 @@ describe('regressions', () => { let countAfter = keysSpy.mock.calls.length expect(countAfter).toBe(countBefore) }) + + // Dropping the theme prefix from `--tw-spacing` has to give `--spacing`, not + // `spacing`. A bare key makes the compat layer switch the design system to + // full plugin support, which replaces `resolveThemeValue` and makes each + // later lookup a lot slower. + test('does not switch to full plugin support when canonicalizing prefixed candidates', async () => { + let designSystem = await __unstable__loadDesignSystem( + css` + @tailwind utilities; + @theme prefix(tw) { + --spacing: 0.25rem; + --color-red-500: oklch(63.7% 0.237 25.331); + } + `, + { base: __dirname }, + ) + + let resolveThemeValue = designSystem.resolveThemeValue + + expect( + designSystem.canonicalizeCandidates( + [ + 'tw:p-4', + 'tw:[margin-top:0.25rem]', + 'tw:w-[1rem]', + 'tw:h-[1rem]', + 'tw:bg-[var(--tw-color-red-500)]', + ], + { rem: 16, collapse: true }, + ), + ).toEqual(['tw:p-4', 'tw:mt-1', 'tw:bg-red-500', 'tw:size-4']) + + expect(designSystem.resolveThemeValue).toBe(resolveThemeValue) + }) }) describe('normalizeEscapes', () => { diff --git a/packages/tailwindcss/src/canonicalize-candidates.ts b/packages/tailwindcss/src/canonicalize-candidates.ts index 7a55a949634f..d7d0c90d65d3 100644 --- a/packages/tailwindcss/src/canonicalize-candidates.ts +++ b/packages/tailwindcss/src/canonicalize-candidates.ts @@ -3193,9 +3193,14 @@ function resolveVariablesInValue( let variable = valueNode.nodes[0].value // Drop the prefix from the variable name if it is present. The - // internal variable doesn't have the prefix. + // internal variable doesn't have the prefix, e.g.: `--tw-spacing` becomes + // `--spacing`. + // + // Keep the leading `--`. Without it, `resolveThemeValue` sees a legacy + // path like `spacing`, and switches the design system to full plugin + // support, which makes each later lookup a lot slower. if (designSystem.theme.prefix && variable.startsWith(`--${designSystem.theme.prefix}-`)) { - variable = variable.slice(`--${designSystem.theme.prefix}-`.length) + variable = `--${variable.slice(`--${designSystem.theme.prefix}-`.length)}` } let variableValue = designSystem.resolveThemeValue(variable) // Prevent infinite recursion when the variable value contains the From e1c7e709201f7958b6e92f29fff53f87ab7cc732 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 25 Nov 2026 15:45:32 +0100 Subject: [PATCH 2/2] Add changelog entry --- .changelog/canonicalize-prefix-theme-lookup.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/canonicalize-prefix-theme-lookup.md diff --git a/.changelog/canonicalize-prefix-theme-lookup.md b/.changelog/canonicalize-prefix-theme-lookup.md new file mode 100644 index 000000000000..9fe7f885dc27 --- /dev/null +++ b/.changelog/canonicalize-prefix-theme-lookup.md @@ -0,0 +1,3 @@ +### Fixed + +- Canonicalization: improve performance when using a theme prefix (e.g. `@import 'tailwindcss' prefix(tw)`), by no longer switching to the slower compatibility lookup for theme values