phase 5 part 2: vite 8, webpack 5, fe-testing-perf topics with verified examples

This commit is contained in:
Shikui Wang committed 2026-10-23 16:29:27 +08:00
1 parent 7b5cae7d24
commit 9eeb10aede
38 files changed
+1823 -37

No files matched your search

@@ -0,0 +1,54 @@
// 不自动运行:需要先 npm install,然后 npm run dev / npm run build
// Vite 8 配置示例:环境变量、开发代理(含 SSE)、自定义插件(虚拟模块)、拆包。以官方文档为准:https://vite.dev/config/
import { defineConfig, loadEnv, type Plugin } from "vite";
import vue from "@vitejs/plugin-vue";
// 自定义插件:提供虚拟模块 virtual:build-info,在代码中拿到构建时间和版本
function buildInfo(version: string): Plugin {
const id = "virtual:build-info";
const resolved = "\0" + id; // \0 前缀:约定表示虚拟模块,其他插件不要处理
return {
name: "build-info",
resolveId(source) {
return source === id ? resolved : null;
},
load(moduleId) {
if (moduleId !== resolved) return null;
return `export default ${JSON.stringify({ version, builtAt: new Date().toISOString() })};`;
},
};
}
export default defineConfig(({ mode }) => {
// 第三个参数 "" 表示加载全部变量(包括没有 VITE_ 前缀的),只在配置文件里用,不会暴露给浏览器
const env = loadEnv(mode, process.cwd(), "");
const apiTarget = env.API_TARGET ?? "http://127.0.0.1:8000";
return {
plugins: [vue(), buildInfo(env.npm_package_version ?? "dev")],
server: {
port: 5173,
proxy: {
// 普通接口:/api/xxx → 后端 /xxx
"/api": {
target: apiTarget,
changeOrigin: true,
rewrite: (p) => p.replace(/^\/api/, ""),
},
// SSE 流式接口:开发代理默认不会缓冲,但要确保后端返回 text/event-stream 并及时 flush
"/stream": { target: apiTarget, changeOrigin: true },
},
},
build: {
sourcemap: mode !== "production",
rolldownOptions: {
output: {
// Vite 8 基于 Rolldown:拆包用 codeSplitting.groups(Rollup 的 manualChunks 已不推荐)
codeSplitting: {
groups: [{ name: "vendor-vue", test: /node_modules[\\/]@?vue/ }],
},
},
},
},
};
});