Skip to content
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

Template migrations: Add automatic var injection codemods #14526

Merged
merged 17 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add support for prefixes ([#14501](https://github.com/tailwindlabs/tailwindcss/pull/14501))
- _Experimental_: Add template codemods for removal of automatic `var(…)` injection ([#14526](https://github.com/tailwindlabs/tailwindcss/pull/14526))

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions integrations/upgrade/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test(
`,
'src/index.html': html`
<h1>🤠👋</h1>
<div class="!flex sm:!block"></div>
<div class="!flex sm:!block bg-[--my-red]"></div>
`,
'src/input.css': css`
@tailwind base;
Expand All @@ -35,7 +35,7 @@ test(
'src/index.html',
html`
<h1>🤠👋</h1>
<div class="flex! sm:block!"></div>
<div class="flex! sm:block! bg-[var(--my-red)]"></div>
`,
)

Expand Down
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)]'],
philipp-spiess marked this conversation as resolved.
Show resolved Hide resolved

// 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.
Show resolved Hide resolved

// Variants
['supports-[--test]:flex', 'supports-[var(--test)]:flex'],
['supports-[_--test]:flex', null],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if these are correct, because
supports-[--test] maps to @supports (--test: var(--tw)) which essentially tests whether the browser understands CSS variables.

But supports-[var(--test)] maps to @supports var(--test) which I think is invalid? (at least I can't get it to work in Safari with any value I throw at it)

https://play.tailwindcss.com/CC7NDpsJfg

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I got this from your list here: https://github.com/tailwindlabs/tailwindcss/pull/13657/files#diff-5c09a7c68a4426650ba1209e3be9652052750e24131acb607e09e5efea477dacR1619-R1624

supports-[var(--test)] mapping to @supports var(--test) seems to be at least compatible with v3 though 🙈

Screenshot 2024-09-26 at 18 19 27

Copy link
Member

@RobinMalfait RobinMalfait Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I see. While this is compatible with v3, it's still wrong (aka will never match). So the question is:

  1. Do we want to fix it (could be a breaking change since it's already broken right now)
  2. Do we get rid of the supports variant in this case (or the candidate entirely because it won't do anything anyway)
  3. Do we do what v4 expects, where it maps to --test: var(--tw) (which essentially is the fix from 1.)
  4. Don't do anything

])('%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
}
3 changes: 2 additions & 1 deletion packages/@tailwindcss-upgrade/src/template/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import path from 'node:path'
import type { Candidate } from '../../../tailwindcss/src/candidate'
import type { DesignSystem } from '../../../tailwindcss/src/design-system'
import { extractCandidates, printCandidate, replaceCandidateInContent } from './candidates'
import { automaticVarInjection } from './codemods/automatic-var-injection'
import { migrateImportant } from './codemods/migrate-important'

export type Migration = (candidate: Candidate) => Candidate | null

export default async function migrateContents(
designSystem: DesignSystem,
contents: string,
migrations: Migration[] = [migrateImportant],
migrations: Migration[] = [migrateImportant, automaticVarInjection],
): Promise<string> {
let candidates = await extractCandidates(designSystem, contents)

Expand Down