-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add CSS codemods for migrating @tailwind directives
#14411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9ea4a17
add CSS codemods for Tailwind directives
RobinMalfait e9889f2
implement `migrate-tailwind-directives` as PostCSS codemod
RobinMalfait 972c5aa
add integration test for tailwind directives
RobinMalfait d612360
use `migrateTailwindDirectives`
RobinMalfait 3a91667
update changelog
RobinMalfait 5796dad
insert `@layer …;` when the default order changes
RobinMalfait File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
214 changes: 214 additions & 0 deletions
214
packages/@tailwindcss-upgrade/src/codemods/migrate-tailwind-directives.test.ts
This file contains hidden or 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,214 @@ | ||
| import dedent from 'dedent' | ||
| import postcss from 'postcss' | ||
| import { expect, it } from 'vitest' | ||
| import { migrateTailwindDirectives } from './migrate-tailwind-directives' | ||
|
|
||
| const css = dedent | ||
|
|
||
| function migrate(input: string) { | ||
| return postcss() | ||
| .use(migrateTailwindDirectives()) | ||
| .process(input, { from: expect.getState().testPath }) | ||
| .then((result) => result.css) | ||
| } | ||
|
|
||
| it("should not migrate `@import 'tailwindcss'`", async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @import 'tailwindcss'; | ||
| `), | ||
| ).toEqual(css` | ||
| @import 'tailwindcss'; | ||
| `) | ||
| }) | ||
|
|
||
| it('should migrate the default @tailwind directives to a single import', async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @tailwind base; | ||
| @tailwind components; | ||
| @tailwind utilities; | ||
| `), | ||
| ).toEqual(css` | ||
| @import 'tailwindcss'; | ||
| `) | ||
| }) | ||
|
|
||
| it('should migrate the default @tailwind directives as imports to a single import', async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @import 'tailwindcss/base'; | ||
| @import 'tailwindcss/components'; | ||
| @import 'tailwindcss/utilities'; | ||
| `), | ||
| ).toEqual(css` | ||
| @import 'tailwindcss'; | ||
| `) | ||
| }) | ||
|
|
||
| it.each([ | ||
| [ | ||
| // The default order | ||
| css` | ||
| @tailwind base; | ||
| @tailwind components; | ||
| @tailwind utilities; | ||
| `, | ||
| css` | ||
| @import 'tailwindcss'; | ||
| `, | ||
| ], | ||
|
|
||
| // @tailwind components moved, but has no effect in v4. Therefore `base` and | ||
| // `utilities` are still in the correct order. | ||
| [ | ||
| css` | ||
| @tailwind base; | ||
| @tailwind utilities; | ||
| @tailwind components; | ||
| `, | ||
| css` | ||
| @import 'tailwindcss'; | ||
| `, | ||
| ], | ||
|
|
||
| // Same as previous comment | ||
| [ | ||
| css` | ||
| @tailwind components; | ||
| @tailwind base; | ||
| @tailwind utilities; | ||
| `, | ||
| css` | ||
| @import 'tailwindcss'; | ||
| `, | ||
| ], | ||
|
|
||
| // `base` and `utilities` swapped order, thus the `@layer` directives are | ||
| // needed. The `components` directive is still ignored. | ||
| [ | ||
| css` | ||
| @tailwind components; | ||
| @tailwind utilities; | ||
| @tailwind base; | ||
| `, | ||
| css` | ||
| @layer theme, components, utilities, base; | ||
| @import 'tailwindcss'; | ||
| `, | ||
| ], | ||
| [ | ||
| css` | ||
| @tailwind utilities; | ||
| @tailwind base; | ||
| @tailwind components; | ||
| `, | ||
| css` | ||
| @layer theme, components, utilities, base; | ||
| @import 'tailwindcss'; | ||
| `, | ||
| ], | ||
| [ | ||
| css` | ||
| @tailwind utilities; | ||
| @tailwind components; | ||
| @tailwind base; | ||
| `, | ||
| css` | ||
| @layer theme, components, utilities, base; | ||
| @import 'tailwindcss'; | ||
| `, | ||
| ], | ||
| ])( | ||
| 'should migrate the default directives (but in different order) to a single import, order %#', | ||
| async (input, expected) => { | ||
| expect(await migrate(input)).toEqual(expected) | ||
| }, | ||
| ) | ||
|
|
||
| it('should migrate `@tailwind base` to theme and preflight imports', async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @tailwind base; | ||
| `), | ||
| ).toEqual(css` | ||
| @import 'tailwindcss/theme' layer(theme); | ||
| @import 'tailwindcss/preflight' layer(base); | ||
| `) | ||
| }) | ||
|
|
||
| it('should migrate `@import "tailwindcss/base"` to theme and preflight imports', async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @import 'tailwindcss/base'; | ||
| `), | ||
| ).toEqual(css` | ||
| @import 'tailwindcss/theme' layer(theme); | ||
| @import 'tailwindcss/preflight' layer(base); | ||
| `) | ||
| }) | ||
|
|
||
| it('should migrate `@tailwind utilities` to an import', async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @tailwind utilities; | ||
| `), | ||
| ).toEqual(css` | ||
| @import 'tailwindcss/utilities' layer(utilities); | ||
| `) | ||
| }) | ||
|
|
||
| it('should migrate `@import "tailwindcss/utilities"` to an import', async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @import 'tailwindcss/utilities'; | ||
thecrypticace marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `), | ||
| ).toEqual(css` | ||
| @import 'tailwindcss/utilities' layer(utilities); | ||
| `) | ||
| }) | ||
|
|
||
| it('should not migrate existing imports using a custom layer', async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @import 'tailwindcss/utilities' layer(my-utilities); | ||
| `), | ||
| ).toEqual(css` | ||
| @import 'tailwindcss/utilities' layer(my-utilities); | ||
| `) | ||
| }) | ||
|
|
||
| // We don't have a `@layer components` anymore, so omitting it should result | ||
| // in the full import as well. Alternatively, we could expand to: | ||
| // | ||
| // ```css | ||
| // @import 'tailwindcss/theme' layer(theme); | ||
| // @import 'tailwindcss/preflight' layer(base); | ||
| // @import 'tailwindcss/utilities' layer(utilities); | ||
| // ``` | ||
| it('should migrate `@tailwind base` and `@tailwind utilities` to a single import', async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @tailwind base; | ||
| @tailwind utilities; | ||
| `), | ||
| ).toEqual(css` | ||
| @import 'tailwindcss'; | ||
| `) | ||
| }) | ||
|
|
||
| it('should drop `@tailwind screens;`', async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @tailwind screens; | ||
| `), | ||
| ).toEqual('') | ||
| }) | ||
|
|
||
| it('should drop `@tailwind variants;`', async () => { | ||
| expect( | ||
| await migrate(css` | ||
| @tailwind variants; | ||
| `), | ||
| ).toEqual('') | ||
| }) | ||
91 changes: 91 additions & 0 deletions
91
packages/@tailwindcss-upgrade/src/codemods/migrate-tailwind-directives.ts
This file contains hidden or 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,91 @@ | ||
| import { AtRule, type Plugin, type Root } from 'postcss' | ||
|
|
||
| const DEFAULT_LAYER_ORDER = ['theme', 'base', 'components', 'utilities'] | ||
|
|
||
| export function migrateTailwindDirectives(): Plugin { | ||
| function migrate(root: Root) { | ||
| let baseNode: AtRule | null = null | ||
| let utilitiesNode: AtRule | null = null | ||
|
|
||
| let defaultImportNode: AtRule | null = null | ||
| let utilitiesImportNode: AtRule | null = null | ||
| let preflightImportNode: AtRule | null = null | ||
| let themeImportNode: AtRule | null = null | ||
|
|
||
| let layerOrder: string[] = [] | ||
|
|
||
| root.walkAtRules((node) => { | ||
| // Track old imports and directives | ||
| if ( | ||
| (node.name === 'tailwind' && node.params === 'base') || | ||
| (node.name === 'import' && node.params.match(/^["']tailwindcss\/base["']$/)) | ||
| ) { | ||
| layerOrder.push('base') | ||
| baseNode = node | ||
| node.remove() | ||
| } else if ( | ||
| (node.name === 'tailwind' && node.params === 'utilities') || | ||
| (node.name === 'import' && node.params.match(/^["']tailwindcss\/utilities["']$/)) | ||
| ) { | ||
| layerOrder.push('utilities') | ||
| utilitiesNode = node | ||
| node.remove() | ||
| } | ||
|
|
||
| // Remove directives that are not needed anymore | ||
| else if ( | ||
| (node.name === 'tailwind' && node.params === 'components') || | ||
| (node.name === 'tailwind' && node.params === 'screens') || | ||
| (node.name === 'tailwind' && node.params === 'variants') || | ||
| (node.name === 'import' && node.params.match(/^["']tailwindcss\/components["']$/)) | ||
| ) { | ||
| node.remove() | ||
| } | ||
| }) | ||
|
|
||
| // Insert default import if all directives are present | ||
| if (baseNode !== null && utilitiesNode !== null) { | ||
| if (!defaultImportNode) { | ||
| root.prepend(new AtRule({ name: 'import', params: "'tailwindcss'" })) | ||
| } | ||
| } | ||
|
|
||
| // Insert individual imports if not all directives are present | ||
| else if (utilitiesNode !== null) { | ||
| if (!utilitiesImportNode) { | ||
| root.prepend( | ||
| new AtRule({ name: 'import', params: "'tailwindcss/utilities' layer(utilities)" }), | ||
| ) | ||
| } | ||
| } else if (baseNode !== null) { | ||
| if (!preflightImportNode) { | ||
| root.prepend(new AtRule({ name: 'import', params: "'tailwindcss/preflight' layer(base)" })) | ||
| } | ||
| if (!themeImportNode) { | ||
| root.prepend(new AtRule({ name: 'import', params: "'tailwindcss/theme' layer(theme)" })) | ||
| } | ||
| } | ||
|
|
||
| // Insert `@layer …;` at the top when the order in the CSS was different | ||
| // from the default. | ||
| { | ||
| // Determine if the order is different from the default. | ||
| let sortedLayerOrder = layerOrder.toSorted((a, z) => { | ||
| return DEFAULT_LAYER_ORDER.indexOf(a) - DEFAULT_LAYER_ORDER.indexOf(z) | ||
| }) | ||
|
|
||
| if (layerOrder.some((layer, index) => layer !== sortedLayerOrder[index])) { | ||
| // Create a new `@layer` rule with the sorted order. | ||
| let newLayerOrder = DEFAULT_LAYER_ORDER.toSorted((a, z) => { | ||
| return layerOrder.indexOf(a) - layerOrder.indexOf(z) | ||
| }) | ||
| root.prepend({ name: 'layer', params: newLayerOrder.join(', ') }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| postcssPlugin: '@tailwindcss/upgrade/migrate-tailwind-directives', | ||
| Once: migrate, | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that this translation is correct — though maybe it's still fine?
In this case in v3 utilities would definitely appear before base layer styles (though I can't think of any reason anyone would ever do that).
@import 'tailwindcss'would definitely not operate that way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The order shouldn't matter anymore since we are using native layers and
@import 'tailwindcss'defines the order at the top:I think if we want to be 100% correct here, we should a modified version of
@layer theme, base, components, utilities;at the top.Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we have multiple
@layer foo, bar, bazdefinitions, then the first one wins. E.g.:https://lightningcss.dev/playground/index.html#%7B%22minify%22%3Atrue%2C%22customMedia%22%3Atrue%2C%22cssModules%22%3Afalse%2C%22analyzeDependencies%22%3Afalse%2C%22targets%22%3A%7B%22chrome%22%3A6225920%7D%2C%22include%22%3A0%2C%22exclude%22%3A0%2C%22source%22%3A%22%40layer%20base%2C%20theme%2C%20components%2C%20utilities%3B%5Cn%40layer%20theme%2C%20components%2C%20base%2C%20utilities%3B%5Cn%40layer%20theme%2C%20base%2C%20components%2C%20utilities%3B%22%2C%22visitorEnabled%22%3Afalse%2C%22visitor%22%3A%22%7B%5Cn%20%20Color(color)%20%7B%5Cn%20%20%20%20if%20(color.type%20%3D%3D%3D%20'rgb')%20%7B%5Cn%20%20%20%20%20%20color.g%20%3D%200%3B%5Cn%20%20%20%20%20%20return%20color%3B%5Cn%20%20%20%20%7D%5Cn%20%20%7D%5Cn%7D%22%2C%22unusedSymbols%22%3A%5B%5D%2C%22version%22%3A%22local%22%7D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that would be the most correct translation but uh maybe it's fine. Maybe @adamwathan has thoughts here? It could help us nudge people to order them in a more consistent way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added support for that in the latest commit: 5796dad