Skip to content

Commit ceae1db

Browse files
authored
CSS codemod: do not wrap comment nodes in @layer (#14517)
When CSS exists between two tailwind directives, then the CSS will be wrapped in an `@layer` to ensure it all ends up in the correct location in the final CSS file. However, if the only thing between two tailwind directives is a comment, then the comment will also be wrapped in an `@layer` directive. E.g.: ```css @tailwind base; /* This is a comment */ @tailwind components; /* This is another comment */ @tailwind utilities; ``` Results in: ```css @import "tailwindcss"; @layer base { /* This is a comment */ } @layer components { /* This is another comment */ } ``` In this case, the layers don't matter, so this can be simplified to: ```css @import "tailwindcss"; /* This is a comment */ /* This is another comment */ ```
1 parent e770c0d commit ceae1db

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- _Experimental_: Improve codemod output, keep CSS after last Tailwind directive unlayered ([#14512](https://github.com/tailwindlabs/tailwindcss/pull/14512))
1313
- _Experimental_: Fix incorrect empty `layer()` at the end of `@import` at-rules when running codemods ([#14513](https://github.com/tailwindlabs/tailwindcss/pull/14513))
1414
- _Experimental_: Migrate `@import "tailwindcss/tailwind.css"` to `@import "tailwindcss"` ([#14514](https://github.com/tailwindlabs/tailwindcss/pull/14514))
15+
- _Experimental_: Do not wrap comment nodes in `@layer` when running codemods ([#14517](https://github.com/tailwindlabs/tailwindcss/pull/14517))
1516

1617
## [4.0.0-alpha.25] - 2024-09-24
1718

packages/@tailwindcss-upgrade/src/codemods/migrate-missing-layers.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,38 @@ it('should not migrate already migrated `@import` at-rules', async () => {
2222
).toMatchInlineSnapshot(`"@import 'tailwindcss';"`)
2323
})
2424

25+
it('should not wrap comments in a layer, if they are the only nodes', async () => {
26+
expect(
27+
await migrate(css`
28+
@tailwind base;
29+
30+
/* BASE */
31+
32+
@tailwind components;
33+
34+
/* COMPONENTS */
35+
36+
@tailwind utilities;
37+
38+
/** UTILITIES */
39+
/** - Another comment */
40+
`),
41+
).toMatchInlineSnapshot(`
42+
"@tailwind base;
43+
44+
/* BASE */
45+
46+
@tailwind components;
47+
48+
/* COMPONENTS */
49+
50+
@tailwind utilities;
51+
52+
/** UTILITIES */
53+
/** - Another comment */"
54+
`)
55+
})
56+
2557
it('should migrate rules between tailwind directives', async () => {
2658
expect(
2759
await migrate(css`

packages/@tailwindcss-upgrade/src/codemods/migrate-missing-layers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ export function migrateMissingLayers(): Plugin {
8484

8585
// Wrap each bucket in an `@layer` at-rule
8686
for (let [layerName, nodes] of buckets) {
87+
// Do not wrap comments in a layer, if they are the only nodes.
88+
if (nodes.every((node) => node.type === 'comment')) {
89+
continue
90+
}
91+
8792
let target = nodes[0]
8893
let layerNode = new AtRule({
8994
name: 'layer',

0 commit comments

Comments
 (0)