Skip to content

Ensure @tailwindcss/vite rebuilds when a plugin changes the input CSS in memory - #20700

Merged
RobinMalfait merged 2 commits into
mainfrom
robin/rebuild-vite-css-when-the-input-changes-in-memory
Nov 16, 2026
Merged

RobinMalfait merged 2 commits into
mainfrom
robin/rebuild-vite-css-when-the-input-changes-in-memory

Conversation

@RobinMalfait

Copy link
Copy Markdown
Contributor

Summary

@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.

@RobinMalfait
RobinMalfait requested a review from a team as a code owner November 10, 2026 23:00
@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Reviews (7) · Last reviewed commit: "Retry the Vite compiler setup after it f..." · Reviewed by Greptile

Comment thread packages/@tailwindcss-vite/src/index.ts Resolved
@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent 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: 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

Comment thread packages/@tailwindcss-vite/src/index.ts Resolved
@RobinMalfait RobinMalfait self-assigned this Nov 16, 2026
@RobinMalfait
RobinMalfait force-pushed the robin/rebuild-vite-css-when-the-input-changes-in-memory branch from 9496a08 to b10a01c Compare November 16, 2026 17:51
`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.
@RobinMalfait
RobinMalfait force-pushed the robin/rebuild-vite-css-when-the-input-changes-in-memory branch from b10a01c to 57bf25d Compare November 16, 2026 17:57
@RobinMalfait
RobinMalfait merged commit bb3fe16 into main Nov 16, 2026
13 checks passed
@RobinMalfait
RobinMalfait deleted the robin/rebuild-vite-css-when-the-input-changes-in-memory branch November 16, 2026 18:04
RobinMalfait added a commit to irontaek/tailwindcss that referenced this pull request Nov 23, 2026
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.
RobinMalfait added a commit to irontaek/tailwindcss that referenced this pull request Nov 23, 2026
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.
RobinMalfait added a commit that referenced this pull request Nov 23, 2026
`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]>
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.

1 participant