Fix tsconfig path aliases not resolved in CSS url() references#17157
Merged
Conversation
…\nThe astro:tsconfig-alias-css transform plugin only handled @import\naliases but not url() references. In Vite 8, CSS url() resolution\ndoesn't go through user resolveId hooks, so aliases like\nurl('@assets/ok.png') were left unresolved.\n\nAdded cssUrlRE regex to also rewrite url() alias references in the\npre-transform hook, matching the behavior that was previously handled\nby vite.resolve.alias entries (removed in #17090).\n\nFixes #17156
1 task
|
akx
reviewed
Jun 23, 2026
Comment on lines
-117
to
-119
| // Fast early-exit: skip the regex if there's no @import anywhere in the file. | ||
| if (!code.includes('@import')) return; | ||
|
|
There was a problem hiding this comment.
I would retain the early-exit:
Suggested change
| // Fast early-exit: skip the regex if there's no @import anywhere in the file. | |
| if (!code.includes('@import')) return; | |
| // Fast early-exit: skip the regex if there's no mention of URLs anywhere in the file. | |
| if (!code.includes('url')) return; |
Comment on lines
87
to
+97
| /** | ||
| * Regex matching CSS @import statements with the specifier in capture group 1. | ||
| * https://regex101.com/?regex=%40import%5Cs%2B%28%3F%3Aurl%5C%28%5Cs*%29%3F%5B%27%22%5D%28%5B%5E%27%22%5D%2B%29%5B%27%22%5D%5Cs*%5C%29%3F&testString=&flags=g&flavor=pcre2&delimiter=%2F | ||
| */ | ||
| const cssImportRE = /@import\s+(?:url\(\s*)?['"]([^'"]+)['"]\s*\)?/g; | ||
|
|
||
| /** | ||
| * Regex matching CSS url() references with the specifier in capture group 1. | ||
| * Matches url('...') and url("...") but not @import url() (handled by cssImportRE). | ||
| */ | ||
| const cssUrlRE = /(?<!@import\s+)url\(\s*['"]([^'"]+)['"]\s*\)/g; |
There was a problem hiding this comment.
Not sure why we need cssImportRE and cssUrlRE -- @imports have the same url(...) syntax; one would do both, right?
matthewp
approved these changes
Jun 23, 2026
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.
Closes #17156
Changes
url()calls (e.g.background-image: url('@assets/ok.png')) now resolve correctly during build, restoring behavior from Astro 6.astro:tsconfig-alias-csspre-transform plugin previously only rewrote@importspecifiers. Added acssUrlREregex to also rewriteurl()references before Vite processes them. In Vite 8, CSSurl()resolution does not go through userresolveIdhooks, making the pre-transform rewrite the only viable approach.Testing
alias-css-url.test.ts) covering the regression: a<style>tag usingurl('@assets/ok.png')with a tsconfig@assets/*alias, verified to produce a resolved asset path in the build output.Docs