-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathvite.config.ts
More file actions
154 lines (146 loc) · 4.63 KB
/
vite.config.ts
File metadata and controls
154 lines (146 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/// <reference types="vitest" />
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import svgr from "vite-plugin-svgr";
import path from "path";
import process from "node:process";
import { fileURLToPath } from "url";
import { readWorkspaceAppVersion } from "./scripts/app-version.mjs";
// ES 模块中获取 __dirname 的方式
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const cargoWorkspaceVersion = readWorkspaceAppVersion(__dirname);
const appVersion =
process.env.VITE_APP_VERSION?.trim() || cargoWorkspaceVersion || "unknown";
if (!process.env.VITE_APP_VERSION && cargoWorkspaceVersion) {
process.env.VITE_APP_VERSION = cargoWorkspaceVersion;
}
// 获取 Tauri mock 目录路径
const tauriMockDir = path.resolve(__dirname, "./src/lib/tauri-mock");
const sharedTauriOptimizeDepsExclude = ["@tauri-apps/plugin-deep-link"];
export default defineConfig(({ mode }) => {
const browserBridgeFlag =
process.env.LIME_BROWSER_BRIDGE ?? process.env.PROXYCAST_BROWSER_BRIDGE;
const forceOptimizeDeps =
process.env.LIME_VITE_FORCE_OPTIMIZE_DEPS?.trim() === "1";
// 检查是否在 Tauri 环境中运行(通过环境变量判断)
const isTauri =
process.env.TAURI_ENV_PLATFORM !== undefined &&
browserBridgeFlag !== "1";
// 避免 Tauri/非 Tauri 共享同一份 optimize deps 缓存导致 chunk 丢失
const cacheDir = isTauri
? "node_modules/.vite-tauri"
: "node_modules/.vite-web";
// 只在非 Tauri 环境(纯浏览器开发)下使用 mock
const tauriAliases = isTauri
? []
: [
{
find: /^@tauri-apps\/api\/core$/,
replacement: path.resolve(tauriMockDir, "core.ts"),
},
{
find: /^@tauri-apps\/api\/event$/,
replacement: path.resolve(tauriMockDir, "event.ts"),
},
{
find: /^@tauri-apps\/api\/window$/,
replacement: path.resolve(tauriMockDir, "window.ts"),
},
{
find: /^@tauri-apps\/api\/app$/,
replacement: path.resolve(tauriMockDir, "window.ts"),
},
{
find: /^@tauri-apps\/api\/path$/,
replacement: path.resolve(tauriMockDir, "window.ts"),
},
{
find: /^@tauri-apps\/plugin-dialog$/,
replacement: path.resolve(tauriMockDir, "plugin-dialog.ts"),
},
{
find: /^@tauri-apps\/plugin-shell$/,
replacement: path.resolve(tauriMockDir, "plugin-shell.ts"),
},
{
find: /^@tauri-apps\/plugin-deep-link$/,
replacement: path.resolve(tauriMockDir, "plugin-deep-link.ts"),
},
{
find: /^@tauri-apps\/plugin-global-shortcut$/,
replacement: path.resolve(tauriMockDir, "plugin-global-shortcut.ts"),
},
];
return {
cacheDir,
define: {
"import.meta.env.VITE_APP_VERSION": JSON.stringify(appVersion),
},
plugins: [
react({
jsxRuntime: mode === "development" ? "automatic" : "automatic",
jsxImportSource: "react",
babel: {
compact: true,
},
}),
svgr(),
],
resolve: {
alias: [
{
find: "@",
replacement: path.resolve(__dirname, "./src"),
},
...tauriAliases,
],
},
optimizeDeps: {
// 仅在显式要求时强制重建依赖预构建,避免 Tauri 冷启动长期卡在入口模块首个请求。
force: forceOptimizeDeps,
// deep-link 在 Tauri 模式下会命中本地 event shim,交给 Vite 常规模块解析更稳
exclude: isTauri
? sharedTauriOptimizeDepsExclude
: [
"@tauri-apps/api",
"@tauri-apps/plugin-dialog",
"@tauri-apps/plugin-shell",
"@tauri-apps/plugin-global-shortcut",
...sharedTauriOptimizeDepsExclude,
],
},
build: {
chunkSizeWarningLimit: 12000,
rollupOptions: {
onwarn(warning, defaultHandler) {
const isMixedImportWarning =
warning.message.includes("dynamically imported by") &&
warning.message.includes("also statically imported by");
if (isMixedImportWarning) {
return;
}
defaultHandler(warning);
},
},
},
clearScreen: false,
server: {
host: "127.0.0.1",
port: 1420,
strictPort: true,
watch: {
ignored: ["**/src-tauri/**"],
},
},
test: {
globals: true,
environment: "jsdom",
exclude: [
"**/node_modules/**",
"**/dist/**",
"**/scripts/playwright-login/**",
"**/src-tauri/target/**",
],
},
};
});