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
Ensure @tailwindcss/vite rebuilds when a plugin changes the input CSS in memory - #20700
@tailwindcss/vite reuses its compiler when the mtimes of the build dependencies (the input file and its @import/@config/@plugin graph) did not change. It never compares the CSS that it receives in transform. So when a plugin that runs before Tailwind changes the CSS in memory, and no file of the stylesheet changes on disk, Tailwind returns CSS from the old input.
An example: a plugin with enforce: 'pre' inlines src/partial.css into src/index.css and calls this.addWatchFile(). After an edit of src/partial.css, Vite transforms src/index.css again, but the CSS still has the old partial. This happens in vite dev, in vite build --watch, and in a kept createBuilder() builder that builds again.
This PR keeps the input CSS of the last compiler setup on the Root, and sets up the compiler again when the input differs. The first transform does no extra work, because there is no compiler yet.
This is the same fix as for @tailwindcss/postcss in #20310, and for @tailwindcss/webpack and @tailwindcss/turbopack in #20582.
Test plan
Added integration tests in integrations/vite/other-transforms.test.ts for vite dev, vite build --watch, and a createBuilder() builder that builds client two times. A plugin before Tailwind inlines a partial in memory, and each test changes the partial from red to blue.
Confirmed that all three fail on main (the CSS keeps color: red), and pass with the fix.
Ran the other tests in integrations/vite/other-transforms.test.ts, and the other Vite integration tests, and pnpm run build.
Review was skipped as selected files did not have any reviewable changes.
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: ce2ca1ce-78cd-4c93-b8c9-4553ba73dce8
Commits
Reviewing files that changed from the base of the PR and between b10a01c and 57bf25d.
You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.
Use the checkbox below for a quick retry:
Trigger review
No actionable comments were generated in the recent review.
Walkthrough
The Vite integration now stores the CSS input used for its last successful compiler setup. It rebuilds the compiler and scanner when that input changes. New tests cover in-memory CSS changes in development mode, watch mode, and repeated builds with a reused Vite builder. The changelog documents the fix.
Merge Risk: Minimal · previous review b10a0
This change makes the Vite plugin rebuild when an earlier plugin changes the CSS input in memory. It fixes stale CSS output, and no merge-blocking risk was found.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: @tailwindcss/vite now rebuilds when a plugin that runs before Tailwind changes the input CSS in memory. The raw summary confirms Root stores the last compiler-setup in…
Description check
The description explains the stale-CSS problem, the cause (mtime-based compiler reuse without comparing the input CSS in transform), the fix (storing input CSS on Root and re-setting up the compiler w…
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.
Full details: Title check
Explanation
The title names the main change: @tailwindcss/vite now rebuilds when a plugin that runs before Tailwind changes the input CSS in memory. The raw summary confirms Root stores the last compiler-setup input CSS and reruns setup when it differs. The title matches the changeset.
Full details: Description check
Explanation
The description explains the stale-CSS problem, the cause (mtime-based compiler reuse without comparing the input CSS in transform), the fix (storing input CSS on Root and re-setting up the compiler when it differs), and the tests added for dev, build --watch, and createBuilder(). This matches the raw summary.
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: bb872108-818a-47a7-8f89-b4a01deaa57b
Commits
Reviewing files that changed from the base of the PR and between 9496a08 and b10a01c.
Files selected for processing: 1
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.
Actionable comments posted: 1
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: 9433d031-dc32-4ea5-9681-39e82e78d315
Commits
Reviewing files that changed from the base of the PR and between 198a24a and e4f3d12.
Files selected for processing: 3
.changelog/vite-in-memory-input-changes.md
integrations/vite/other-transforms.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.
`Root.generate()` reused the compiler when the mtimes of the build
dependencies did not change. It never compared the `content` that it
received. So when a plugin that runs before Tailwind changes the CSS in
memory (e.g. it inlines a partial and calls `this.addWatchFile()`), and
no file of the stylesheet changes on disk, Tailwind returned the CSS of
the old input. This happened in `vite dev`, in `vite build --watch`, and
in a kept `createBuilder()` builder that builds again.
Keep the input of the last compiler setup on the `Root`, and set up the
compiler again when the input differs. The first transform does no extra
work, because there is no compiler yet.
This is the same fix as for `@tailwindcss/postcss` in
#20310, and for
`@tailwindcss/webpack` and `@tailwindcss/turbopack` in
#20582.
The previous commit recorded the input CSS before `compile()` ran. When
the setup threw (e.g. a plugin before Tailwind inlined `@apply` of an
unknown utility), the old compiler stayed, and `this.content` held the
failed input. So the next transform with the same input skipped the
setup and returned the CSS of the last valid input, without an error.
Clear `this.content` when the setup starts, and set it only after the
setup succeeds. Then the next transform after a failure always sets up
the compiler again. This also covers a revert to the last valid input,
because a failed setup already cleared the build dependencies.
Since tailwindlabs#20700,
`Root.generate()` compares the input CSS with the input of the last
compiler setup. So an edit of the input stylesheet always sets up the
compiler again, also when the `stat` of the input is still pending. The
tests that this PR added in `integrations/vite/other-transforms.test.ts`
("a plugin that changes the input CSS in memory") cover that behavior.
On `main`, this test failed without the `await` only at its check on the
timing of the first response. Its stale CSS check passed. It also waited
a fixed 5 s on each run, could pass falsely on a slow runner, and found
the `stat` of the input by its stack trace.
Keep the `await`: it removes a floating promise, so `buildDependencies`
is complete when `generate()` returns.
Since tailwindlabs#20700,
`Root.generate()` compares the input CSS with the input of the last
compiler setup. So an edit of the input stylesheet always sets up the
compiler again, also when the `stat` of the input is still pending. The
tests that this PR added in `integrations/vite/other-transforms.test.ts`
("a plugin that changes the input CSS in memory") cover that behavior.
On `main`, this test failed without the `await` only at its check on the
timing of the first response. Its stale CSS check passed. It also waited
a fixed 5 s on each run, could pass falsely on a slow runner, and found
the `stat` of the input by its stack trace.
Keep the `await`: it removes a floating promise, so `buildDependencies`
is complete when `generate()` returns.
`Root.generate()` recorded the modification time of the input stylesheet
without awaiting it. This PR awaits that `stat` before it compiles, so
no promise is left floating, and the input is in `buildDependencies`
when `generate()` returns.
Since #20700,
`generate()` also compares the input CSS with the input of the last
compiler setup. So an edit of the input stylesheet already sets up the
compiler again, also when this `stat` is still pending. The tests of
that PR in `integrations/vite/other-transforms.test.ts` cover that
behavior, so this PR adds no test.
## Test plan
- The `a plugin that changes the input CSS in memory` tests in
`integrations/vite/other-transforms.test.ts` pass.
- The `dev mode` tests in `integrations/vite/index.test.ts` pass.
---------
Co-authored-by: irontaek <[email protected]>
Co-authored-by: Robin Malfait <[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
@tailwindcss/vitereuses its compiler when the mtimes of the build dependencies (the input file and its@import/@config/@plugingraph) did not change. It never compares the CSS that it receives intransform. So when a plugin that runs before Tailwind changes the CSS in memory, and no file of the stylesheet changes on disk, Tailwind returns CSS from the old input.An example: a plugin with
enforce: 'pre'inlinessrc/partial.cssintosrc/index.cssand callsthis.addWatchFile(). After an edit ofsrc/partial.css, Vite transformssrc/index.cssagain, but the CSS still has the old partial. This happens invite dev, invite build --watch, and in a keptcreateBuilder()builder that builds again.This PR keeps the input CSS of the last compiler setup on the
Root, and sets up the compiler again when the input differs. The first transform does no extra work, because there is no compiler yet.This is the same fix as for
@tailwindcss/postcssin #20310, and for@tailwindcss/webpackand@tailwindcss/turbopackin #20582.Test plan
integrations/vite/other-transforms.test.tsforvite dev,vite build --watch, and acreateBuilder()builder that buildsclienttwo times. A plugin before Tailwind inlines a partial in memory, and each test changes the partial fromredtoblue.main(the CSS keepscolor: red), and pass with the fix.integrations/vite/other-transforms.test.ts, and the other Vite integration tests, andpnpm run build.