8000 Fix Vite asset import rewriting for template literal strings by Oussemasahbeni · Pull Request #1010 · keycloakify/keycloakify · GitHub
[go: up one dir, main page]

Skip to content

Fix Vite asset import rewriting for template literal strings - #1010

Closed
Oussemasahbeni wants to merge 2 commits into
keycloakify:mainfrom
Oussemasahbeni:main
Closed

Oussemasahbeni wants to merge 2 commits into
keycloakify:mainfrom
Oussemasahbeni:main

Conversation

@Oussemasahbeni
@Oussemasahbeni Oussemasahbeni commented Mar 20, 2026 •
Copy link
Copy Markdown
Contributor

Fixes a JS asset rewrite miss in the Vite Keycloakify build flow.

replaceImportsInJsCode_vite was only rewriting asset imports when Vite emitted them as double-quoted string literals, for example:

"/assets/auth-logo-BR8ovgvV.svg"
Starting with Vite 8, the generated JS can emit the same asset path as a template literal instead:

`/assets/auth-logo-BR8ovgvV.svg`
image

When that happens, the rewrite step is skipped, so the generated Keycloak theme JS keeps /assets/... URLs instead of rewriting them to the Keycloak theme resources path.
image

This causes runtime-imported assets such as logos or SVG illustrations to 404 in Keycloak even though the files are bundled correctly in the theme JAR.

Example broken output:

src:n??`/assets/auth-logo-BR8ovgvV.svg`
var hu=`/assets/shape-DpIsiul3.svg`

In Keycloak, those assets need to resolve through:

${window.kcContext["x-keycloakify"].resourcesPath}/dist/assets/...

To fix that we need to xtend the Vite JS import replacer so it rewrites exact asset literals wrapped in any standard JS string delimiter:

  • double quotes: "..."
  • single quotes: '...'
  • backticks: `...`

Summary by CodeRabbit

  • Refactor

    • Improved string-literal handling so asset URL rewrites work reliably across double quotes 8000 , single quotes, and backticks.
    • More robust handling of variations in asset path forms.
  • Tests

    • Added new tests covering single-quote, backtick, and template-literal asset URL cases and different path variations to prevent regressions.

@coderabbitai
coderabbitai Bot commented Mar 20, 2026 •
Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7028e984-aa87-4d4b-b15f-a1b2ad37ffe9

📥 Commits

Reviewing files that changed from the base of the PR and between ff230cb and ba7c81e.

📒 Files selected for processing (2)
  • src/bin/keycloakify/replacers/replaceImportsInJsCode/vite.ts
  • test/bin/replacers.spec.ts

📝 Walkthrough

Walkthrough

Reordered imports in vite.ts and expanded asset-relative path replacement to handle double quotes, single quotes, and backticks. Added multiple tests covering template-literal and single-quote asset URL forms and variant urlPathname behaviors across posix/win32 cases.

Changes

Cohort / File(s) Summary
Vite JS replacer
src/bin/keycloakify/replacers/replaceImportsInJsCode/vite.ts
Swapped import ordering and changed asset-relative path replacement to iterate over three quote types (", ', `) performing replaceAll for direct and buildContext.urlPathname-prefixed string literals. No exported signatures changed.
Tests — JS replacer coverage
test/bin/replacers.spec.ts
Reordered some imports and added four new Vite JS replacer test cases (replaceImportsInJsCode_vite - 6 → - 9) exercising single-quote and template-literal asset URLs, urlPathname variations, and posix/win32 path behaviors.
Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped through quotes—single, double, backtick—
Rewriting paths with a nimble flick.
Tests now cover each curious case,
Resources find their proper place.
A tiny rabbit's tidy trick.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately and concisely describes the main change: extending Vite asset import rewriting to handle template literal strings (backticks) in addition to double-quoted strings.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@garronej garronej left a comment
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for the PR.

Please add a test case here: https://github.com/keycloakify/keycloakify/blob/main/test/bin/replacers.spec.ts

Thank you for your contribution.

@Oussemasahbeni
Copy link
Copy Markdown
Contributor Author

@garronej done

@garronej
Copy link
Copy Markdown
Collaborator

Let's create a release candidate to make sure the fix works

garronej added a commit that referenced this pull request Mar 21, 2026
@garronej
Copy link
Copy Markdown
Collaborator

Superseeded by #1012

@garronej garronej closed this Mar 21, 2026
garronej added a commit that referenced this pull request Mar 21, 2026
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.

2 participants

0