-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Template migrations: Add automatic var injection codemods #14526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
b378d29
c2b28ac
821c816
6674b67
ae04af1
6135579
5522f31
514c13c
0679e8a
ebacd7c
e72c333
947eccd
dd686fb
897c8b2
80f6719
0192f66
0687077
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { __unstable__loadDesignSystem } from '@tailwindcss/node' | ||
| import { expect, test } from 'vitest' | ||
| import { printCandidate } from '../candidates' | ||
| import { automaticVarInjection } from './automatic-var-injection' | ||
|
|
||
| test.each([ | ||
| // Arbitrary candidates | ||
| ['[color:--my-color]', '[color:var(--my-color)]'], | ||
|
|
||
| // Arbitrary values for functional candidates | ||
| ['bg-[--my-color]', 'bg-[var(--my-color)]'], | ||
| ['bg-[color:--my-color]', 'bg-[color:var(--my-color)]'], | ||
| ['border-[length:--my-length]', 'border-[length:var(--my-length)]'], | ||
| ['border-[line-width:--my-width]', 'border-[line-width:var(--my-width)]'], | ||
|
|
||
| // Does not add var() if there is a _ before the variable name | ||
| ['bg-[_--my-color]', null], | ||
| ['bg-[color:_--my-color]', null], | ||
| ['border-[length:_--my-length]', null], | ||
| ['border-[line-width:_--my-width]', null], | ||
|
|
||
| // Modifiers | ||
| ['[color:--my-color]/[--my-opacity]', '[color:var(--my-color)]/[var(--my-opacity)]'], | ||
| ['bg-red-500/[--my-opacity]', 'bg-red-500/[var(--my-opacity)]'], | ||
| ['bg-[--my-color]/[--my-opacity]', 'bg-[var(--my-color)]/[var(--my-opacity)]'], | ||
| ['bg-[color:--my-color]/[--my-opacity]', 'bg-[color:var(--my-color)]/[var(--my-opacity)]'], | ||
|
|
||
| ['[color:--my-color]/[_--my-opacity]', '[color:var(--my-color)]/[_--my-opacity]'], | ||
| ['bg-red-500/[_--my-opacity]', null], | ||
| ['bg-[--my-color]/[_--my-opacity]', 'bg-[var(--my-color)]/[_--my-opacity]'], | ||
| ['bg-[color:--my-color]/[_--my-opacity]', 'bg-[color:var(--my-color)]/[_--my-opacity]'], | ||
philipp-spiess marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Variants | ||
| ['supports-[--test]:flex', 'supports-[var(--test)]:flex'], | ||
| ['supports-[_--test]:flex', null], | ||
|
||
| ])('%s => %s', async (candidate, result) => { | ||
| let designSystem = await __unstable__loadDesignSystem('@import "tailwindcss";', { | ||
| base: __dirname, | ||
| }) | ||
|
|
||
| let migrated = automaticVarInjection(designSystem.parseCandidate(candidate)[0]!) | ||
| expect(migrated ? printCandidate(migrated) : migrated).toEqual(result) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import type { Candidate, Variant } from '../../../../tailwindcss/src/candidate' | ||
|
|
||
| export function automaticVarInjection(candidate: Candidate): Candidate | null { | ||
| let didChange = false | ||
|
|
||
| // Add `var(…)` in modifier position, e.g.: | ||
| // | ||
| // `bg-red-500/[--my-opacity]` => `bg-red-500/[var(--my-opacity)]` | ||
| if ( | ||
| 'modifier' in candidate && | ||
| candidate.modifier?.kind === 'arbitrary' && | ||
| candidate.modifier.value.startsWith('--') | ||
| ) { | ||
| candidate.modifier.value = `var(${candidate.modifier.value})` | ||
| didChange = true | ||
| } | ||
|
|
||
| // Add `var(…)` to all variants, e.g.: | ||
| // | ||
| // `supports-[--test]:flex'` => `supports-[var(--test)]:flex` | ||
| for (let variant of candidate.variants) { | ||
| let didChangeVariant = injectVarIntoVariant(variant) | ||
| if (didChangeVariant) { | ||
| didChange = true | ||
| } | ||
| } | ||
|
|
||
| // Add `var(…)` to arbitrary candidates, e.g.: | ||
| // | ||
| // `[color:--my-color]` => `[color:var(--my-color)]` | ||
| if (candidate.kind === 'arbitrary' && candidate.value.startsWith('--')) { | ||
| candidate.value = `var(${candidate.value})` | ||
| didChange = true | ||
| } | ||
|
|
||
| // Add `var(…)` to arbitrary values for functional candidates, e.g.: | ||
| // | ||
| // `bg-[--my-color]` => `bg-[var(--my-color)]` | ||
| if ( | ||
| candidate.kind === 'functional' && | ||
| candidate.value && | ||
| candidate.value.kind === 'arbitrary' && | ||
| candidate.value.value.startsWith('--') | ||
| ) { | ||
| candidate.value.value = `var(${candidate.value.value})` | ||
| didChange = true | ||
| } | ||
|
|
||
| if (didChange) { | ||
| return candidate | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| function injectVarIntoVariant(variant: Variant): boolean { | ||
| let didChange = false | ||
| if ( | ||
| variant.kind === 'functional' && | ||
| variant.value && | ||
| variant.value.kind === 'arbitrary' && | ||
| variant.value.value.startsWith('--') | ||
| ) { | ||
| variant.value.value = `var(${variant.value.value})` | ||
| didChange = true | ||
| } | ||
|
|
||
| if (variant.kind === 'compound') { | ||
| let compoundDidChange = injectVarIntoVariant(variant.variant) | ||
| if (compoundDidChange) { | ||
| didChange = true | ||
| } | ||
| } | ||
|
|
||
| return didChange | ||
| } |

Uh oh!
There was an error while loading. Please reload this page.