Skip to content

Fix tsconfig path aliases not resolved in CSS url() references#17157

Merged
matthewp merged 1 commit into
mainfrom
flue/fix-17156
Jun 23, 2026
Merged

Fix tsconfig path aliases not resolved in CSS url() references#17157
matthewp merged 1 commit into
mainfrom
flue/fix-17156

Conversation

@astrobot-houston

Copy link
Copy Markdown
Contributor

Closes #17156

Changes

  • Tsconfig path aliases in CSS url() calls (e.g. background-image: url('@assets/ok.png')) now resolve correctly during build, restoring behavior from Astro 6.
  • The astro:tsconfig-alias-css pre-transform plugin previously only rewrote @import specifiers. Added a cssUrlRE regex to also rewrite url() references before Vite processes them. In Vite 8, CSS url() resolution does not go through user resolveId hooks, making the pre-transform rewrite the only viable approach.

Testing

  • New fixture and test (alias-css-url.test.ts) covering the regression: a <style> tag using url('@assets/ok.png') with a tsconfig @assets/* alias, verified to produce a resolved asset path in the build output.

Docs

  • No docs update needed; this restores existing documented behavior for tsconfig aliases in CSS.

…\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
@astrobot-houston astrobot-houston added the fix verified Reporter confirmed the triage bot fix works label Jun 23, 2026
@changeset-bot

changeset-bot Bot commented Jun 23, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: dd31daf

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added the pkg: astro Related to the core `astro` package (scope) label Jun 23, 2026
@codspeed-hq

codspeed-hq Bot commented Jun 23, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing flue/fix-17156 (dd31daf) with main (ccceda3)

Open in CodSpeed

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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why we need cssImportRE and cssUrlRE -- @imports have the same url(...) syntax; one would do both, right?

@matthewp matthewp merged commit 4766f37 into main Jun 23, 2026
32 checks passed
@matthewp matthewp deleted the flue/fix-17156 branch June 23, 2026 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix verified Reporter confirmed the triage bot fix works pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tsconfig aliases aren't resolved for <style> in Astro 7

3 participants