Skip to content

[FIX] Handle missing plugin asset imports in Vite build#1818

Merged
hari-kuriakose merged 3 commits intomainfrom
fix/vite-plugin-asset-imports
Mar 2, 2026
Merged

[FIX] Handle missing plugin asset imports in Vite build#1818
hari-kuriakose merged 3 commits intomainfrom
fix/vite-plugin-asset-imports

Conversation

@vishnuszipstack
Copy link
Contributor

What

  • Fix Vite optionalPluginImports plugin to handle missing asset imports (SVG, PNG, JPEG, etc.) gracefully by exporting a default empty string instead of throwing

Why

  • On-prem frontend builds fail because ProductContentComponents.jsx (a cloud plugin) uses static imports for asset files from plugins/assets/llmWhisperer/
  • The on-prem build workflow removes plugins/assets/llmWhisperer/, so these assets are missing
  • The existing Vite plugin resolves all missing plugin files to a module that only throws — this works for dynamic try/catch imports but breaks static imports because Rollup requires a default export
  • Error: "default" is not exported by "optional-plugin-empty"

How

  • Introduce a separate empty module ID (EMPTY_ASSET_MODULE_ID) for asset file imports
  • When a missing plugin file has an asset extension (.svg, .png, .jpg, .jpeg, .gif, .webp, .ico, .bmp, .tiff), resolve it to a module that does export default ''
  • JS/TS plugin imports continue to throw so the existing try { await import(...) } catch {} pattern still works

Can this PR break any existing features. If yes, please list possible items. If no, please explain why. (PS: Admins do not merge the PR without this section filled)

  • No. This only changes how missing asset files are handled during the build. Existing assets that are present on disk are unaffected — the plugin returns null for those and Vite processes them normally. Missing JS/TS plugin imports still throw as before.

Database Migrations

  • None

Env Config

  • None

Relevant Docs

  • None

Related Issues or PRs

Dependencies Versions

  • None

Notes on Testing

  • Verify on-prem frontend Docker build succeeds
  • Verify cloud frontend build is unaffected
  • Verify existing dynamic plugin imports with try/catch still work

Screenshots

  • N/A

Checklist

I have read and understood the Contribution Guidelines.

The optionalPluginImports Vite plugin resolved all missing plugin files
to a module that only throws, which breaks static imports (no default
export). Asset imports (SVG, PNG, etc.) now resolve to a module that
exports an empty string, so static `import logo from "..."` statements
don't crash the build when assets are absent.

JS/TS plugin imports still throw so the existing try/catch dynamic
import pattern continues to work.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Cache: Disabled due to Reviews > Disable Cache setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 637ac04 and 2fa0e10.

📒 Files selected for processing (1)
  • frontend/vite.config.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • frontend/vite.config.js

Summary by CodeRabbit

  • New Features
    • Adds support for importing static assets so missing asset references no longer break builds.
  • Bug Fixes
    • Improved resolution logic for asset imports (handles query/hash, relative paths, and common extensions) to prevent build-time failures.
  • Documentation
    • Clarified asset-handling behavior to explain how static imports are treated during build.

Walkthrough

Adds asset-import handling to the Vite config: normalizes import paths, recognizes asset extensions, returns a sentinel EMPTY_ASSET_MODULE_ID for missing assets, and emits a default empty-string export for those asset modules to avoid build failures.

Changes

Cohort / File(s) Summary
Asset Import Configuration
frontend/vite.config.js
Adds EMPTY_ASSET_MODULE_ID and ASSET_EXTENSIONS; extends the optional-plugin-imports logic to normalize source paths (strip query/hash), resolve relative imports with common extensions, check for file existence, and return EMPTY_ASSET_MODULE_ID for missing asset files. Adds loader handling to emit export default '' when id matches EMPTY_ASSET_MODULE_ID.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The PR title clearly and concisely describes the main change: fixing Vite's handling of missing plugin asset imports during the build process.
Description check ✅ Passed The PR description comprehensively addresses all required template sections including What, Why, How, impact analysis, and testing guidance, with clear and detailed explanations.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/vite-plugin-asset-imports

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@frontend/vite.config.js`:
- Around line 57-60: The resolveId and load hooks are using
path.extname(resolved) directly so imports with queries/hashes like
"./logo.svg?react" yield ".svg?react" and miss ASSET_EXTENSIONS; update
resolveId (the code using path.extname and ASSET_EXTENSIONS) to strip query
string and hash from resolved before calling path.extname (e.g. remove anything
from the first "?" or "#" onward), and make the same change in the load hook
where EMPTY_ASSET_MODULE_ID vs EMPTY_MODULE_ID is decided so that assets with
query params/hashes are correctly detected and return EMPTY_ASSET_MODULE_ID.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Cache: Disabled due to Reviews > Disable Cache setting

Knowledge base: Disabled due to Reviews -> Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 073146d and 637ac04.

📒 Files selected for processing (1)
  • frontend/vite.config.js

path.extname() on query-suffixed imports like "./logo.svg?react" returns
".svg?react" instead of ".svg", causing the ASSET_EXTENSIONS check to
fail and routing asset imports to the throwing module instead of the
empty-string default export. Strip query strings and hashes before
resolving the path.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2026

Frontend Lint Report (Biome)

All checks passed! No linting or formatting issues found.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 2, 2026

@hari-kuriakose hari-kuriakose merged commit 1cad9b7 into main Mar 2, 2026
7 checks passed
@hari-kuriakose hari-kuriakose deleted the fix/vite-plugin-asset-imports branch March 2, 2026 13:10
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.

4 participants