fix: transform CSS url() references in ?url imports for bundledDev (fix #22863)#22880
Open
AmariahAK wants to merge 2 commits into
Open
fix: transform CSS url() references in ?url imports for bundledDev (fix #22863)#22880AmariahAK wants to merge 2 commits into
AmariahAK wants to merge 2 commits into
Conversation
vitejs#22863) When bundledDev is enabled, CSS files imported with ?url had their url() references left untransformed because: - The CSS load hook returned undefined (delegating to the asset plugin) - The CSS transform hook excluded ?url via SPECIAL_QUERY_RE - The asset plugin emitted the raw CSS as-is Fix by handling ?url CSS in the CSS plugin's load hook for bundledDev: read the file, run through compileCSS to transform url() references, emit the transformed CSS as a Rolldown asset, and return the output URL. Co-authored-by: atlarix-agent <agent@atlarix.dev>
Author
|
@sapphi-red kindly review |
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.
PR Description
What is this PR solving?
When
bundledDev: trueis enabled, CSS files imported with?urldon't have theirurl()references transformed. Assets referenced inside the CSS (e.g.,url("./vite.webp")) resolve to incorrect paths that the dev server doesn't serve.Root cause: Three code paths converge:
loadhook — returnedundefinedfor?urlCSS in dev mode, delegating to the asset plugin which reads the raw filetransformfilter — excludes?urlviaSPECIAL_QUERY_RE, socompileCSS()URL rewriting never runsfileToBuiltUrl— in bundledDev, emits the raw CSS as-is viaemitFile(), andmemoryFilesMiddlewareserves it directly without any transformIn normal dev mode this works because
fileToDevUrlreturns the raw file path — the browser then makes a follow-up request that goes throughtransformMiddleware, which invokes the CSS transform. In bundledDev there's no follow-up request; the raw CSS is served directly from in-memory Rolldown output.Fixes #22863.
What other alternatives have you explored?
fileToBuiltUrl(asset plugin): Would import CSS transformation logic into the asset plugin — cross-cutting concern, violates single responsibility. Rejected.?urlfrom the CSS transform filter exclusion: The transform handler outputs CSS JS-module code (applying styles), not URL exports. Would require two fundamentally different code paths in one handler. Rejected.loadhook (chosen): Mirrors the existing build-mode?urlhandling pattern; all CSS logic stays in one plugin;emitFile/getFileNameare natively available in Rolldownloadhooks; minimal, non-invasive diff.Are there any parts you think require more attention from reviewers?
?urlimports in bundledDev. A file change will require a full page reload (which already happens for the importing module). HMR can be addressed as a follow-up.preprocessorWorkerController: The load hook usespreprocessorWorkerController!(non-null assertion), matching the existing pattern in the transform handler. In bundledDev,buildStartis called by the Rolldown DevEngine before anyloadhooks, so this is safe.fileToUrltype cast: The sharedcreateCssUrlResolverhelper usespluginContext as anyfor thefileToUrlcall becausethishas different types in bundledDev (RolldownPluginContext) vs normal dev (ViteTransformPluginContext). This is an internal helper, not a public API.