Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add new `in-*` variant ([#15025](https://github.com/tailwindlabs/tailwindcss/pull/15025))
- _Upgrade (experimental)_: Migrate `[&>*]` to the `*` variant ([#15022](https://github.com/tailwindlabs/tailwindcss/pull/15022))
- _Upgrade (experimental)_: Migrate `[&_*]` to the `**` variant ([#15022](https://github.com/tailwindlabs/tailwindcss/pull/15022))
- _Upgrade (experimental)_: Warn when trying to migrating a project that is not on Tailwind CSS v3 ([#15015](https://github.com/tailwindlabs/tailwindcss/pull/15015))

### Fixed

Expand Down
35 changes: 35 additions & 0 deletions integrations/upgrade/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ test(
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -2463,3 +2464,37 @@ test(
`)
},
)

test(
'requires Tailwind v3 before attempting an upgrade',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/upgrade": "workspace:^"
}
}
`,
'tailwind.config.ts': js` export default {} `,
'src/index.html': html`
<div class="underline"></div>
`,
'src/index.css': css`
@tailwind base;
@tailwind components;
@tailwind utilities;
`,
},
},
async ({ exec }) => {
let output = await exec('npx @tailwindcss/upgrade', {}, { ignoreStdErr: true }).catch((e) =>
e.toString(),
)

expect(output).toMatch(
/Tailwind CSS v.* found. The migration tool can only be run on v3 projects./,
)
},
)
16 changes: 16 additions & 0 deletions integrations/upgrade/js-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test(
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -297,6 +298,7 @@ test(
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/typography": "^0.5.15",
"@tailwindcss/upgrade": "workspace:^"
}
Expand Down Expand Up @@ -404,6 +406,7 @@ test(
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -485,6 +488,7 @@ test(
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -572,6 +576,7 @@ test(
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -640,6 +645,7 @@ test(
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -718,6 +724,7 @@ test(
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -838,6 +845,7 @@ test(
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -915,6 +923,7 @@ describe('border compatibility', () => {
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -973,6 +982,7 @@ describe('border compatibility', () => {
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -1034,6 +1044,7 @@ describe('border compatibility', () => {
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -1077,6 +1088,7 @@ describe('border compatibility', () => {
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -1136,6 +1148,7 @@ describe('border compatibility', () => {
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -1219,6 +1232,7 @@ describe('border compatibility', () => {
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -1330,6 +1344,7 @@ describe('border compatibility', () => {
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down Expand Up @@ -1443,6 +1458,7 @@ describe('border compatibility', () => {
'package.json': json`
{
"dependencies": {
"tailwindcss": "^3",
"@tailwindcss/upgrade": "workspace:^"
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/@tailwindcss-upgrade/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { prepareConfig } from './template/prepare-config'
import { args, type Arg } from './utils/args'
import { isRepoDirty } from './utils/git'
import { hoistStaticGlobParts } from './utils/hoist-static-glob-parts'
import { getPackageVersion } from './utils/package-version'
import { pkg } from './utils/packages'
import { eprintln, error, header, highlight, info, relative, success } from './utils/renderer'

Expand Down Expand Up @@ -50,6 +51,7 @@ async function run() {
let cleanup: (() => void)[] = []

if (!flags['--force']) {
// Require a clean git directory
if (isRepoDirty()) {
error('Git directory is not clean. Please stash or commit your changes before migrating.')
info(
Expand All @@ -59,6 +61,15 @@ async function run() {
}
}

// Require an installed `tailwindcss` version < 4
let tailwindVersion = await getPackageVersion('tailwindcss', base)
if (tailwindVersion && Number(tailwindVersion.split('.')[0]) !== 3) {
error(
`Tailwind CSS v${tailwindVersion} found. The migration tool can only be run on v3 projects.`,
)
process.exit(1)
}

{
// Stylesheet migrations

Expand Down
18 changes: 18 additions & 0 deletions packages/@tailwindcss-upgrade/src/utils/package-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from 'node:fs/promises'
import { createRequire } from 'node:module'

const localResolve = createRequire(import.meta.url).resolve

/**
* Resolves the version string of an npm dependency installed in the based
* directory.
*/
export async function getPackageVersion(pkg: string, base: string): Promise<string | null> {
try {
let packageJson = localResolve(`${pkg}/package.json`, { paths: [base] })
let { version } = JSON.parse(await fs.readFile(packageJson, 'utf8'))
return version
} catch {
return null
}
}