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

Ensure layer(…) on @import is only removed when @utility is present #14783

Merged
merged 4 commits into from
Oct 24, 2024

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Oct 24, 2024

This PR fixes an issue where layer(…) next to imports were removed where they shouldn't have been removed.

The issue exists if any of the @import nodes in a file contains @utility, if that's the case then we removed the layer(…) next to all @import nodes.

Before we were checking if the current sheet contained @utility or in any of its children (sub-@import nodes).

This fixes that by looping over the @import nodes in the current sheet, and looking for the @utility in the associated/imported file. This way we update each node individually.

Test plan:

Added a dedicated integration test to make sure all codemods together result in the correct result. Input:

'src/index.css': css`
@import 'tailwindcss/utilities';
/* No layer expected */
@import './my-components.css';
/* No layer expected */
@import './my-utilities.css';
/* Expecting a layer */
@import './my-other.css';
@import 'tailwindcss/components';
`,
'src/my-components.css': css`
@layer components {
.foo {
color: red;
}
}
`,
'src/my-utilities.css': css`
@layer utilities {
.css {
color: red;
}
}
`,
'src/my-other.css': css`
/* All my fonts! */
@font-face {
}
`,

Output:

--- ./src/index.css ---
@import 'tailwindcss/utilities' layer(utilities);
/* No layer expected */
@import './my-components.css';
/* No layer expected */
@import './my-utilities.css';
/* Expecting a layer */
@import './my-other.css' layer(utilities);

@RobinMalfait RobinMalfait force-pushed the robin/ensure_layer_on_import_is_correct branch 5 times, most recently from 380c128 to 1342dc8 Compare October 24, 2024 17:33
@RobinMalfait RobinMalfait force-pushed the robin/ensure_layer_on_import_is_correct branch from 1342dc8 to 4e5b48e Compare October 24, 2024 17:33
@RobinMalfait RobinMalfait changed the title Ensure layer(…) on @import is correct Ensure layer(…) on @import is only removed when @utility is present Oct 24, 2024
@RobinMalfait RobinMalfait marked this pull request as ready for review October 24, 2024 17:43
@RobinMalfait RobinMalfait requested a review from a team as a code owner October 24, 2024 17:43
@@ -26,6 +22,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- _Upgrade (experimental)_: Ensure `@import` statements for relative CSS files are actually migrated to use relative path syntax ([#14769](https://github.com/tailwindlabs/tailwindcss/pull/14769))
- _Upgrade (experimental)_: Only generate Preflight compatibility styles when Preflight is used ([#14773](https://github.com/tailwindlabs/tailwindcss/pull/14773))
- _Upgrade (experimental)_: Don't escape underscores when printing theme values migrated to CSS variables in arbitrary values (e.g. `m-[var(--spacing-1_5)]` instead of `m-[var(--spacing-1\_5)]`) ([#14778](https://github.com/tailwindlabs/tailwindcss/pull/14778))
- _Upgrade (experimental)_: Ensure `layer(…)` on `@import` is only removed when `@utility` is present ([#14783](https://github.com/tailwindlabs/tailwindcss/pull/14783))

### Changed
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved this down to be consistent with other changelog entries

Comment on lines +161 to +162
// Only remove the `layer(…)` next to the import, if any of the children
// contains an `@utility`. Otherwise the `@utility` will not be top-level.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Only remove the `layer(…)` next to the import, if any of the children
// contains an `@utility`. Otherwise the `@utility` will not be top-level.
// Only remove the `layer(…)` next to the import if any of the children
// contain `@utility`. Otherwise `@utility` will not be top-level.

/* Expecting a layer */
@import './my-other.css';

@import 'tailwindcss/components';
Copy link
Member

Choose a reason for hiding this comment

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

Is it important that components comes last here? If that's not a crucial part of the test it seems better to me to structure things closer to how they would be in a real project.

Copy link
Member

@adamwathan adamwathan left a comment

Choose a reason for hiding this comment

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

Going to merge as-is so we can tag a release since this will already be an improvement over what's there but would like to come back and address the feedback here too before we forget 👍

@adamwathan adamwathan merged commit 430836f into next Oct 24, 2024
1 check passed
@adamwathan adamwathan deleted the robin/ensure_layer_on_import_is_correct branch October 24, 2024 18:33
adamwathan added a commit that referenced this pull request Oct 24, 2024
This PR is a continuation of #14783 to handle the feedback on that PR.

1. Update the test to be more realistic
2. Updated the comment

---------

Co-authored-by: Adam Wathan <[email protected]>
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