Skip to main content
Astro is built on top of Vite, a fast and modern build tool. You can pass additional Vite configuration options through the vite key in your Astro config.

Basic Vite Configuration

Add Vite configuration to your astro.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
Many popular tools have Astro integrations that handle Vite configuration for you. Check the integrations directory first.

Configuring SSR External Packages

Exclude problematic packages from SSR processing:
astro.config.mjs
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 update tsconfig.json):
astro.config.mjs
Then use in your code:
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
Only use define for non-secret values. Secrets should never be exposed to client code.

Optimizing Dependencies

Control which dependencies Vite pre-bundles:
astro.config.mjs
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
Exclude when:
  • 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
  • server.fs.allow: Restrict file system access for security
  • server.watch.usePolling: Enable polling for file watching (Docker, WSL)
  • server.proxy: Configure proxy for API requests
  • server.cors: Enable CORS for development

Proxy Configuration

Proxy API requests during development:
astro.config.mjs
Now requests to /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 base64
  • cssCodeSplit: 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
Run astro build to generate a visual report of your bundle.

Common Issues

Try adding the module to vite.optimizeDeps.include:
astro.config.mjs
Exclude large dependencies from optimization:
astro.config.mjs
Mark the package as external:
astro.config.mjs
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