TL;DR
Was: Ein Frontend-Build-Tool der nächsten Generation für schnelle Entwicklung.
Warum: Sofortiger Serverstart, blitzschnelles HMR, optimierte Builds, keine Konfiguration nötig.
Quick Start
Neues Projekt erstellen:
npm create vite@latest my-app
cd my-app
npm install
npm run dev
Oder mit bestimmtem Framework:
npm create vite@latest my-app -- --template react
npm create vite@latest my-app -- --template vue
npm create vite@latest my-app -- --template svelte
Öffnen Sie http://localhost:5173
Cheatsheet
| Befehl | Beschreibung |
|---|---|
npm run dev | Dev-Server starten |
npm run build | Für Produktion bauen |
npm run preview | Produktions-Build vorschauen |
vite --host | Im Netzwerk freigeben |
vite --port 3000 | Benutzerdefinierter Port |
Verfügbare Templates:
vanilla,vanilla-tsreact,react-ts,react-swc,react-swc-tsvue,vue-tssvelte,svelte-tspreact,preact-tssolid,solid-ts
Gotchas
Environment variables
# .env
VITE_API_URL=https://api.example.com
// Access in code (must start with VITE_)
console.log(import.meta.env.VITE_API_URL);
console.log(import.meta.env.MODE); // 'development' or 'production'
Static assets
// Import as URL
import imgUrl from './img.png';
// Import as raw string
import content from './file.txt?raw';
// Public folder (served at root)
// public/icon.png → /icon.png
Config file
// vite.config.js
import { defineConfig } from 'vite';
export default defineConfig({
server: {
port: 3000,
proxy: {
'/api': 'http://localhost:8080',
},
},
build: {
outDir: 'build',
},
});
CSS
// Automatically handled
import './style.css';
import styles from './style.module.css'; // CSS Modules
import './style.scss'; // Sass (npm install -D sass)
Next Steps
- Vite Documentation - Offizielle Anleitung
- Vite Plugins - Plugin-Ökosystem
- Awesome Vite - Ressourcen
- Vitest - Vite-natives Testen