Skip to content

Conversation

@RobinMalfait
Copy link
Member

This PR improves where we inject the border compatibility CSS. Before this change we injected it if it was necessary in one of these spots:

  • Above the first @layer base to group it together with existing @layer base at-rules.
  • If not present, after the last @import, to make sure that we emit valid CSS because @import should be at the top (with a few exceptions).

However, if you are working with multiple CSS files, then it could be that we injected the border compatibility CSS multiple times if those files met one of the above conditions.

To solve this, we now inject the border compatibility CSS with the same rules as above, but we also have another condition:

The border compatibility CSS is only injected if the file also has a @import "tailwindcss"; or @import "tailwindcss/preflight"; in the current file.


Added integration tests to make sure that we are generating what we expect in a real environment. Some of the integration tests also use the old @tailwind directives to make sure that the order of migrations is correct (first migrate to @import syntax, then inject the border compatibility CSS).

@RobinMalfait RobinMalfait requested a review from a team as a code owner October 24, 2024 10:45
@RobinMalfait RobinMalfait force-pushed the fix/inject-border-compatibility-in-root-files branch from bea6b13 to f90aa82 Compare October 24, 2024 10:46
@import './a.1.utilities.1.css';
@import './b.1.css' layer(components);
@import './b.1.utilities.css';
@import './b.1.css';
Copy link
Member Author

Choose a reason for hiding this comment

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

File doesn't contain @import "tailwindcss" or @import "tailwindcss/preflight" therefore we don't inject the border compatibility CSS. Therefore no utilities and "other" CSS exists which means that we don't have to split the file anymore.

@import 'tailwindcss/utilities' layer(utilities);
@import './utilities.css';
/*
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed because @import "tailwincdss" or @import "tailwindcss/preflight" does not exist in this file.

--- ./src/a.1.css ---
@import './a.1.utilities.css';
/*
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed because @import "tailwincdss" or @import "tailwindcss/preflight" does not exist in this file.

}
--- ./src/b.1.css ---
/*
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed because @import "tailwincdss" or @import "tailwindcss/preflight" does not exist in this file.

--- ./src/c.1.css ---
@import './c.2.css' layer(utilities);
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed because @import "tailwincdss" or @import "tailwindcss/preflight" does not exist in this file.

--- ./src/root.4/utilities.css ---
@import 'tailwindcss/utilities' layer(utilities);
/*
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed because @import "tailwincdss" or @import "tailwindcss/preflight" does not exist in this file.

--- src/tailwind.css ---
@import 'tailwindcss';
/*
Copy link
Member Author

Choose a reason for hiding this comment

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

Injected the border compatibility CSS because @import "tailwindcss" exists

@import 'tailwindcss/theme' layer(theme);
@import 'tailwindcss/preflight' layer(base);
/*
Copy link
Member Author

Choose a reason for hiding this comment

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

Injected the border compatibility CSS because @import "tailwindcss/preflight" exists

}
--- src/utilities.css ---
@import 'tailwindcss/utilities' layer(utilities);
Copy link
Member Author

Choose a reason for hiding this comment

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

No border compatibility CSS was injected because we only have tailwindcss/utilities but not the tailwindcss or tailwindcss/preflight imports

border-color: var(--color-gray-200, currentColor);
}
}
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 whitespace handling in this file a little bit annoying but this is a test for the codemod itself not the full integration test. In integration tests the whitespace is more correct.

This issue existed before, but wanted to point it out explicitly that it's not directly related to this change.

@RobinMalfait RobinMalfait force-pushed the fix/inject-border-compatibility-in-root-files branch from dd2186a to 4fb6cfc Compare October 24, 2024 15:11
We were always injecting the border compatibility CSS above the first
`@layer base`. But in projects where you have multiple files using this
`@layer base` it means that the reset is injected in each file.

Instead, only try to inject it in files where `@import 'tailwindcss'` or
`@import 'tailwindcss/preflight'` is being used.

When we see a file with `@import 'tailwindcss'`, we consider it a root
file because that's where Tailwind CSS will be injected.

If you split up your code, it could be that you have a different file
that contains `@import "tailwindcss/preflight";`. This is the import
responsible for adding the `@layer base` resets, so this is a good file
to put the border compatibility CSS as well.
@RobinMalfait RobinMalfait force-pushed the fix/inject-border-compatibility-in-root-files branch from 4fb6cfc to 8754b6c Compare October 24, 2024 15:15
@adamwathan adamwathan changed the title Improve injection of the border compatibility CSS Only generate Preflight compatibility styles when Preflight is used Oct 24, 2024
@adamwathan adamwathan merged commit 5a1c2e7 into next Oct 24, 2024
1 check passed
@adamwathan adamwathan deleted the fix/inject-border-compatibility-in-root-files branch October 24, 2024 17:19
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