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 @import url(…) is hoisted #16203

Closed
wants to merge 6 commits into from
Closed

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Feb 3, 2025

This PR fixes an issue where if you use @import url(…); after the @import "tailwindcss";, then invalid CSS is being generated.

This is because the @import "tailwindcss"; directive is being replaced with the actual generated CSS variables, utilities, etc. but the @import url(…); is kept as-is (Tailwind CSS doesn't handle these). This means that in the final CSS the @import url(…) could be in the middle of the generated CSS which is invalid.

/* Invalid: */
@import "tailwindcss";
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
/* Valid: */
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
@import "tailwindcss";

While the above examples should both work from a CSS perspective, this PR makes sure to hoist the @import url(…); rules. All non-external imports will be inlined already.

Once this PR is merged, both these situations are valid:

/* Valid: */
@import "tailwindcss";
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
/* Valid: */
@import "tailwindcss";
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');

In order to do this correctly, we also have to make sure that the license comment and @charset is in the correct spot.

Test plan

  • Added a simple unit test
  • Added an integration test

@RobinMalfait RobinMalfait requested a review from a team as a code owner February 3, 2025 11:22
@wongjn
Copy link
Contributor

wongjn commented Feb 4, 2025

Both situations seem the same to me:

@import "tailwindcss";
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
@import "tailwindcss";
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');

@RobinMalfait
Copy link
Member Author

Going to close this for now. Will document this behavior instead, where we explain how these imports are inlined.

@RobinMalfait
Copy link
Member Author

Documented here: tailwindlabs/tailwindcss.com#2051

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