Astro uses Vite as its build tool and dev server. astro dev starts a Vite dev server with hot module replacement, and astro build runs Vite builds that bundle your code for the server and the browser. Astro adds its own Vite plugins on top, most importantly one that compiles .astro files, so anything Vite supports (CSS imports, import.meta.env, import.meta.glob, Vite plugins) works in an Astro project.
Astro 7 runs on Vite 8, which bundles with Rolldown and uses Vite’s environments API to build server-rendered pages and browser code separately. This explainer covers what that means in practice and how to change Vite’s configuration from astro.config.mjs without breaking anything.
Key Takeaways
- Astro is built on Vite: the dev server, HMR, module resolution, CSS handling and bundling all come from Vite.
- Astro registers its own Vite plugins to compile
.astrocomponents, handle Markdown, generate routes and wire up islands.- In Astro 7 (Vite 8), builds run in separate environments:
prerender,ssrandclient, bundled by Rolldown.- Customize Vite through the
vitekey inastro.config.mjs. Prefer an Astro integration when one exists for the tool.- Vite features such as
import.meta.env,import.meta.glob,?rawand?urlimports, and CSS modules work directly in Astro.
Why Astro is built on Vite
Vite handles the parts every modern framework needs and none want to rebuild: a dev server that serves native ES modules, near-instant hot updates, dependency pre-bundling, CSS processing, and a production bundler. By building on Vite, Astro inherits all of that and its plugin ecosystem, and can focus on what’s specific to Astro: the component format, routing, content collections, islands and static generation.
It’s also why Astro supports React, Vue, Svelte, Solid and Preact components side by side. Each framework already has a Vite plugin; Astro’s framework integrations add that plugin plus a renderer that tells Astro how to render the component to HTML and hydrate it.
The dev server: astro dev
When you run astro dev, Astro creates a Vite dev server with its own plugins and your vite config merged in. A request for /blog/my-post/ goes through Astro’s routing middleware on that server:
- Astro matches the URL to a route in
src/pages/. - It loads the page module through Vite, which compiles the
.astrofile (and anything it imports) on demand. - It renders the page to HTML on the server side of the dev server and returns it.
- The browser loads the page’s client scripts and CSS as individual ES modules served by Vite.
Nothing is bundled in development. Each module is transformed when requested and cached. That’s why dev startup is fast regardless of project size, and also why dev performance numbers mean nothing: hundreds of unbundled modules behave nothing like production.
Hot module replacement
Edit a CSS file and Vite swaps the styles in place. Edit a React or Svelte island and the framework’s HMR updates the component without losing state where possible. Edit an .astro component and Astro re-renders the page; since .astro components are server-only, the update is a fresh render rather than a client-side patch.
Changes to astro.config.mjs restart the server. Changes to content files update the content store and re-render pages that use them.
The build: astro build
Production builds use Vite’s bundler. In Astro 7, that’s Vite 8 with Rolldown, a Rust-based bundler designed as a faster drop-in for Rollup.
Astro uses Vite’s environments API to run more than one build in a single process:
| Environment | Output | Purpose |
|---|---|---|
prerender |
Server-side bundle, deleted after use | Renders static pages to HTML during the build |
ssr |
dist/server/ |
Server entrypoint for on-demand routes (only with an adapter) |
client |
dist/_astro/ (or dist/client/_astro/) |
JavaScript, CSS and assets for the browser |
The client build produces content-hashed filenames, splits island code into separate chunks per component, and deduplicates shared dependencies. Each page then references only the chunks it uses, which is how a page with no islands ends up with no JavaScript at all. The Astro build process explainer walks through every phase in order.
Astro’s own Vite plugins
Look inside node_modules/astro/dist/ and you’ll find a set of vite-plugin-* folders. A few of the important ones:
| Plugin | What it does |
|---|---|
vite-plugin-astro |
Compiles .astro files to JavaScript modules with the Astro compiler, and extracts their styles and scripts |
vite-plugin-markdown |
Turns .md files into page modules with frontmatter and rendered HTML |
vite-plugin-routes, vite-plugin-pages |
Build the route manifest from src/pages/ |
vite-plugin-scripts |
Bundles component <script> tags and injected scripts |
vite-plugin-head |
Ensures styles and scripts end up in the document head |
vite-plugin-config-alias |
Applies path aliases from tsconfig.json |
vite-plugin-environment |
Handles environment variables and import.meta.env |
vite-plugin-renderers |
Registers framework renderers from integrations |
When Vite requests src/components/Card.astro, vite-plugin-astro runs the Astro compiler on it. The output is a JavaScript module whose default export renders HTML, plus virtual modules for each <style> and <script> block. Vite then treats those like any other CSS or TypeScript file, which is why Sass, PostCSS and Tailwind CSS work inside component styles.
Vite features you can use directly
Because the build tool is Vite, these work in any Astro project without configuration.
Environment variables. Variables in .env files are available on import.meta.env. Astro sets Vite’s envPrefix to PUBLIC_, so only PUBLIC_* variables are exposed to client code:
---
// src/components/Map.astro
const key = import.meta.env.PUBLIC_MAPS_KEY; // available in browser code too
const secret = import.meta.env.MAPS_SECRET; // server-side only
---
For typed, validated variables, Astro’s astro:env builds on this. The environment variables guide covers both.
Glob imports. import.meta.glob loads many files at once:
// src/pages/icons.astro (frontmatter)
const icons = import.meta.glob('../icons/*.svg', { eager: true, query: '?raw', import: 'default' });
For content, use content collections instead, which add schemas and types. The import.meta.glob tip covers when glob imports still make sense.
Import suffixes. ?raw imports a file as a string, ?url gives its built URL:
import licenseText from '../../LICENSE?raw';
import workerUrl from '../scripts/worker.ts?url';
CSS modules. Name a file Card.module.css and import it to get locally scoped class names. Astro’s own scoped <style> blocks usually make this unnecessary, but it’s useful in framework components.
Static assets. Importing an image, font or other file returns its built URL (for images, Astro’s astro:assets adds dimensions and optimization on top).
Customizing Vite from astro.config.mjs
Astro exposes Vite’s config through the vite key. Astro merges it with its own settings:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import tailwindcss from '@tailwindcss/vite';
export default defineConfig({
vite: {
plugins: [tailwindcss()],
css: {
preprocessorOptions: {
scss: { additionalData: `@use "src/styles/tokens" as *;` },
},
},
server: {
allowedHosts: ['.ngrok-free.app'],
},
},
});
That Tailwind CSS example is exactly what npx astro add tailwind writes for Tailwind v4: Tailwind’s official Vite plugin in vite.plugins. The Tailwind CSS setup guide covers the rest.
Commonly changed options:
vite.plugins: add Vite plugins for tools that don’t have an Astro integration.vite.css: preprocessor options, PostCSS config, CSS modules naming.vite.server: allowed hosts, proxy rules for local APIs, file watching options.vite.ssr.noExternal: force a dependency to be bundled for the server when it ships uncompiled code (some UI libraries need this).vite.define: replace global constants at build time.
Options to leave alone
Some Vite options are controlled by Astro and changing them causes confusing failures: root, base (use Astro’s base instead), build.outDir (use outDir), envPrefix (Astro sets it), and the build input settings. When an Astro config option exists for something, use that rather than its Vite equivalent.
Integration or Vite plugin?
If an Astro integration exists for a tool, prefer it. Integrations can add the Vite plugin and also hook into Astro’s routing, rendering and build steps. Use vite.plugins directly when the tool only needs to transform files, as with Tailwind v4 or an SVG loader.
Writing a small Vite plugin for Astro
A Vite plugin is an object with hooks. This one exposes the build time as a virtual module:
// astro.config.mjs
import { defineConfig } from 'astro/config';
function buildInfo() {
const id = 'virtual:build-info';
return {
name: 'build-info',
resolveId(source) {
if (source === id) return '\0' + id;
},
load(resolved) {
if (resolved === '\0' + id) {
return `export const builtAt = ${JSON.stringify(new Date().toISOString())};`;
}
},
};
}
export default defineConfig({
vite: { plugins: [buildInfo()] },
});
---
// src/components/Footer.astro
import { builtAt } from 'virtual:build-info';
---
<footer>Last built {new Date(builtAt).toLocaleDateString('en-US')}</footer>
For anything that should also be reusable across projects or touch Astro’s lifecycle, wrap it in an integration with the astro:config:setup hook and its updateConfig() helper.
Debugging Vite in Astro
astro dev --verboseandastro build --verboseprint Vite’s debug logs, including how modules resolve.- A “Failed to resolve import” error comes from Vite’s resolver: check the path, file extension and aliases.
- Errors mentioning “optimize deps” or “pre-bundling” in dev often clear after deleting
node_modules/.viteand restarting. - “Cannot use import statement outside a module” during build usually means a dependency needs
vite.ssr.noExternal.
Every Komal theme is a standard Astro 7 project with a stock Vite setup, and the Astro config docs show where its vite options live; browse Astro themes if you want a working project to experiment in. The official Astro configuration reference for vite and the Vite documentation cover every option.
FAQ
Does Astro use Vite?
Yes. Astro is built on Vite. astro dev runs a Vite dev server and astro build runs Vite builds, with Astro’s own plugins added to compile .astro files, generate routes and handle islands.
Which version of Vite does Astro 7 use?
Astro 7 depends on Vite 8, which uses Rolldown as its bundler and the environments API for separate server and client builds. Check node_modules/vite/package.json for the exact version installed in your project.
How do I add a Vite plugin to Astro?
Add it to vite.plugins in astro.config.mjs. If the tool has an official Astro integration, use that instead, since it can also hook into Astro’s build and routing.
Can I use vite.config.js with Astro?
No. Astro doesn’t read a separate vite.config.js. Put Vite options under the vite key in astro.config.mjs, and Astro merges them with its own configuration.
Why is the Astro dev server fast but the build slower?
The dev server transforms modules only when requested and doesn’t bundle anything. The build bundles and optimizes all code, renders every page to HTML and processes images, which is more work in total.