Skip to content

Commit 0e61002

Browse files
committed
ensure we re-print the candidate in case it didn't change
When printing a candidate, we do some optimizations already, such as: - `bg-[var(--foo)]` -> `bg-(--foo)` - `bg-[rgb(0,_0,_0)]` -> `bg-[rgb(0,0,0)]` Consistency in your project will reduce the file size. We parse and reprint the candidate if nothing changed during migrations because we don't have dedicated migrations for them. So (a lot) of these classes weren't fully updated to the v4 flavor of the classes. Luckily re-parsing the candidate is fast because we are re-using the design system which means that we have a cached version of the candidate.
1 parent ba4cab7 commit 0e61002

File tree

1 file changed

+15
-1
lines changed
  • packages/@tailwindcss-upgrade/src/codemods/template

1 file changed

+15
-1
lines changed

packages/@tailwindcss-upgrade/src/codemods/template/migrate.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import fs from 'node:fs/promises'
22
import path, { extname } from 'node:path'
3+
import { parseCandidate } from '../../../../tailwindcss/src/candidate'
34
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
45
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
56
import { spliceChangesIntoString, type StringChange } from '../../utils/splice-changes-into-string'
6-
import { extractRawCandidates } from './candidates'
7+
import { extractRawCandidates, printCandidate } from './candidates'
78
import { migrateArbitraryValueToBareValue } from './migrate-arbitrary-value-to-bare-value'
89
import { migrateAutomaticVarInjection } from './migrate-automatic-var-injection'
910
import { migrateBgGradient } from './migrate-bg-gradient'
@@ -56,9 +57,22 @@ export async function migrateCandidate(
5657
end: number
5758
},
5859
): Promise<string> {
60+
let original = rawCandidate
5961
for (let migration of DEFAULT_MIGRATIONS) {
6062
rawCandidate = await migration(designSystem, userConfig, rawCandidate, location)
6163
}
64+
65+
// If nothing changed, let's parse it again and re-print it. This will migrate
66+
// pretty print candidates to the new format. If it did change, we already had
67+
// to re-print it.
68+
//
69+
// E.g.: `bg-red-500/[var(--my-opacity)]` -> `bg-red-500/(--my-opacity)`
70+
if (rawCandidate === original) {
71+
for (let candidate of parseCandidate(rawCandidate, designSystem)) {
72+
return printCandidate(designSystem, candidate)
73+
}
74+
}
75+
6276
return rawCandidate
6377
}
6478

0 commit comments

Comments
 (0)