Back to all fixes
CuratedTypeScriptVite 8local
Dep-optimized WASM package breaks: "CompileError: wasm validation error: failed to match magic number"
Problem
A package that does `new URL('file.wasm', import.meta.url)` (common in WASM-using libs) works in production builds but breaks in dev, because Vite's dependency optimizer copies the JS into `node_modules/.vite/deps/` without copying or rewriting the referenced `.wasm` file path. Browser console shows `Uncaught (in promise) CompileError: wasm validation error: at offset 4: failed to match magic number`, and network tab shows a 404 for `node_modules/.vite/deps/<name>.wasm`. Related pattern also breaks `new Worker(new URL(...))` with a MIME-type/404 error.
Solution
typescript
This is a known dep-optimizer limitation shared with classic Vite (vitejs/vite#8427); rolldown-vite's reimplemented optimizer has the same gap and there is no automatic fix — the maintainers say a plugin that rewrites the `new URL` call during optimization would be needed and welcomed a PR, but none has landed. Two practical workarounds: (1) if the WASM/worker file comes from your own package, publish it under a distinct subpath export (e.g. `"./workers/*": "./src/workers/*"`) and add just that subpath to `optimizeDeps.exclude` so it's skipped by the optimizer while the rest of the package is still optimized; (2) if you control the esbuild plugin used for the optimizer (e.g. an import-meta-url plugin), make sure it's registered under `optimizeDeps.esbuildOptions.plugins`, not `build.esbuildOptions.plugins` — the latter doesn't exist and silently does nothing, which was the actual cause of one reporter's Monaco-editor breakage.