Skip to content

Canonicalization: keep the -- when dropping the theme prefix of a variable - #20765

Open
RobinMalfait wants to merge 2 commits into
mainfrom
robin/open-pr-keep-the-when-canonicalization-drops-the-theme-prefix
Open

RobinMalfait wants to merge 2 commits into
mainfrom
robin/open-pr-keep-the-when-canonicalization-drops-the-theme-prefix

Conversation

@RobinMalfait

Copy link
Copy Markdown
Contributor

This PR fixes a slow path in canonicalization when you use a theme prefix (e.g. @import 'tailwindcss' prefix(tw)).

resolveVariablesInValue drops the theme prefix from a variable before it looks up the value. The slice also dropped the leading --:

variable = variable.slice(`--${designSystem.theme.prefix}-`.length)
// `--tw-spacing` → `spacing`, not `--spacing`

For the compat layer, a path without -- is a legacy path (like colors.red.500). So the first such lookup called upgradeToFullPluginSupport, which replaces designSystem.resolveThemeValue with a version that goes through theme() → readFromCss → Theme.namespace() for each lookup after it.

The fix keeps the --:

variable = `--${variable.slice(`--${designSystem.theme.prefix}-`.length)}`

The names in seen and in the inlined theme variables of a signature now have the -- too. Those names are only compared with each other, so this changes nothing else.

History

The slice came in with 551bbed "add signatures" in #17831. #19171 moved the code, but kept the line as is. The code comment says only "The internal variable doesn't have the prefix", and no review comment or changelog entry mentions it, so the missing -- looks like an oversight. I found no issue or PR that reports this.

Output

The output stays the same. With the default theme, the bare key and the -- variable resolve to the same value for each of the 419 theme variables, so this is a performance fix only. To make sure, I compared the output before and after the fix:

  • 36 prefixed candidates (arbitrary values with var(--tw-…), collapses like tw:w-[1rem] tw:h-[1rem] → tw:size-4, etc.), with 6 theme setups (default, extra @theme values, @theme inline, @theme reference, @theme static, a custom --spacing): 216 results, all the same.
  • A setup with @config that extends the theme in JS (full plugin support is already on there): the same.
  • 207 classes from getClassList(), with collapse: true: the same.

Performance

Measured with the built dist, on a fresh design system with the full default theme and prefix(tw), Node 24, Apple M5 Pro. Each run is a new process.

Before After
canonicalizeCandidates(['tw:p-4']), first call (5 runs) 2385–2472 ms 757–775 ms
207 prefixed classes from getClassList(), collapse: true 16.5 s 7.0 s

Without a prefix, the first call takes 734–829 ms, so a prefix no longer adds a cost.

Test plan

  • Added a test in canonicalize-candidates.test.ts that canonicalizes prefixed candidates and checks that designSystem.resolveThemeValue is still the same function after it, so the compat layer did not switch to full plugin support. It fails without the fix (the output is the same), and passes with it.
  • pnpm vitest run in packages/tailwindcss: all tests pass.

`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 551bbed "add signatures", in #17831.
@RobinMalfait
RobinMalfait requested a review from a team as a code owner November 25, 2026 14:46
@RobinMalfait RobinMalfait self-assigned this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant