blog.dopana

Back

Vite là build tool cho web hiện đại. Dev server dùng native ES modules — khởi động và HMR nhanh hơn Webpack 10-20x.

Webpack:  Bundle → serve (càng nhiều code, càng chậm)
Vite:     Serve native ESM → transform on demand (nhanh bất kể project size)
plaintext

Tại Sao Vite?#

Vấn Đề với Webpack#

Khi project lớn dần, Webpack chậm dần:

100 files  →  5s dev start
1000 files → 30s dev start
10000+     → 2-5 phút dev start + HMR chậm
plaintext

Vì Webpack bundle mọi thứ trước khi serve.

Vite Approach#

Vite dùng native ES modules của browser. Trình duyệt tải modules trực tiếp:

<!-- Vite dev: trình duyệt tải từng module riêng -->
<script type="module" src="/src/main.tsx"></script>
<script type="module" src="/src/App.tsx"></script>
<script type="module" src="/src/components/Header.tsx"></script>
html

Không bundle — chỉ transform file khi trình duyệt request.

Setup#

npm create vite@latest my-project
# Select: React, Vue, Svelte, Vanilla, Lit...

cd my-project
npm install
npm run dev
bash
# Templates
npm create vite@latest my-app -- --template react-ts
npm create vite@latest my-api -- --template vanilla-ts
npm create vite@latest my-vue -- --template vue-ts
bash

Config#

HMR (Hot Module Replacement)#

Vite HMR nhanh vì chỉ cần thay thế module đã thay đổi — không rebuild cả bundle:

Webpack HMR:
[file changed] → rebuild bundle → gửi new bundle → replace

Vite HMR:
[file changed] → transform 1 file → gửi file → replace
plaintext

Kết quả: HMR dưới 50ms cho mọi project size.

TypeScript#

Vite hỗ trợ TypeScript native — không cần cấu hình:

// Chạy TypeScript trực tiếp
// Vite dùng esbuild để transpile — nhanh hơn tsc 20-30x
typescript
# esbuild transpile ~50ms per file
# tsc transpile ~1000ms per file
bash

Tuy nhiên Vite không type-check — dùng tsc --noEmit riêng:

{
  "scripts": {
    "dev": "vite",
    "build": "tsc --noEmit && vite build",
    "preview": "vite preview"
  }
}
json

CSS & Preprocessors#

Static Assets#

// Import asset → URL string
import logo from './assets/logo.png'
// logo = '/assets/logo.abc123.png'

// Dynamic import
const url = new URL('./images/hero.jpg', import.meta.url).href

// Public directory (copy as-is)
// public/favicon.ico → /favicon.ico
typescript

Environment Variables#

// .env
VITE_API_URL=https://api.example.com
VITE_GOOGLE_ANALYTICS_ID=G-XXXXX

// src/env.d.ts
/// <reference types="vite/client" />

// Dùng trong code
const api = fetch(`${import.meta.env.VITE_API_URL}/data`)
typescript
// .env.development — dev only
// .env.production — build only
// .env.local — local override (gitignored)
typescript

Plugins#

Vite có ecosystem plugin lớn, tương thích Rollup:

Build cho Production#

npm run build
# dist/ — build output
bash

Vite dùng Rollup cho production build — tree-shaking, code splitting, minify:

Library Mode#

Vite build cho thư viện — xuất ESM, CJS, UMD:

npm run build
# dist/my-lib.es.js
# dist/my-lib.cjs.js
# dist/my-lib.umd.js
bash

SSR#

// vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  ssr: {
    external: ['react', 'react-dom'],
    noExternal: ['some-local-lib'],
  },
})
typescript
// server.js (Node)
import { createServer } from 'vite'

const vite = await createServer({ server: { middlewareMode: true } })
const app = express()
app.use(vite.middlewares)
typescript

So Sánh#

WebpackVite
Dev start10-60s<1s
HMR1-5s<50ms
ConfigPhức tạpĐơn giản
TypeScriptLoader configNative
CSSLoader configNative
BuildWebpack bundleRollup
Dev serverExpressConnect + ESM
Plugin ecosystemLớnLớn (Rollup compatible)

Migration từ Webpack#

npm uninstall webpack webpack-cli webpack-dev-server
npm install -D vite @vitejs/plugin-react
bash
// webpack.config.js → vite.config.ts
// Thường đơn giản hơn 70-80%
typescript
WebpackVite
babel-loader@vitejs/plugin-react
ts-loaderNative (esbuild)
css-loader + style-loaderNative
sass-loadersass + css.preprocessorOptions
html-webpack-pluginNative
copy-webpack-pluginpublic/ directory

Khi Nào Dùng Vite?#

✅ Nên dùng#

  • Dự án mới — React, Vue, Svelte, Solid, Lit
  • Monorepo — Turborepo, Nx với Vite
  • Library build — ESM/CJS/UMD output
  • SSR app — hỗ trợ tốt cho server rendering
  • Web component — Lit, Stencil

❌ Không nên#

  • Legacy project — migration từ Webpack quá lớn có thể không đáng
  • Cần IE11 support — Vite target ES2015+
  • Custom build pipeline phức tạp — Webpack flexibility cao hơn

Tổng Kết#

Vite thay đổi dev experience cho web developer.

# Vite: 3 lệnh là xong
npm create vite@latest my-app -- --template react-ts
cd my-app
npm run dev
# → Dev server trong 1 giây
bash
Webpack 2018Vite 2026
npm run dev~30s chờ bundle~300ms
Sửa 1 dòng code~3s HMR~30ms HMR
Config50+ dòng10-20 dòng
Build2-5 phút10-30s

Vite là lựa chọn mặc định cho web development hiện tại — nhanh hơn, đơn giản hơn, TypeScript-native.

Tài liệu tham khảo#