Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .changelog/canonicalize-prefix-theme-lookup.md
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions packages/tailwindcss/src/canonicalize-candidates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/tailwindcss/src/canonicalize-candidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down