Skip to content

Vite: Add option to disable module-graph based scanning and turn it on for Astro #16425

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

Closed
wants to merge 7 commits into from

Conversation

philipp-spiess
Copy link
Member

@philipp-spiess philipp-spiess commented Feb 11, 2025

This PR adds an option to the Vite plugin to control wether or not the module-graph based dependency scanner is used.

The main motivation for doing this is a number of SSR specific build issues where differences in the dependency graphs for SSR and client builds would cause unexpected production builds. Some specific examples:

The new option has a way to force module-graph and file-system based candidate scanning but will default to an automatic mode that will detect Astro and change the default behavior there to immediately fix the most prominent issue (#15794).

It is configured like this:

import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'

export default defineConfig({
  plugins: [tailwindcss({ scanner: 'file-system' })],
})

Test plan

I ensured that the new option turns-on automatically for Astro and that enabling it manually fixes the other cases.

@justingolden21
Copy link

If true that this would allow tailwind 4 to once again purge unused utility classes, then that would be an ENORMOUS win for developers and the web as a whole!

Thank you for working on this!

@philipp-spiess philipp-spiess changed the title Vite: Add option to disable module-graph based scanning Vite: Add option to disable module-graph based scanning and turn it on for Astro Feb 14, 2025
@philipp-spiess philipp-spiess marked this pull request as ready for review February 14, 2025 15:35
@philipp-spiess philipp-spiess requested a review from a team as a code owner February 14, 2025 15:35
@philipp-spiess
Copy link
Member Author

We're going with an option-less approach for now since it's less confusing and won't require debugging if things go wrong: #16631

@philipp-spiess philipp-spiess deleted the feat/vite-disable-module-graph-scanner branch February 20, 2025 12:33
philipp-spiess added a commit that referenced this pull request 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).
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