Back to all fixes
CuratedTypeScriptVite 8local

typeof asset === 'string' check always false in production build with Vite asset placeholders + inlineConst

Problem

Production-only bug (does not reproduce in dev): a runtime `typeof` check against an imported asset always evaluates to false, so the wrong code branch runs and later crashes accessing properties like `.sources`/`.img.src`. Seen with SvelteKit + `@sveltejs/enhanced-img` importing an SVG via `?enhanced`. The built chunk contains malformed code such as `if ("string" + new URL(...).href === "string")` instead of a correct string comparison.

Solution

typescript
Confirmed workaround: disable rolldown's constant-inlining optimization in vite.config: `build: { rolldownOptions: { optimization: { inlineConst: false } } }`. Root cause: rolldown's `inlineConst` substitutes a Vite asset placeholder string directly into a `typeof` operand; Vite's later text-based runtime-URL substitution (`"+${runtime}+"`) then gets misparsed because `typeof` binds tighter than `+`. This is tracked as a Vite-side bug (proposed fix: wrap Vite's substitution in parentheses) and was not fixed at time of writing — use `inlineConst: false` until Vite/rolldown ship a permanent fix.