Vite Tutorial
Vite is a modern frontend build tool designed to provide a fast development experience by leveraging native ES module support in modern browsers.
Vite consists of two parts:
-
Dev server: Based on native ES modules, providing extremely fast hot updates.
-
Build command: Uses Rollup to bundle code into optimized static assets suitable for production.
Vite requires Node.js version 18+ or 20+. However, some templates need a higher Node version to run properly; when your package manager warns you, please upgrade your Node version.

Pain Points of Traditional Build Tools
Before Vite, frontend development typically used build tools like Webpack. Although powerful, these tools had some pain points:
-
Slow startup: The larger the project, the longer the startup time, hurting development efficiency.
-
Slow hot updates: After every code change, the entire project must be rebuilt, resulting in slow hot updates.
-
Complex configuration: Tools like Webpack have complex configuration and a high learning curve.
Vite solves the pain points of traditional build tools and has the following advantages:
-
Blazing-fast startup: Leverages native browser ES module support, requires no bundling, and starts extremely fast.
-
Fast hot updates: Only updates the modified module while preserving app state, improving development efficiency.
-
Rich features: Supports TypeScript, JSX, CSS, etc. out of the box.
-
Highly extensible: Easily integrates other tools and frameworks through its plugin system.
How Vite Works
Vite's working principle can be divided into development mode and production mode:
-
Development mode:
-
Vite starts a dev server and uses the browser's native ES module support to load source code directly.
-
When code changes, Vite only updates the modified module and tells the browser to hot-update, preserving app state.
Vite starts a dev server and uses the browser's native ES module support to load source code directly.
-When code changes, Vite only updates the modified module and tells the browser to hot-update, preserving app state.
-
Production mode:
-
Vite uses Rollup to bundle code into optimized static asset files.
-
These files can be deployed to any static file server.
Vite uses Rollup to bundle code into optimized static asset files.
-These files can be deployed to any static file server.
Vite Use Cases
Vite is suitable for various projects, especially:
-
Single-page applications (SPA): Such as Vue and React projects.
-
Static websites: Quickly build blogs, docs, etc.
-
Library development: Use Vite's build features to develop and bundle libraries efficiently.
Create a Vite Project
Vite provides several ways to create a new project; the simplest is using the command-line tool.
Open a terminal or command-line tool and run the following command to create a new Vite project:
npm create vite@latest
Enter the project name and choose a template as prompted.
Vite provides several templates, including:
-
vanilla: Plain JavaScript project
-
vue: Vue.js project
-
react: React project
-
preact: Preact project
-
lit: Lit project
-
svelte: Svelte project

After selecting a template, Vite automatically creates the project directory and installs dependencies; in this section we chose the Vue framework.
If you're not sure how to choose for now, just pressing Enter all the way works too; the output is similar to the following:

A project created with Vite typically contains the following files and folders:

-
node_modules: Folder that stores project dependencies.
-
public: Folder that stores static assets such as images, fonts, etc.
-
src: Folder that stores project source code.
-
main.js: Project entry file.
-
App.vue: The root component of a Vue project.
main.js: Project entry file.
-App.vue: The root component of a Vue project.
-
index.html: The project home page.
-
package.json: Project configuration file containing project info, dependencies, and script commands.
-
vite.config.js: Vite configuration file, used to configure various Vite options.
Start the Dev Server
Enter the project directory:
cd runoob-vite-test
Install dependencies:
npm install
Run the following command to start the dev server:
npm run dev
After execution, you'll see output like this:
VITE v6.2.0 ready in 684 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
➜ Vue DevTools: Open http://localhost:5173/__devtools__/ as a separate window
➜ Vue DevTools: Press Option(⌥)+Shift(⇧)+D in App to toggle the Vue DevTools
➜ press h + enter to show help
Vite starts a local dev server and prints the access address, e.g. http://localhost:5173; the port can be configured in vite.config.js.
Open a browser to that address to see your Vite project.

Modify Code and See the Result
Open the src/App.vue file, modify the code, and save.

You'll find the browser auto-refreshes and shows the modified result.

Vite Startup Flow
Defined in package.json:
"scripts": {
"dev": "vite"
}
After running npm run dev, npm looks for the local node_modules/.bin/vite executable, calls it, and starts the dev server.
The core steps of Vite's startup process:
-
Read configuration
vite.config.js or vite.config.ts.root, alias, plugins, ports, etc.-
Resolve dependencies
src/main.js(or src/main.ts) as the entry file.@/components/Hello.vue).-
Start the Dev Server
5173.File invocation order (using a typical Vue 3 project as an example)
npm run dev
↓
vite 命令执行
↓
读取 vite.config.js
↓
扫描入口文件 src/main.js / src/main.ts
↓
main.js 导入 App.vue 和 router/store 等
↓
App.vue -> template + script + style 分别由 Vite Vue 插件处理
↓
浏览器请求资源时,Vite 进行按需编译和模块缓存
↓
页面呈现,支持 HMR 热更新
Key points
- Entry file:
main.jsormain.ts, the starting point of a Vue application. - Module resolution: Vite uses
esbuildto quickly process JS/TS and handle Vue single-file components. - Alias resolution:
@Corresponds tosrc/, invite.config.jsthroughresolve.aliasconfiguration. - Hot update: Modifying
.vuefiles or JS/TS files triggers Vite to update modules locally without refreshing the entire page.

Common Vite Features
Vite provides rich features that help you develop frontend applications more efficiently.
1. Using a CSS Preprocessor
Vite supports CSS preprocessors such as Sass, Less, and Stylus.
To use these preprocessors, you need to install the corresponding dependencies first:
Sass:
npm install -D sass
Less:
npm install -D less
Stylus:
npm install -D stylus
After installation, you can use the corresponding CSS preprocessor in your project.
For example, in a Vue project you can use