Skip to content

Ensure @tailwindcss/upgrade runs on Tailwind CSS v4 projects and is idempotent #17717

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

Merged
merged 36 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e326b54
add semver dependency
RobinMalfait Mar 31, 2025
83bf845
do not require Tailwind CSS v3 projects anymore
RobinMalfait Apr 1, 2025
2879f4e
add `version` related helpers
RobinMalfait Apr 18, 2025
ba315ec
conditionally apply migrations based on version number
RobinMalfait Apr 18, 2025
0f97d3e
only link stylesheets to JS config files when migrating Tailwind CSS …
RobinMalfait Apr 18, 2025
6bef56e
only split stylesheets when migrating Tailwind CSS v3 projects
RobinMalfait Apr 18, 2025
986092a
only migrate PostCSS config when migrating Tailwind CSS v3 projects
RobinMalfait Apr 18, 2025
cfaeb55
only migrate JS files if they have linked config paths
RobinMalfait Apr 18, 2025
2f6e368
make `jsConfigMigration` nullable
RobinMalfait Apr 18, 2025
f78f582
change order
RobinMalfait Apr 18, 2025
b6089e4
bail on empty config
RobinMalfait Apr 18, 2025
a2836e5
make `UserConfig` nullable
RobinMalfait Apr 18, 2025
c52201f
migrate source files based on Tailwind root stylesheets
RobinMalfait Apr 18, 2025
eacaabf
remove unused `hoistStaticGlobParts`
RobinMalfait Apr 18, 2025
9be7647
make `configFilePath` also nullable
RobinMalfait Apr 18, 2025
bc93e2c
do not migrate `preflight` in non-v3 projects
RobinMalfait Apr 18, 2025
51b2200
run prettier
RobinMalfait Apr 18, 2025
6a492da
upgrade `tailwindcss` after we migrated the stylesheets
RobinMalfait Apr 18, 2025
c5ec584
remove test that requires Tailwind CSS v3
RobinMalfait Apr 18, 2025
71f3461
make the `DesignSystem` fully nullable
RobinMalfait Apr 18, 2025
6ffa7cb
mock version in local tests
RobinMalfait Apr 18, 2025
4f987fa
only migrate `@layer` in v3 projects
RobinMalfait Apr 18, 2025
1b53461
add migrations for newly deprecated classes
RobinMalfait Apr 18, 2025
6085b47
add `:user-valid` and `:user-invalid` arbitrary variant replacements
RobinMalfait Apr 18, 2025
a6d4309
replace arbitrary `@media` variants
RobinMalfait Apr 18, 2025
5857fd0
handle `[@media_not(…)]` variants
RobinMalfait Apr 18, 2025
ba4cab7
handle `@media` with single argument (e.g.: `@media print`)
RobinMalfait Apr 18, 2025
0e61002
ensure we re-print the candidate in case it didn't change
RobinMalfait Apr 18, 2025
37323c6
add tests to ensure upgrade tool runs on v4 and is idempotent
RobinMalfait Apr 18, 2025
aadc926
add test to ensure upgrade runs on v4 projects
RobinMalfait Apr 18, 2025
d20902f
only commit changes in dirty git repo
RobinMalfait Apr 19, 2025
94ef122
Merge branch 'main' into feat/enable-codemods-on-v4-projects
RobinMalfait Apr 22, 2025
7bbeb99
Merge branch 'main' into feat/enable-codemods-on-v4-projects
RobinMalfait Apr 22, 2025
3d8eed9
adjust comment
RobinMalfait Apr 22, 2025
fff3cc0
update changelog
RobinMalfait Apr 22, 2025
6b5935c
add new migration examples to upgrade test
RobinMalfait Apr 22, 2025
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
Prev Previous commit
Next Next commit
conditionally apply migrations based on version number
  • Loading branch information
RobinMalfait committed Apr 22, 2025
commit ba315ec1ddc98acb0a96b35a05ce94290ad144c1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Candidate } from '../../../../tailwindcss/src/candidate'
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
import { DefaultMap } from '../../../../tailwindcss/src/utils/default-map'
import * as version from '../../utils/version'
import { printCandidate } from './candidates'
import { isSafeMigration } from './is-safe-migration'

Expand Down Expand Up @@ -75,6 +76,15 @@ export async function migrateLegacyClasses(
end: number
},
): Promise<string> {
// These migrations are only safe when migrating from v3 to v4.
//
// Migrating from `rounded` to `rounded-sm` once is fine (v3 -> v4). But if we
// migrate again (v4 -> v4), then `rounded-sm` would be migrated to
// `rounded-xs` which is incorrect because we already migrated this.
if (!version.isMajor(3)) {
return rawCandidate
}

let defaultDesignSystem = await DESIGN_SYSTEMS.get(__dirname)

function* migrate(rawCandidate: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
import * as version from '../../utils/version'
import { printCandidate } from './candidates'

// Classes that used to exist in Tailwind CSS v3, but do not exist in Tailwind
// CSS v4 anymore.
const LEGACY_CLASS_MAP = {
const LEGACY_CLASS_MAP: Record<string, string> = {
'overflow-ellipsis': 'text-ellipsis',

'flex-grow': 'grow',
Expand All @@ -14,8 +15,15 @@ const LEGACY_CLASS_MAP = {

'decoration-clone': 'box-decoration-clone',
'decoration-slice': 'box-decoration-slice',
}

'outline-none': 'outline-hidden',
// `outline-none` in v3 has the same meaning as `outline-hidden` in v4. However,
// `outline-none` in v4 _also_ exists but has a different meaning.
//
// We can only migrate `outline-none` to `outline-hidden` if we are migrating a
// v3 project to v4.
if (version.isMajor(3)) {
LEGACY_CLASS_MAP['outline-none'] = 'outline-hidden'
}

let seenDesignSystems = new WeakSet<DesignSystem>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ import { walk, type AstNode } from '../../../../tailwindcss/src/ast'
import { type Variant } from '../../../../tailwindcss/src/candidate'
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
import * as version from '../../utils/version'
import { printCandidate } from './candidates'

export function migrateVariantOrder(
designSystem: DesignSystem,
_userConfig: Config,
rawCandidate: string,
): string {
// This migration is only needed for Tailwind CSS v3
//
// Changing the variant order when migrating from v3 to v4 is fine, but
// migrating v4 to v4 would make it unsafe because the variant order would
// flip-flop every time you run the migration.
if (!version.isMajor(3)) {
return rawCandidate
}

for (let candidate of designSystem.parseCandidate(rawCandidate)) {
if (candidate.variants.length <= 1) {
continue
Expand Down