Skip to content

Commit 723f5db

Browse files
authored
CSS codemod: Fix incorrect empty layer() at the end of @import at-rules (#14513)
This PR fixes an issue where some `@import` at-rules had an empty `layer()` attached at the end of the `@import` string. We should only add that if a Tailwind directive or Tailwind import such as `@tailwind base` or `@import "tailwindcss/base"` preceded the current `@import` at-rule. If there was no Tailwind directive, the `lastLayer` will be empty and we don't need to attach it to the `@import` string.
1 parent 951f644 commit 723f5db

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- _Experimental_: Improve codemod output, keep CSS after last Tailwind directive unlayered ([#14512](https://github.com/tailwindlabs/tailwindcss/pull/14512))
13+
- _Experimental_: Fix incorrect empty `layer()` at the end of `@import` at-rules when running codemods ([#14513](https://github.com/tailwindlabs/tailwindcss/pull/14513))
1314

1415
## [4.0.0-alpha.25] - 2024-09-24
1516

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ function migrate(input: string) {
1414
.then((result) => result.css)
1515
}
1616

17+
it('should not migrate already migrated `@import` at-rules', async () => {
18+
expect(
19+
await migrate(css`
20+
@import 'tailwindcss';
21+
`),
22+
).toMatchInlineSnapshot(`"@import 'tailwindcss';"`)
23+
})
24+
1725
it('should migrate rules between tailwind directives', async () => {
1826
expect(
1927
await migrate(css`

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function migrateMissingLayers(): Plugin {
6565

6666
// Add layer to `@import` at-rules
6767
if (node.name === 'import') {
68-
if (!node.params.includes('layer(')) {
68+
if (lastLayer !== '' && !node.params.includes('layer(')) {
6969
node.params += ` layer(${lastLayer})`
7070
}
7171

0 commit comments

Comments
 (0)