-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Conversation
ef3282f to
8b7c40c
Compare
bc9b0a7 to
330dbff
Compare
8b7c40c to
8ac1fe3
Compare
d9bc23a to
befda0f
Compare
| @tailwind utilities; | ||
| @tailwind components; | ||
| @tailwind base; |
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:
/* tailwindcss/index.css */
@layer theme, base, components, utilities;
@import './theme.css' layer(theme);
@import './preflight.css' layer(base);
@import './utilities.css' layer(utilities);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.
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
packages/@tailwindcss-upgrade/src/codemods/migrate-tailwind-directives.test.ts
Show resolved
Hide resolved
162b1a0 to
5796dad
Compare
This PR adds a codemod that ensures that some parts of your stylesheet are wrapped in an `@layer`. This is a follow-up PR of #14411, in that PR we migrate `@tailwind` directives to imports. As a quick summary, that will turn this: ```css @tailwind base; @tailwind components; @tailwind utilities; ``` Into: ```css @import 'tailwindcss'; ``` But there are a few issues with that _if_ we have additional CSS on the page. For example let's imagine we had this: ```css @tailwind base; body { background-color: red; } @tailwind components; .btn {} @tailwind utilities; ``` This will now be turned into: ```css @import 'tailwindcss'; body { background-color: red; } .btn {} ``` But in v4 we use real layers, in v3 we used to replace the directive with the result of that layer. This means that now the `body` and `.btn` styles are in the incorrect spot. To solve this, we have to wrap them in a layer. The `body` should go in an `@layer base`, and the `.btn` should be in an `@layer components` to make sure it's in the same spot as it was before. That's what this PR does, the original input will now be turned into: ```css @import 'tailwindcss'; @layer base { body { background-color: red; } } @layer components { .btn { } } ``` There are a few internal refactors going on as well, but those are less important.
This PR adds CSS codemods for migrating existing
@tailwinddirectives to the new alternatives.This PR has the ability to migrate the following cases:
Typical default usage of
@tailwinddirectives in v3.Input:
Output:
Similar as above, but always using
@importinstead of@importdirectly.Input:
Output:
When you are only using
@tailwind base:Input:
@tailwind base;Output:
When you are only using
@tailwind utilities:Input:
@tailwind utilities;Output:
If the default order changes (aka,
@tailwind utilitieswas defined before@tailwind base), then an additional@layerwill be added to the top to re-define the default order.Input:
Output:
When you are only using
@tailwind base; @tailwind utilities;:Input:
Output:
We currently don't have a concept of
@tailwind componentsin v4, so if you are not using@tailwind components, we can expand to the default@import 'tailwindcss';instead of the individual imports.@tailwind screensand@tailwind variantsare not supported/necessary in v4, so we can safely remove them.Input:
Output: