-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…14748) This PR migrates the `@variants` and `@responsive` directives. In Tailwind CSS v2, these were used to generate certain variants of responsive variants for the give classes. In Tailwind CSS v3, these still worked but were implemented as a no-op such that these directives don't end up in your final CSS. In Tailwind CSS v4, these don't exist at all anymore, so we can safely get rid of them by replacing them with their contents. Input: ```css @Variants hover, focus { .foo { color: red; } } @Responsive { .bar { color: blue; } } ``` Output: ```css .foo { color: red; } .bar { color: blue; } ```
- Loading branch information
1 parent
d59f1b3
commit 5bf2efb
Showing
7 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/@tailwindcss-upgrade/src/codemods/migrate-variants-directive.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import dedent from 'dedent' | ||
import postcss from 'postcss' | ||
import { expect, it } from 'vitest' | ||
import { formatNodes } from './format-nodes' | ||
import { migrateVariantsDirective } from './migrate-variants-directive' | ||
|
||
const css = dedent | ||
|
||
function migrate(input: string) { | ||
return postcss() | ||
.use(migrateVariantsDirective()) | ||
.use(formatNodes()) | ||
.process(input, { from: expect.getState().testPath }) | ||
.then((result) => result.css) | ||
} | ||
|
||
it('should replace `@variants` with `@layer utilities`', async () => { | ||
expect( | ||
await migrate(css` | ||
@variants hover, focus { | ||
.foo { | ||
color: red; | ||
} | ||
} | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"@layer utilities { | ||
.foo { | ||
color: red; | ||
} | ||
}" | ||
`) | ||
}) |
35 changes: 35 additions & 0 deletions
35
packages/@tailwindcss-upgrade/src/codemods/migrate-variants-directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { type Plugin, type Root } from 'postcss' | ||
|
||
export function migrateVariantsDirective(): Plugin { | ||
function migrate(root: Root) { | ||
root.walkAtRules('variants', (node) => { | ||
// Migrate `@variants` to `@utility` because `@variants` make the classes | ||
// an actual utility. | ||
// ```css | ||
// @variants hover { | ||
// .foo {} | ||
// } | ||
// ``` | ||
// | ||
// Means that you can do this in your HTML: | ||
// ```html | ||
// <div class="focus:foo"></div> | ||
// ``` | ||
// | ||
// Notice the `focus:`, even though we _only_ configured the `hover` | ||
// variant. | ||
// | ||
// This means that we can convert it to an `@layer utilities` rule. Later, | ||
// this will get converted to an `@utility` rule. | ||
if (node.name === 'variants') { | ||
node.name = 'layer' | ||
node.params = 'utilities' | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
postcssPlugin: '@tailwindcss/upgrade/migrate-variants-directive', | ||
OnceExit: migrate, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters