Vite Tutorial
Learn Vite step by step: 38 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Vite? Learn what Vite is, why it starts instantly, how its dev server and production build differ, and what problems a front-end build tool solves for modern web projects.
- Create Your First Vite Project Scaffold a Vite project with npm create vite, pick a template, install dependencies, start the dev server and learn what each generated file does.
- CLI Commands and npm Scripts Learn the Vite command line - vite, vite build, vite preview, optimize - plus useful flags such as --port, --host, --mode and --config, and how npm scripts wrap them.
- Native ES Modules in the Browser Understand how browsers load ES modules with import and export, why bare specifiers fail, how import maps work, and how Vite uses native ESM to serve code without bundling.
- Vite Compared With Other Build Tools Compare Vite with webpack, Parcel, esbuild, Rollup and Turbopack on startup time, configuration, ecosystem and production output, and learn when each tool is the better choice.
Project Structure and Config
- Project Structure and index.html Learn how a Vite project is organized, why index.html is the entry point at the root, how root and base paths work, and how multi-page apps use several HTML files.
- The Vite Config File Learn how to write vite.config.js or vite.config.ts, use defineConfig for autocomplete, and set the most important options for root, base, plugins, server, build and resolve.
- Conditional Config by Command and Mode Export a function from vite.config.js to change options for serve versus build, pick settings by mode, load env values with loadEnv, and write async or multi-project configs.
- Modes and Environment Variables Use .env files and import.meta.env in Vite, understand the VITE_ prefix, built-in constants such as MODE and PROD, custom modes, and why env values are never secret in client code.
- Aliases and Path Resolution Configure resolve.alias for clean imports like @/components, keep tsconfig paths in sync, understand resolve.extensions and conditions, and fix common "failed to resolve import" errors.
- TypeScript in Vite Learn how Vite handles TypeScript by stripping types with esbuild, what isolatedModules and verbatimModuleSyntax mean, how to set up tsconfig, and how to type check with tsc.
Dev Server and HMR
- Dev Server Options Configure the Vite dev server with server.port, host, open, strictPort, headers, warmup, watch and fs.allow, and learn how to add custom middleware and read the startup output.
- Dependency Pre-Bundling Understand why Vite pre-bundles node_modules with esbuild, how CommonJS conversion and merging many files work, and how to use optimizeDeps.include, exclude and the cache folder.
- Hot Module Replacement Learn how Vite HMR updates modules without a page reload, how the module graph and update boundaries work, and how to use import.meta.hot accept, dispose, data and invalidate.
- Proxy, CORS and HTTPS in Development Configure server.proxy to forward API calls to a backend, avoid CORS errors in development, rewrite paths, proxy WebSockets, and serve the Vite dev server over HTTPS.
Assets and CSS
- Static Assets and the public Directory Learn how Vite handles images, fonts and other assets, when to import them from src or place them in public, the ?url, ?raw and ?inline suffixes, hashing and assetsInlineLimit.
- Importing CSS and CSS Modules Learn how Vite handles plain CSS imports, CSS Modules with scoped class names, global and local selectors, css.modules options, CSS code splitting, inline styles and lightningcss.
- Sass, Less and PostCSS Add Sass, Less or Stylus to a Vite project by installing the preprocessor, share variables with preprocessorOptions, configure PostCSS plugins such as autoprefixer and Tailwind, and fix common import errors.
- Asset URLs and Glob Imports Use import.meta.glob to import many files at once, choose eager or lazy loading, pull query and import options, build asset lists with new URL, and understand base and static analysis rules.
Plugins
- Anatomy of a Vite Plugin Understand how Vite plugins work: a plain object with a name and hooks such as resolveId, load and transform, plus the order in which Vite calls them.
- Writing a Transform Plugin Write a real Vite plugin step by step: filter files, transform code, keep source maps honest, add options, and test the logic as a plain function.
- Virtual Modules and Custom HMR Create virtual modules with resolveId and load, add dev-server middleware with configureServer, and send custom hot updates with handleHotUpdate and server.ws.
- Official and Community Vite Plugins A guided tour of the Vite plugins you will actually use: framework plugins, legacy browser support, PWA, compression, SVG, checker and inspect, with config examples.
Build and Optimization
- The Production Build How vite build works: Rollup bundling, hashed file names, outDir, base, minification, source maps, preview, and the build options you will touch most often.
- Code Splitting and Chunks in Vite Split your Vite bundle with dynamic import(), control chunks with manualChunks, understand vendor chunks, CSS code splitting and preload hints, and avoid common traps.
- Build Targets and Browser Support Choose which browsers your Vite app supports: the default baseline target, build.target, esbuild syntax lowering, polyfills, plugin-legacy and CSS targets.
- Vite Library Mode Build a reusable library with Vite: build.lib, ES and UMD formats, externalizing dependencies, package.json exports, type declarations and publishing checks.
- Bundle Analysis and Performance Find out what is inside your Vite bundle with a visualizer, shrink it with tree shaking, lazy imports and lighter dependencies, and speed up dev start and builds.
Frameworks and SSR
- Framework Templates and Setup Start a Vite project for React, Vue, Svelte or vanilla JS with create-vite, see what each template adds, and configure TypeScript, JSX and path aliases correctly.
- Server-Side Rendering Fundamentals in Vite Understand server-side rendering with Vite: what SSR is, why use it, how the dev server loads modules with ssrLoadModule, and how a Node server renders HTML.
- SSR Build, Hydration and Prerendering Build both bundles for a Vite SSR app, serve them in production, pass state to the client, avoid hydration mismatches, and prerender pages to static HTML.
- Vite Backend Integration and the Manifest Use Vite with a traditional backend: the dev server and proxy, build.manifest, reading the manifest to emit hashed tags, CORS, multi-page entries and server-rendered HTML.
Testing and Tooling
- Testing a Vite Project with Vitest Test Vite projects with Vitest: setup, test and expect, mocking functions and modules, jsdom components, coverage, watch mode, and a plain-JavaScript test runner stand-in.
- Tooling: TypeScript, ESLint and Prettier Set up the quality tooling around a Vite project: tsc type checks, ESLint flat config, Prettier, Git hooks, npm scripts, editor settings and a CI pipeline.
- Migrating to Vite and Troubleshooting Move a webpack or Create React App project to Vite step by step, then diagnose the errors people hit most: CommonJS, env variables, aliases, HMR, CORS and slow starts.
Projects and Practice
- Project: A Component Library Build and publish a small UI component library with Vite library mode: project layout, design tokens, components, build config, exports, tests and a release checklist.
- Project: A Static Site Generator Build a small prerendered blog with Vite: content as data, a server entry that renders pages, a build script that writes HTML files, sitemap generation and deployment.
- Vite Practice: Exercises and Interview Questions Test your Vite knowledge with hands-on exercises you can run, a debugging checklist and common interview questions about HMR, builds, plugins, SSR and performance.