Skip to content
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

Add missing layer(…) to imports above Tailwind directives #14982

Merged
merged 8 commits into from
Nov 14, 2024

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Nov 12, 2024

This PR fixes an issue where imports above Tailwind directives didn't get a layer(…) argument.

Given this CSS:

@import "./typography.css";
@tailwind base;
@tailwind components;
@tailwind utilities;

It was migrated to:

@import "./typography.css";
@import "tailwindcss";

But to ensure that the typography styles end up in the correct location, it requires the layer(…) argument.

This PR now migrates the input to:

@import "./typography.css" layer(base);
@import "tailwindcss";

Test plan:

Added an integration test where an import receives the layer(…), but an import that eventually contains @utility does not receive the layer(…) argument. This is necessary otherwise the @utility will be nested when we are processing the inlined CSS.

Running this on the Commit template, we do have a proper layer(…)
image

@RobinMalfait RobinMalfait requested a review from a team as a code owner November 12, 2024 19:51
@RobinMalfait RobinMalfait force-pushed the fix/missing-layer-above-tailwind-directives branch from 06cebc8 to 3c168fb Compare November 12, 2024 19:52
@philipp-spiess philipp-spiess marked this pull request as draft November 13, 2024 15:44
@philipp-spiess philipp-spiess marked this pull request as draft November 13, 2024 15:44
@RobinMalfait RobinMalfait force-pushed the fix/missing-layer-above-tailwind-directives branch 2 times, most recently from c065675 to 51254b5 Compare November 14, 2024 07:17
@RobinMalfait RobinMalfait force-pushed the fix/missing-layer-above-tailwind-directives branch from 51254b5 to 1f55220 Compare November 14, 2024 10:49
At this point it doesn't matter if we are layered or not, but if the
stylesheet contains an `@utility`. If it does, then that's a reason to
potentially split stylesheets (if necessary).
@RobinMalfait RobinMalfait force-pushed the fix/missing-layer-above-tailwind-directives branch from 1f55220 to 7a41b4e Compare November 14, 2024 11:09
@@ -1571,8 +1698,9 @@ test(
}

--- ./src/components.css ---
@import './typography.css';
@import './typography.css' layer(components);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has layer(components) because this is coming from the components file where nothing sits above it (in that file), but there is a @layer components and @tailwind components below it.

if we want to look at as-if it was flattened, then it exists between base and components. This means that if this code existed in the same file we would've generated layer(base) instead. 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also what we get on the Commit template with this PR:
image

Without knowing the rest of the files, it looks correct. But I wonder if layer(base) is more correct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked with @philipp-spiess and @thecrypticace and we came to the conclusion that this is the least surprising outcome where you look at CSS files in isolation.

@RobinMalfait RobinMalfait marked this pull request as ready for review November 14, 2024 13:45
Copy link
Member

@philipp-spiess philipp-spiess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can fix the case where the tailwind-setup.css file is split, otherwise this looks solid

Comment on lines 1716 to 1717
@import './components.utilities.css';
@import './utilities.utilities.css'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can avoid splitting up this file as tailwind-setup.css is also the Tailwind root. The result here does seem a bit unexpected 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, a split made sense, but the split should've stopped at the Tailwind root file indeed. Handled here: b62c499

You can see the difference in the integration tests: 87c0bb2

Re-ran it on the Commit template and the output is the same (because no splits happened there, but it also didn't break anything)

When we detect CSS files that contain `@utility` and non-`@utility`
parts, then we split them. We will also split every parent file to
ensure the imports are correct.

The new files with `@utility` should not result in a `layer(…)` next to
the imports. The other CSS might end up with a `layer(…)`
Comment on lines 1838 to 1844
--- ./src/components.css ---
@import './typography.css';
@import './typography.css' layer(components);

--- ./src/components.utilities.css ---
@utility foo {
color: red;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that technically since the only non @utility is an @import with a layer(…) that this split shouldn't be required. But that feels like an optimization for a separate PR (if we even need that.).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into it and it's not a lot of work to make it work because this does feel unexpected right now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok now I'm happy with this.

We only need to split if the stylesheet contains `@utility` and non-safe
CSS. Imports with `layer(…)`, `@layer` blocks and comments are
considered safe, everything else is not.

The non safe-code might end up in a new import with a `layer(…)` to
guarantee that it's injected in the correct spot.
@RobinMalfait RobinMalfait merged commit 4079059 into next Nov 14, 2024
1 check passed
@RobinMalfait RobinMalfait deleted the fix/missing-layer-above-tailwind-directives branch November 14, 2024 17:05
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.

2 participants