You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve @tailwindcss/vite build performance by scanning the same sources only once per build - #20678
Hi, and thanks for maintaining Tailwind CSS.
While profiling react-router build in our React Router app, I found that Tailwind scanned the project for the client build, scanned it again for the SSR build, and walked it once more each time to collect files to watch, although a build doesn't watch anything.
Outside of watch mode, this:
shares the scanned candidates between roots and environments with the same sources in a build. React Router (with the Vite Environment API), SvelteKit, TanStack Start and SolidStart build their environments with the same plugin instance (builder.sharedPlugins), so the SSR build no longer scans again.
stops reading scanner.files and scanner.globs just to call addWatchFile(). Reading globs walks the sources again.
doesn't create a Scanner for stylesheets that aren't Tailwind roots.
On our app (5,971 files to scan, React Router 8.5.1, Vite 8.3.6, macOS), react-router build goes from 15.9 s to 13.2 s with identical output: the client build from 8.6 s to 7.8 s, and the SSR build from 6.5 s to 4.6 s.
In a Linux container, the same files take 125 ms to scan and 21 ms for globs, so the saving there is about 170 ms per build.
This also keeps the SSR CSS the same as the client CSS.
When build/ isn't ignored, the SSR scan picked up the client build output, and React Router emitted a second stylesheet with a different hash, which looks like #19888.
Dev mode and vite build --watch behave as before, including workers built during a watched build.
The shared candidates are reset in configResolved and when an environment builds again, and only the first scan of a new scanner is shared.
The source(…) check still throws in the same cases.
[ci-all]
Test plan
The new React Router integration test builds with v8_viteEnvironmentApi and without build/ in .gitignore, and expects one stylesheet. It fails on main and passes here.
pnpm run test:integrations ./integrations/vite passes, and so does the watch mode test with its fixture switched to Vite 8.
The 57 CSS files emitted by the 51 building integration tests are identical to main, apart from the extra stylesheet in the new test.
In small Vite 8 fixtures, a source() listing files, a worker in worker.plugins during vite build --watch, a reused builder and a reused plugin instance all give the same CSS as main.
No actionable comments were generated in the recent review.
Walkthrough
The Vite plugin resets candidate-scan state as build environments start. Non-watch builds share candidate scans, while watch builds register source dependencies. Root.generate skips scanner setup for compilers without Tailwind-root features and adjusts source-root validation. Integration tests cover Vite 7 and Vite 8 builds. A changelog entry documents the scanning and watch behavior.
Merge Risk: Low · up to e13ff
Shared scanning speeds up Vite builds. In a narrow case, a reused builder that builds an environment for the first time after source edits can reuse earlier scan results and miss new utilities. The author documents this as an accepted trade-off. Owners should be aware of it, but it does not block merge.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title clearly summarizes the main change: reducing repeated source scans in @tailwindcss/vite during builds.
Description check
The description explains the repeated scans, the proposed performance changes, and the related tests and results.
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 4cb6ed07-e07e-4eb6-a2f3-f29b6c89a814
Commits
Reviewing files that changed from the base of the PR and between 198a24a and e13ffd9.
Files selected for processing: 4
.changelog/vite-build-scan-once.md
integrations/vite/index.test.ts
integrations/vite/react-router.test.ts
packages/@tailwindcss-vite/src/index.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Additional comments: 2
packages/@tailwindcss-vite/src/index.ts: 1
443-445: Trivial · Maintainability & Code Quality · Low value
Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
Remove the duplicated isTailwindRoot() check.
The check at Lines 443-445 runs only when the compiler is rebuilt. The check at Lines 473-475 runs on every call. The second check is therefore redundant on the rebuild path. It is still needed on the cached path. Keep one check after the if/else block to avoid duplicate logic.
If the early return is intended to skip scanner setup, keep the first check. Add a short comment that explains why the second check exists.
Also applies to: 473-475
integrations/vite/react-router.test.ts: 1
310-310: Trivial · Maintainability & Code Quality · Low value
Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
Use className instead of class in the JSX.
React uses className. class="dark" type-checks poorly in TSX. It is also not needed for the assertion. Remove the attribute or rename it.
Proposed fix
- <body class="dark">+ <body className="dark">
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 04040d15-2ea1-47e4-a4a4-2cb543ba0088
Commits
Reviewing files that changed from the base of the PR and between 9b2c925 and 5779b80.
Files selected for processing: 3
.changelog/vite-build-scan-once.md
integrations/vite/react-router.test.ts
packages/@tailwindcss-vite/src/index.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
The first check is what skips the scanner setup for stylesheets that aren't Tailwind roots, so I kept both.
The second one is the existing check from main, so I didn't add a comment for it.
Use className instead of class in the JSX.
This root.tsx is copied from the ?url test above, which has the same class="dark".
It doesn't affect the build, so I kept the two fixtures the same.
…urces only once per build
Outside of watch mode, roots and environments with the same sources
share one scan per build, files to watch are no longer collected, and
stylesheets that aren't Tailwind roots don't get a scanner.
A long-lived process can build the same environments again with one
plugin instance, for example with a `createBuilder` builder that it
keeps. The scan cache of `@tailwindcss/vite` must not give the
candidates of the first round to the second round.
The test changes `src/index.css` between the rounds. So the compiler
and the scanner are set up again, and they read the scan cache. The
test fails when the `buildStart` hook does not clear the cache.
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Additional comments: 1
packages/@tailwindcss-vite/src/index.ts: 1
443-445: Trivial · Maintainability & Code Quality · Low value
Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
Remove the duplicated isTailwindRoot() gate or document why both are needed.
The first check (Line 443) runs only when the compiler is rebuilt. The second check (Line 473) runs on every call and is also correct when the compiler is reused. The first check can return before this.scanner is set. A reused compiler then reaches the second check only if the first check passed once. This is consistent. Add a short comment on the first check that explains it skips scanner setup, so a later reader does not remove it.
Also applies to: 473-475
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 37815d49-8646-49fe-8cdb-355b714e8e20
Commits
Reviewing files that changed from the base of the PR and between 198a24a and 825b099.
Files selected for processing: 3
.changelog/vite-build-scan-once.md
integrations/vite/react-router.test.ts
packages/@tailwindcss-vite/src/index.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
The environments of a build share their scans. A reused builder that
builds an environment for the first time after files changed still uses
the earlier scans. This is a deliberate trade-off: the shared scans also
keep the CSS of a later environment the same when it would otherwise
scan the output of an earlier one. The comment makes this visible where
the cache is reset.
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Scanning the shared sources only once per build is a nice speedup for frameworks that build multiple environments, and the detailed profiling notes made this easy to review.
… act on (#20728)
This PR keeps `### Changed` in the changelog for changes that users may
have to act on, and documents which section to use.
`[Unreleased]` had a `### Changed` section with two performance entries
(#20680 and
#20688). A performance
improvement is not a change that users have to act on, and the repo
already lists them under `### Fixed` (e.g.
#20694,
#20678 and
#20663).
## Changes
- `CHANGELOG.md`: remove the `### Changed` section of `[Unreleased]`.
Combine the two entries into one entry in `### Fixed`, because
#20688 only extends
#20680 to the other
integrations. The entry goes in the merge order of
#20680.
- `CHANGELOG.md`: move the canonicalization entry of
#20697 from `### Added`
to `### Fixed`, in merge order, and drop the empty `### Added` heading.
A canonicalization improvement is not a new feature for users, and the
canonicalization entries of 4.3.4 and 4.3.5 are in `### Fixed` too.
- `.github/CONTRIBUTING.md`: add a guide on which section to pick. The
examples for `### Changed` come from earlier releases: changes to the
CSS of existing utilities, renamed or replaced utilities (also when the
upgrade tool migrates them), theme defaults, APIs, stricter parsing,
source detection defaults, and supported versions.
- `.changelog/README.md`: link to that guide.
This PR edits `CHANGELOG.md` directly instead of adding a fragment,
because it fixes entries that the "Compile changelog" workflow already
compiled. Released sections stay as they are. The PR changes only docs,
so it needs no changelog entry of its own.
## Test plan
- `pnpm changelog check` passes.
- `pnpm --silent changelog` shows `[Unreleased]` with only `### Fixed`.
- `pnpm prettier --check` passes for the three files.
This PR makes `@tailwindcss/vite` release the compilers and scanners of
a build once the build finishes.
On `main`, a finished build no longer keeps its plugin context or its
module graph, thanks to
#20531 and
#20678. A probe like the
one in the issue reports both as freed.
But the build plugin still keeps a `Root` for each CSS file of each
environment, for as long as the plugin lives. Each root keeps its
compiler, its scanner, and the resolvers of the environment that built
it. So a process that keeps working after the build keeps all of them in
memory. Examples are Astro, which renders its pages after the build, and
Nuxt, which bundles the server with Nitro after the Vite builds. On Vite
8, Vite itself keeps the plugin alive after `build()`, so this happens
even when the process drops the plugin.
This PR deletes the roots of an environment in `closeBundle`, except in
watch mode, where rebuilds use them again. In watch mode, it deletes
them in `closeWatcher`, once the watcher closes. Unlike the
`rootsByEnv.clear()` that the issue suggests, it only deletes the roots
of the environment that finished, so it doesn't touch environments that
are still building. Dev mode doesn't change, because the hooks are on
the build plugin only (`apply: 'build'`).
The hooks come from
#20503 by lazerg, who is
a co-author of the commit.
## Behavior change
A reused builder (`builder.build(environment)` more than once) now
starts each build of an environment with new roots. So it no longer
generates CSS for classes that were removed since an earlier build, like
a build in a new process.
#20678 already starts
the scans over in this case, but the roots kept their earlier
candidates. The cost is that the second build sets up its compilers and
scanners again.
## Memory
Heap after GC, while the process keeps the plugin, for a fixture with
2,000 modules and one Tailwind CSS root, on Node 24.22:
| | `main` | This PR |
| --- | --- | --- |
| Vite 7, `build()` | 34.8 MiB | 23.4 MiB |
| Vite 7, builder with client and SSR | 46.6 MiB | 23.7 MiB |
| Vite 7, watch build after `close()` | 35.8 MiB | 23.0 MiB |
| Vite 8, `build()` | 33.8 MiB | 21.4 MiB |
| Vite 8, builder with client and SSR | 46.3 MiB | 22.5 MiB |
Before the build, the heap is 13.6 MiB with Vite 7 and 14.5 MiB with
Vite 8. Each root keeps about 11 MiB here, so projects with more roots
gain more. In a Nuxt project,
#20501 (comment)
measured about 130 MiB of live heap for the state of the plugin on
`main`.
The fix also doesn't depend on how the runtime captures closures. On Bun
1.4, the heap of the builder goes from 59.3 MiB to 22.9 MiB.
What stays belongs to Vite. The plugin keeps the resolved `config` for
its next build. On Vite 7, the config keeps the environment of the
latest build, because `vite:prepare-out-dir` keeps a `Set` of the
environments that it prepared. Vite 8 keeps its environments alive by
itself.
## Test plan
- A new integration test checks that a finished build releases its
compiler while the process keeps the plugin, and that a watch build
keeps it until the watcher closes. A `@plugin` tracks an object that
only the compiler keeps, with a `WeakRef`, and the scripts run with
`node --expose-gc`.
- A new integration test checks that a reused builder doesn't keep a
removed class.
- Both tests fail without the fix: the compiler stays alive, and
`.font-bold` stays in the second build.
- Watch rebuilds still reuse their roots: with `DEBUG=tailwindcss`, the
number of compiler setups doesn't change.
- `pnpm run build` and the Vite integration tests pass locally, except
the tests whose `pnpm install` fails in my local setup
(`?commonjs-proxy` and Nuxt).
Fixes#20501
<sub>Sent by an AI assistant on behalf of @RobinMalfait.</sub>
---------
Co-authored-by: lazerg <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Hi, and thanks for maintaining Tailwind CSS.
While profiling
react-router buildin our React Router app, I found that Tailwind scanned the project for the client build, scanned it again for the SSR build, and walked it once more each time to collect files to watch, although a build doesn't watch anything.Outside of watch mode, this:
builder.sharedPlugins), so the SSR build no longer scans again.scanner.filesandscanner.globsjust to calladdWatchFile(). Readingglobswalks the sources again.Scannerfor stylesheets that aren't Tailwind roots.On our app (5,971 files to scan, React Router 8.5.1, Vite 8.3.6, macOS),
react-router buildgoes from 15.9 s to 13.2 s with identical output: the client build from 8.6 s to 7.8 s, and the SSR build from 6.5 s to 4.6 s.In a Linux container, the same files take 125 ms to scan and 21 ms for
globs, so the saving there is about 170 ms per build.This also keeps the SSR CSS the same as the client CSS.
When
build/isn't ignored, the SSR scan picked up the client build output, and React Router emitted a second stylesheet with a different hash, which looks like #19888.Dev mode and
vite build --watchbehave as before, including workers built during a watched build.The shared candidates are reset in
configResolvedand when an environment builds again, and only the first scan of a new scanner is shared.The
source(…)check still throws in the same cases.[ci-all]
Test plan
v8_viteEnvironmentApiand withoutbuild/in.gitignore, and expects one stylesheet. It fails onmainand passes here.pnpm run test:integrations ./integrations/vitepasses, and so does thewatch modetest with its fixture switched to Vite 8.main, apart from the extra stylesheet in the new test.source()listing files, a worker inworker.pluginsduringvite build --watch, a reused builder and a reused plugin instance all give the same CSS asmain.pnpm testandpnpm changelog checkpass.