fix(css): rewrite urls on the live postcss root (fix #23348) - #23358
Open
chanmilee-fe wants to merge 2 commits into
Open
fix(css): rewrite urls on the live postcss root (fix #23348)#23358chanmilee-fe wants to merge 2 commits into
chanmilee-fe wants to merge 2 commits into
Conversation
Some PostCSS plugins hand back a freshly parsed tree by reassigning `result.root` at OnceExit instead of mutating the root in place (postcss-lightningcss, @tailwindcss/vite). PostCSS keeps passing the pre-reassignment root to the OnceExit hooks that run after it, so ever since vitejs#22983 moved the url rewriter from Once to OnceExit it has been walking a tree that never reaches the output. The emitted CSS kept the source-relative url() and the referenced asset was never emitted. Walk `result.root` instead. Moving back to Once would reintroduce the bug vitejs#22983 fixed, whereas reading the live root keeps both cases working. fix vitejs#23348 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Since #22983 the url rewriter runs in
OnceExit.Plugins like
postcss-lightningcssand@tailwindcss/vitedon't mutate the root there, they reassignresult.root.PostCSS keeps passing the old root to the
OnceExithooks that run after them, so we were rewriting a tree that gets thrown away:the emitted CSS keeps the source-relative
url()and the asset is never emitted at all.Walking
result.rootinstead fixes it. I considered going back toOnce, but that reintroduces what #22983 fixed. Reading the live root covers both cases.fixes #23348
#23349 was an earlier attempt at the same bug and landed on the same idea, but it was closed without review. Two things I hit while going down the same path:
result.rootback to therootparameter, which fails typecheck.result.rootisDocument | Rootwhile the parameter isRoot.So here the rewriter reads
result.rootdirectly, and the fixture lives in a subdirectory. The test adds a postcss plugin toplayground/cssthat swaps the root atOnceExit, and it fails in both serve and build without the patch.I used Claude Code for this. It wrote the patch and the test. I picked the issue, reviewed the diff, and decided what went in.
Two things from that review are worth calling out, since they're why the diff looks like this. The first patch reassigned the
rootparameter, which is what #23349 does and what its Lint job fails on, so it readsresult.rootdirectly instead.And the first version of the test passed with the bug still in place:
getComputedStylehands back an absolute url, so the raw./replaced-bg.pngnever appears and the assertion matched either way. That's why the fixture sits in a subdirectory now and the assertion looks for the emitted asset hash.Before trusting any of it I ran the test with the patch reverted in both serve and build, and checked that the existing
OnceExittest from #22983 still passes, since breaking that was the main risk.