Vite 外掛開發實戰在前端開發中的應用越來越廣泛。本文從實際專案出發,深入分析其核心原理和最佳實踐。
基礎用法
在這個基礎上,我們可以進一步最佳化:
javascript
const express = require('express')
const app = express()
app.use(express.json())
class AppError extends Error {
constructor(status, message) {
super(message); this.statusCode = status
}
}
const asyncHandler = (fn) => (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch(next)
app.get('/api/users/:id', asyncHandler(async (req, res) => {
const user = await User.findById(req.params.id)
if (!user) throw new AppError(404, '使用者不存在')
res.json({ data: user })
}))
這種模式在大型專案中非常實用,能顯著降低維護成本。
進階用法
實際專案中的用法會更復雜一些:
javascript
import { useReducer, useCallback } from 'react'
const initialState = { items: [], filter: '', sort: 'date' }
function reducer(state, action) {
switch (action.type) {
case 'SET_ITEMS': return { ...state, items: action.payload }
case 'SET_FILTER': return { ...state, filter: action.payload }
case 'ADD_ITEM': return { ...state, items: [...state.items, action.payload] }
case 'REMOVE_ITEM': return { ...state, items: state.items.filter(i => i.id !== action.payload) }
default: throw new Error(`Unknown: ${action.type}`)
}
}
通過這種方式,程式碼的可測試性和可擴充套件性都得到了提升。
實戰案例
以下是一個完整的示例:
javascript
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T
async function fetchUser(id: string) {
const res = await fetch(`/api/users/${id}`)
return res.json() as Promise<{ id: string; name: string; email: string }>
}
type User = UnwrapPromise<ReturnType<typeof fetchUser>>
// 型別安全的事件系統
interface EventMap {
login: { userId: string; timestamp: number }
logout: { userId: string }
}
class TypedEmitter<T extends Record<string, any>> {
private handlers = new Map<keyof T, Set<Function>>()
on<K extends keyof T>(event: K, handler: (payload: T[K]) => void) {
if (!this.handlers.has(event)) this.handlers.set(event, new Set())
this.handlers.get(event)!.add(handler)
}
emit<K extends keyof T>(event: K, payload: T[K]) {
this.handlers.get(event)?.forEach(h => h(payload))
}
}
注意邊界條件處理,這在生產環境中至關重要。
效能最佳化
關鍵在於理解核心邏輯:
javascript
const express = require('express')
const app = express()
app.use(express.json())
class AppError extends Error {
constructor(status, message) {
super(message); this.statusCode = status
}
}
const asyncHandler = (fn) => (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch(next)
app.get('/api/users/:id', asyncHandler(async (req, res) => {
const user = await User.findById(req.params.id)
if (!user) throw new AppError(404, '使用者不存在')
res.json({ data: user })
}))
效能最佳化需要結合具體場景,不是所有情況都需要過度最佳化。
常見陷阱
我們可以通過以下方式來改進:
javascript
import { useReducer, useCallback } from 'react'
const initialState = { items: [], filter: '', sort: 'date' }
function reducer(state, action) {
switch (action.type) {
case 'SET_ITEMS': return { ...state, items: action.payload }
case 'SET_FILTER': return { ...state, filter: action.payload }
case 'ADD_ITEM': return { ...state, items: [...state.items, action.payload] }
case 'REMOVE_ITEM': return { ...state, items: state.items.filter(i => i.id !== action.payload) }
default: throw new Error(`Unknown: ${action.type}`)
}
}
這套方案已經在線上穩定運行了半年以上,經過了實際驗證。
小結
- 不要為了用新技術而用新技術
- 程式碼示例僅供參考,需根據業務場景調整
- Vite 外掛開發實戰不是銀彈,需要根據專案規模和技術棧選擇
- 理解底層原理比記住 API 更重要