vite key in your Astro config.
Basic Vite Configuration
Add Vite configuration to yourastro.config.mjs:
astro.config.mjs
Astro automatically configures Vite with sensible defaults. Only add custom Vite config when you need to override or extend the defaults.
Common Use Cases
Adding Vite Plugins
Extend Vite with community plugins:astro.config.mjs
Configuring SSR External Packages
Exclude problematic packages from SSR processing:astro.config.mjs
When to use ssr.external
When to use ssr.external
Use this when a package:
- Has CommonJS modules that break during SSR
- Uses Node.js built-ins that need to be excluded
- Has incompatible dependencies
- Causes “Cannot use import statement outside a module” errors
Adding Module Aliases
Create import aliases (TypeScript users should also updatetsconfig.json):
astro.config.mjs
src/pages/index.astro
Astro provides
@/* as an alias to src/* by default. For TypeScript projects, also configure path mappings in tsconfig.json.Environment Variables
Customize which environment variables are exposed to your client-side code:astro.config.mjs
Optimizing Dependencies
Control which dependencies Vite pre-bundles:astro.config.mjs
When to optimize dependencies
When to optimize dependencies
Include when:
- A package is linked locally and not being detected
- A package has many internal modules that slow down dev server
- You’re experiencing slow initial page loads
- A package is very large and slows down the dev server startup
- A package has conditional imports that break when pre-bundled
Advanced Configuration
Build Options
Customize the build output:astro.config.mjs
Vite uses Rollup for production builds. Use
rollupOptions to configure Rollup directly.CSS Configuration
Configure CSS processing:astro.config.mjs
Server Options
Configure the Vite dev server:astro.config.mjs
Common server options
Common server options
server.fs.allow: Restrict file system access for securityserver.watch.usePolling: Enable polling for file watching (Docker, WSL)server.proxy: Configure proxy for API requestsserver.cors: Enable CORS for development
Proxy Configuration
Proxy API requests during development:astro.config.mjs
/api/users are proxied to http://localhost:3000/users.
Framework-Specific Configuration
React
Customize React plugin options:astro.config.mjs
Vue
Configure Vue plugin:astro.config.mjs
Performance Optimization
Code Splitting
Optimize bundle size with manual chunks:astro.config.mjs
Benefits
- Smaller initial bundle
- Better caching
- Parallel downloads
Trade-offs
- More HTTP requests
- Complex configuration
- Overhead for small sites
Asset Optimization
Configure asset handling:astro.config.mjs
assetsInlineLimit: Assets smaller than this are inlined as base64cssCodeSplit: Enable CSS code splitting
Environment-Specific Configuration
Configure Vite differently for dev vs. build:astro.config.mjs
The config function receives
command (“dev” | “build” | “preview”) and mode (“development” | “production”).Debugging Vite
Enable Debug Logging
astro.config.mjs
Inspect Bundle
Use the rollup-plugin-visualizer to analyze your bundle:astro.config.mjs
astro build to generate a visual report of your bundle.
Common Issues
Module not found errors
Module not found errors
Try adding the module to
vite.optimizeDeps.include:astro.config.mjs
Slow dev server startup
Slow dev server startup
Exclude large dependencies from optimization:
astro.config.mjs
SSR errors with CommonJS packages
SSR errors with CommonJS packages
Mark the package as external:
astro.config.mjs
File watching not working (Docker/WSL)
File watching not working (Docker/WSL)
Enable polling:
astro.config.mjs
Complete Example
Here’s a comprehensive Vite configuration example:astro.config.mjs
Learn More
Vite Documentation
Official Vite configuration reference
Astro Config
Complete Astro configuration reference
Integrations
Learn about Astro integrations
Build Options
Astro build configuration