phase 5 part 2: vite 8, webpack 5, fe-testing-perf topics with verified examples
This commit is contained in:
1 parent
7b5cae7d24
commit
9eeb10aede
38 files changed
+1823
-37
No files matched your search
@@ -0,0 +1,5 @@
|
||||
# 复制为 .env 后使用(.env 已在 .gitignore 中,避免误提交)
|
||||
# 以 VITE_ 开头的变量会被打包进浏览器代码,绝对不要放密钥
|
||||
VITE_APP_TITLE=Vite 配置示例
|
||||
# 不带 VITE_ 前缀的变量只在 vite.config.ts 中可用
|
||||
API_TARGET=http://127.0.0.1:8000
|
||||
@@ -0,0 +1,11 @@
|
||||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>Vite 配置示例</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/src/main.ts"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "vite-config-demo",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"vue": "^3.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitejs/plugin-vue": "^6.0.0",
|
||||
"typescript": "^5.9.0",
|
||||
"vite": "^8.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/// <reference types="vite/client" />
|
||||
interface ImportMetaEnv {
|
||||
readonly VITE_APP_TITLE: string;
|
||||
}
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv;
|
||||
}
|
||||
declare module "virtual:build-info" {
|
||||
const info: { version: string; builtAt: string };
|
||||
export default info;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// 不自动运行:被 main.ts 动态 import,构建后会成为单独的 chunk
|
||||
export function summary(): string {
|
||||
return "这个模块在点击按钮时才加载";
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// 不自动运行:浏览器入口,由 vite 构建
|
||||
import { createApp, h } from "vue";
|
||||
import info from "virtual:build-info";
|
||||
|
||||
const App = {
|
||||
setup() {
|
||||
return () =>
|
||||
h("main", [
|
||||
h("h1", import.meta.env.VITE_APP_TITLE),
|
||||
h("p", `版本 ${info.version},构建于 ${info.builtAt},模式 ${import.meta.env.MODE}`),
|
||||
h("button", { onClick: () => import("./heavy").then((m) => alert(m.summary())) }, "按需加载模块"),
|
||||
]);
|
||||
},
|
||||
};
|
||||
createApp(App).mount("#app");
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"strict": true,
|
||||
"noEmit": true,
|
||||
"skipLibCheck": true,
|
||||
"types": ["vite/client", "node"]
|
||||
},
|
||||
"include": ["src/**/*.ts", "src/**/*.d.ts", "vite.config.ts"]
|
||||
}
|
||||
@@ -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/ }],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
Reference in new issue
Block a user