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