Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vite Tutorial

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:

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:

Vite solves the pain points of traditional build tools and has the following advantages:

How Vite Works

Vite's working principle can be divided into development mode and production 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 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:


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:

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:

-

main.js: Project entry file.

-

App.vue: The root component of a Vue project.

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:

  1. Read configuration

  2. By default it reads the vite.config.js or vite.config.ts.
  3. Configuration includes root, alias, plugins, ports, etc.
  4. - By default reads `vite.config.js` or `vite.config.ts` from the root directory. - Configuration includes `root`, `alias`, plugins, ports, etc.
    1. Resolve dependencies

    2. Scan src/main.js(or src/main.ts) as the entry file.
    3. Perform dependency analysis on ES modules.
    4. Resolve aliases for import paths (such as @/components/Hello.vue).
    5. - Scan `src/main.js` (or `src/main.ts`) as the entry file. - Perform dependency analysis on ES modules. - Resolve aliases for import paths (such as `@/components/Hello.vue`).
      1. Start the Dev Server

      2. Create an HTTP server with the default port 5173.
      3. Provides hot module replacement (HMR) capability.
      4. Convert Vue files, JS/TS, CSS, etc. into browser-recognizable code through Vite's built-in plugins.
      5. - Create an HTTP server with the default port `5173`. - Provide hot module replacement (HMR) capability. - Convert Vue files, JS/TS, CSS, etc. into browser-recognizable code through Vite's built-in plugins.

        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


        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