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

Can @tailwindcss/vite scan step seems to miss classes in waku #16585

Closed
tylersayshi opened this issue Feb 16, 2025 · 6 comments · Fixed by #16631
Closed

Can @tailwindcss/vite scan step seems to miss classes in waku #16585

tylersayshi opened this issue Feb 16, 2025 · 6 comments · Fixed by #16631
Labels

Comments

@tylersayshi
Copy link

tylersayshi commented Feb 16, 2025

We're seeing not all classes be picked up by the tailwind vite plugin right now. I've attached a reproduction (thanks to one of our waku users on discord). The issue in this case is that bg-blue-400 does not make it into the tailwind style output. I am able to see the className in the html output so it does seem like a bug with the plugin for now to me.

repro: https://github.com/tylersayshi/waku-vite-tailwind-bug

From this code it seems like this should auto-read from the html output, but it does seem to miss this case for now.

https://github.com/tailwindlabs/tailwindcss/blob/main/packages/%40tailwindcss-vite/src/index.ts#L187-L214

I'm happy to help however I can here :)

Update: I pushed a working example with postcss here: https://github.com/tylersayshi/waku-vite-tailwind-bug/tree/postcss

tylersayshi added a commit to tylersayshi/waku that referenced this issue Feb 17, 2025
some nested components not working with the tailwind vite plugin. the
hmr seemed broken when using the tailwind plugin as well.

related issue:
tailwindlabs/tailwindcss#16585
dai-shi added a commit to dai-shi/waku that referenced this issue Feb 17, 2025
some nested components not working with the tailwind vite plugin. the
hmr seemed broken when using the tailwind plugin as well.

related issue:
tailwindlabs/tailwindcss#16585

---------

Co-authored-by: Tyler <26290074+thegitduck@users.noreply.github.com>
Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
@philipp-spiess
Copy link
Member

Thanks for the report @tylersayshi. Two quick questions:

  • Is this only an issue for production builds or also for dev builds?
  • Does Waku run multiple Vite builds (e.g for ssr vs client builds)?

I'm asking because the way the moudle-graph dependency scanner works has some drawbacks around production builds with SSR specifically, see this PR for some context and a proposed fix: #16425

Would love to understand if this approach would work for you!

@philipp-spiess
Copy link
Member

Been looking into the repro and found something interesting. Apparently Waku has two different Vite builds running (I identified them by invocations of the tailwindcss() plugin function. ClientComponent1 is seen by both instances but ClientComponent2 only by the latter:

Image

I suspect that the stylesheet is generated as an artefact of the first vite invocation sand thus the changes are not propagated.

One thing that fixes it but I'm not to sure about wether it's a good idea or not: We can move the scanned candidates into the module scope instead of the vite function scope so they are shared between all Vite runs 🤔 That fixes the issue but I'm sure it creates other ones now since the module can not be invalidated if the class is found later because it's not part of the same module tree we get the transform callback...

The PR I linked to with the file-system option also makes the issue work, which I guess is going to be the better approach here although it's unfortunate that you have to enable this option now. Perhaps there's a way to detect waku in the Vite plugin and enable this option automatically?

@tylersayshi
Copy link
Author

tylersayshi commented Feb 18, 2025

Been looking into the repro and found something interesting. Apparently Waku has two different Vite builds running (I identified them by invocations of the tailwindcss() plugin function. ClientComponent1 is seen by both instances but ClientComponent2 only by the latter:

With this as the issue, we should be able to configure vite to use the tailwind plugin for both builds. We manage the vite configs for users, so we should move to including the tailwind plugin through our waku.config.ts to be sure it's always used if I am following correctly here.

Thanks for linking #16425 btw! That looks like it will help us as well, we do run SSR with RSC.

@philipp-spiess
Copy link
Member

With this as the issue, we should be able to configure vite to use the tailwind plugin for both builds. We manage the vite configs for users, so we should move to including the tailwind plugin through our waku.config.ts to be sure it's always used if I am following correctly here.

I think that alone won't help, the issue is still that one build emits the css file but another build transforms the components.

Let me keep you posted here, we're testing some larger scale Vite changes that should resolve this as well now (basically turning on that new option from #16425 by default).

philipp-spiess added a commit that referenced this issue Feb 20, 2025
Alternative to #16425

Fixes #16585
Fixes #16389
Fixes #16252
Fixes #15794
Fixes #16646
Fixes #16358

This PR changes the Vite plugin to use the file-system to discover
potential class names instead of relying on the module-graph. This comes
after a lot of testing and various issue reports where builds that span
different Vite instances were missing class names.

Because we now scan for candidates using the file-system, we can also
remove a lot of the bookkeeping necessary to make production builds and
development builds work as we no longer have to change the resulting
stylesheet based on the `transform` callbacks of other files that might
happen later.

This change comes at a small performance penalty that is noticeable
especially on very large projects with many files to scan. However, we
offset that change by fixing an issue that I found in the current Vite
integration that did a needless rebuild of the whole Tailwind root
whenever any source file changed. Because of how impactful this change
is, I expect many normal to medium sized projects to actually see a
performance improvement after these changes. Furthermore we do plan to
continue to use the module-graph to further improve the performance in
dev mode.

## Test plan

- Added new integration tests with cases found across the issues above.
- Manual testing by adding a local version of the Vite plugin to repos
from the issue list above and the [tailwindcss
playgrounds](https://github.com/philipp-spiess/tailwindcss-playgrounds).
@philipp-spiess
Copy link
Member

@tylersayshi Hey! We decided to land some changes in the Vite plugin that will ensure that both SSR and RSC builds now have access to the same class name list. We plan to release this in a patch very soon (hopefully today!). Thanks for the bug report. 🙇

@tylersayshi
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants