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

Vite: Don't rebase urls that appear to be aliases #16078

Merged

Conversation

philipp-spiess
Copy link
Member

Closes #16039

This PR changes our URL rebasing logic used with Vite so that it does not rebase URLs that look like common alias paths (e.g. urls starting in ~, @ or #, etc.). Unfortunately this is only an approximation and you can configure an alias for a path that starts with a regular alphabetical character (e.g. foo => ./my/foo) so this isn't a perfect fix, however in practice most aliases will be prefixed with a symbol to make it clear that it's an alias anyways.

One alternative we have considered is to only rebase URLs that we know are relative (so they need to start with a .). This, however, will break common CSS use cases where urls are loaded like this:

background: image-set(
  url('image1.jpg') 1x,
  url('image2.jpg') 2x
);

So making this change felt like we only trade one GitHub issue for another one.

In a more ideal scenario we try to resolve the URL with the Vite resolver (we have to run the resolver and can't rely on the resolve setting alone due to packages like vite-tsconfig-paths), however even then we can have relative paths being resolvable to different files based on wether they were rebased or not (e.g. when an image with the same filename exists in two different paths).

So ultimately we settled on extending the already existing blocklist (which we have taken from the Vite implementation) for now.

Test plan

  • Added unit test and it was tested with the Vite playground.

@philipp-spiess philipp-spiess requested a review from a team as a code owner January 30, 2025 17:03
@philipp-spiess philipp-spiess force-pushed the fix/vite-dont-rewrite-urls-that-look-like-aliases branch from ab7b735 to 4b23045 Compare January 30, 2025 17:14
@ArnaudBarre
Copy link

Could the tailwind plugin skip the rebasing all together? It seems this is something that should be done by Vite core and not Tailwind. What's the need that made you add this? (I think we already discuss this but I don't remember the reason)

@philipp-spiess
Copy link
Member Author

philipp-spiess commented Jan 31, 2025

@ArnaudBarre

Could the tailwind plugin skip the rebasing all together? It seems this is something that should be done by Vite core and not Tailwind. What's the need that made you add this? (I think we already discuss this but I don't remember the reason)

The reason we need to rebase is that we do @import resolving in pre-phase so the information about where the CSS file was located is gone by the time Vite gets it :/ And since the transformers are string => string, I don't think there's any type of meta information we could pass with the transformation step so that Vite knows that some chunk of the content comes from different files.

What we did is inline the rebasing logic from Vite but since Vite seems to resolve filenames before it does the rebasing (or maybe while it does that?) it still works slightly differently.

@philipp-spiess philipp-spiess merged commit deb33a9 into main Jan 31, 2025
5 checks passed
@philipp-spiess philipp-spiess deleted the fix/vite-dont-rewrite-urls-that-look-like-aliases branch January 31, 2025 14:23
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.

@tailwindcss/vite breaks url() using resolve.alias
3 participants