Skip to content

Conversation

@RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Sep 12, 2024

This PR adds CSS codemods for migrating existing @tailwind directives to the new alternatives.

This PR has the ability to migrate the following cases:


Typical default usage of @tailwind directives in v3.

Input:

@tailwind base;
@tailwind components;
@tailwind utilities;

Output:

@import 'tailwindcss';

Similar as above, but always using @import instead of @import directly.

Input:

@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

Output:

@import 'tailwindcss';

When you are only using @tailwind base:

Input:

@tailwind base;

Output:

@import 'tailwindcss/theme' layer(theme);
@import 'tailwindcss/preflight' layer(base);

When you are only using @tailwind utilities:

Input:

@tailwind utilities;

Output:

@import 'tailwindcss/utilities' layer(utilities);

If the default order changes (aka, @tailwind utilities was defined before @tailwind base), then an additional @layer will be added to the top to re-define the default order.

Input:

@tailwind utilities;
@tailwind base;

Output:

@layer theme, components, utilities, base;
@import 'tailwindcss';

When you are only using @tailwind base; @tailwind utilities;:

Input:

@tailwind base;
@tailwind utilities;

Output:

@import 'tailwindcss';

We currently don't have a concept of @tailwind components in v4, so if you are not using @tailwind components, we can expand to the default @import 'tailwindcss'; instead of the individual imports.


@tailwind screens and @tailwind variants are not supported/necessary in v4, so we can safely remove them.

Input:

@tailwind screens;
@tailwind variants;

Output:

@RobinMalfait RobinMalfait marked this pull request as draft September 16, 2024 14:42
@RobinMalfait RobinMalfait force-pushed the feat/css-codemods-tailwind-directives branch from ef3282f to 8b7c40c Compare September 16, 2024 15:26
@RobinMalfait RobinMalfait changed the base branch from next to feat/add-codemod-tooling September 16, 2024 15:26
@RobinMalfait RobinMalfait marked this pull request as ready for review September 16, 2024 15:33
@RobinMalfait RobinMalfait force-pushed the feat/add-codemod-tooling branch from bc9b0a7 to 330dbff Compare September 18, 2024 13:49
@RobinMalfait RobinMalfait force-pushed the feat/css-codemods-tailwind-directives branch from 8b7c40c to 8ac1fe3 Compare September 18, 2024 13:49
Base automatically changed from feat/add-codemod-tooling to next September 18, 2024 14:45
@RobinMalfait RobinMalfait force-pushed the feat/css-codemods-tailwind-directives branch 2 times, most recently from d9bc23a to befda0f Compare September 18, 2024 14:48
Comment on lines +73 to +115
@tailwind utilities;
@tailwind components;
@tailwind base;
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

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.

Copy link
Member Author

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

@RobinMalfait RobinMalfait force-pushed the feat/css-codemods-tailwind-directives branch from 162b1a0 to 5796dad Compare September 18, 2024 20:20
@RobinMalfait RobinMalfait merged commit 67d1849 into next Sep 18, 2024
@RobinMalfait RobinMalfait deleted the feat/css-codemods-tailwind-directives branch September 18, 2024 20:40
RobinMalfait added a commit that referenced this pull request Sep 24, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants