Overview
Vite handles WebAssembly files automatically with minimal configuration, making it one of the easiest ways to get started with React ThorVG Fiber. This guide covers everything you need to know to set up and configure ThorVG in your Vite project.Installation
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
})
Vite provides a special
?url suffix for importing WASM files as URLs. This is the recommended approach:Advanced Configuration
TypeScript Configuration
If you’re using TypeScript, you may need to add a type declaration for WASM imports. Create avite-env.d.ts file:
Custom WASM Path
If you need to serve WASM files from a custom path (e.g., CDN), you can modify thelocateFile function:
Public Directory
Alternatively, you can place WASM files in thepublic directory and reference them directly:
node_modules/react-thorvg-fiber/dist/ to your public/wasm/ directory.
Build Optimization
Vite automatically handles WASM file chunking and optimization. For production builds, ensure your WASM files are properly included:dist folder with content hashing for cache busting.
Troubleshooting
WASM Import Errors
Problem:Cannot find module 'react-thorvg-fiber/thorvg-sw.wasm?url'
Solution: Ensure you’re using the ?url suffix when importing WASM files in Vite:
TypeScript Errors
Problem: TypeScript doesn’t recognize.wasm?url imports
Solution: Add the type declaration shown in the Advanced Configuration section above.
Canvas Not Rendering
Problem: Canvas appears blank or doesn’t render shapes Solution:- Verify the
locateFilecallback is properly implemented - Check browser console for WASM loading errors
- Ensure the WASM URL is accessible (check Network tab in DevTools)
- Verify you’re using
useCallbackto memoize thelocateFilefunction
Production Build Issues
Problem: WASM files not found in production build Solution:- Run
npm run buildand check thedistfolder for WASM files - Verify your server is configured to serve
.wasmfiles with the correct MIME type (application/wasm) - Check that WASM files are not being excluded by your
.gitignoreor build configuration
Development Server Issues
Problem: WASM files fail to load in development Solution:- Restart the Vite dev server:
npm run dev - Clear browser cache and reload
- Check that the WASM import path is correct
Next Steps
- Learn about WASM Configuration for advanced setup
- Explore Performance Tips to optimize rendering
- Check out Examples to see what’s possible