Most real projects need aliases and a proxy to talk to their backend during development.
vite.config.ts (or .js/.mjs) exports a config object, ideally wrapped in defineConfig for type hints. It can also export a function receiving { command, mode } to vary settings between serve and build.
Frequently used options:
plugins: framework and feature plugins, e.g.react().resolve.alias: map@tosrc/so imports become@/components/Button. Mirror it intsconfig.jsonpathsfor TypeScript (or use theresolve.tsconfigPathsoption / a plugin).base: the public path when the app is deployed under a sub-path, e.g./docs/.server.port,server.host(trueto listen on all addresses),server.open,server.strictPort.server.proxy: forward API calls to your backend, avoiding CORS in development.changeOrigin: truerewrites theHostheader;rewriteadjusts paths;ws: trueproxies WebSockets.server.allowedHosts: hosts other than localhost (for tunnels or containers) must be listed.build.outDir,build.sourcemap, andpreviewoptions that mirrorserver.
Vite restarts the dev server automatically when the config file changes. The dev server is for development only: it is not hardened and should never be exposed to the internet.
Example
// vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { fileURLToPath, URL } from "node:url";
export default defineConfig(({ command, mode }) => ({
plugins: [react()],
resolve: {
alias: { "@": fileURLToPath(new URL("./src", import.meta.url)) },
},
server: {
port: 3000,
proxy: {
// /api/users -> http://localhost:8000/users
"/api": {
target: "http://localhost:8000",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
},
},
build: { sourcemap: mode === "staging" },
base: command === "build" ? "/app/" : "/",
}));Remember
defineConfiggives typed config; a function form sees command and mode- Aliases need matching tsconfig paths
server.proxyforwards API calls and avoids CORS in dev- Set
basewhen deploying under a sub-path
Watch out for
- Exposing the dev server publicly as if it were a production server.