Skip to content

Commit 329126b

Browse files
committed
migrate @variants and @responsive
They only do something in Tailwind CSS v2, but they still "worked" as a no-op in Tailwind CSS v3. In v4 we would parse these directives and keep them in the output, so let's just drop them.
1 parent d2865c3 commit 329126b

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

packages/@tailwindcss-upgrade/src/codemods/migrate-tailwind-directives.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,35 @@ it('should drop `@tailwind variants;`', async () => {
400400
`),
401401
).toEqual('')
402402
})
403+
404+
it('should replace `@variants` with its children', async () => {
405+
expect(
406+
await migrate(css`
407+
@variants hover, focus {
408+
.foo {
409+
color: red;
410+
}
411+
}
412+
`),
413+
).toMatchInlineSnapshot(`
414+
".foo {
415+
color: red;
416+
}"
417+
`)
418+
})
419+
420+
it('should replace `@responsive` with its children', async () => {
421+
expect(
422+
await migrate(css`
423+
@responsive {
424+
.foo {
425+
color: red;
426+
}
427+
}
428+
`),
429+
).toMatchInlineSnapshot(`
430+
".foo {
431+
color: red;
432+
}"
433+
`)
434+
})

packages/@tailwindcss-upgrade/src/codemods/migrate-tailwind-directives.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AtRule, type ChildNode, type Plugin, type Root } from 'postcss'
1+
import { AtRule, Root, type ChildNode, type Plugin } from 'postcss'
22

33
const DEFAULT_LAYER_ORDER = ['theme', 'base', 'components', 'utilities']
44

@@ -54,6 +54,19 @@ export function migrateTailwindDirectives(options: { newPrefix: string | null })
5454
) {
5555
node.remove()
5656
}
57+
58+
// Replace Tailwind CSS v2 directives that still worked in v3. But apart
59+
// from not breaking, these directives don't do anything anymore.
60+
else if (node.name === 'variants' || node.name === 'responsive') {
61+
if (node.nodes) {
62+
for (let child of node.nodes) {
63+
child.raws.tailwind_pretty = true
64+
}
65+
node.replaceWith(node.nodes)
66+
} else {
67+
node.remove()
68+
}
69+
}
5770
})
5871

5972
// Insert default import if all directives are present

0 commit comments

Comments
 (0)