-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvite.config.ts
More file actions
252 lines (245 loc) · 8.76 KB
/
Copy pathvite.config.ts
File metadata and controls
252 lines (245 loc) · 8.76 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import fs from "node:fs"
import { resolve } from "node:path"
import tailwindcss from "@tailwindcss/vite"
import vue from "@vitejs/plugin-vue"
import vueJsx from "@vitejs/plugin-vue-jsx"
import RekaResolver from "reka-ui/resolver"
// @ts-expect-error
import graphqlTag from "rollup-plugin-graphql-tag"
import Component from "unplugin-vue-components/vite"
import { defineConfig } from "vite"
import { chunkSplitPlugin } from "vite-plugin-chunk-split"
import { VitePWA } from "vite-plugin-pwa"
import { dataPackRewritePlugin } from "./src/data/data-pack-rewrite-plugin"
const host = process.env.TAURI_DEV_HOST
const mockDataPackDir = resolve(__dirname, "mock/data-pack")
const isAppBuild = process.env.DNA_BUILDER_APP_BUILD === "1"
const isDisableRewrite = process.env.DISABLE_REWRITE === "1"
/**
* 从构建产物中剔除 public/imgs 资源。
*/
function stripPublicImgsPlugin(): import("vite").Plugin {
let outDir = resolve(__dirname, "dist")
return {
name: "dna-builder-strip-public-imgs",
configResolved(config) {
outDir = resolve(config.root, config.build.outDir)
},
closeBundle() {
fs.rmSync(resolve(outDir, "imgs"), { recursive: true, force: true })
},
}
}
// https://vite.dev/config/
export default defineConfig(async () => ({
test: {
includeSource: ["src/**/*.{js,ts}"],
},
define: {
"import.meta.vitest": "undefined",
__APP_BUILD__: JSON.stringify(isAppBuild),
__WEB_BUILD__: JSON.stringify(!isAppBuild),
},
resolve: {
alias: {
"@": resolve(__dirname, "src"),
// "dna-api": resolve(__dirname, "externals/dna-api/src/index.ts"),
"node:async_hooks": resolve(__dirname, "src/polyfills/async_hooks.ts"),
async_hooks: resolve(__dirname, "src/polyfills/async_hooks.ts"),
},
},
plugins: [
...(isAppBuild ? [stripPublicImgsPlugin()] : []),
// 仅在构建时启用数据包重写插件,dev 启动(command 为 serve)时直接使用源码数据模块
...(!isDisableRewrite ? [dataPackRewritePlugin()] : []),
vue(),
vueJsx(),
tailwindcss(),
Component({
dts: "./src/components.d.ts",
resolvers: [RekaResolver()],
}),
chunkSplitPlugin({
strategy: "default",
customSplitting: {
// DNA/DB 两类页面存在交叉依赖,拆成独立 chunk 容易形成循环引用,统一合并到同一块避免闭环
// db: [/src\/views\/DB/],
},
}),
graphqlTag({
include: ["src/**/*.{vue,js,ts}"],
}),
VitePWA({
registerType: "autoUpdate",
includeAssets: [],
manifest: {
name: "DNA Builder",
short_name: "DNA Builder",
description: "A character builder and damage calculator for Duet Night Abyss",
theme_color: "#ffffff",
icons: [
{
src: "app-icon.png",
sizes: "192x192",
type: "image/png",
},
{
src: "app-icon.png",
sizes: "512x512",
type: "image/png",
},
{
src: "app-icon.png",
sizes: "512x512",
type: "image/png",
purpose: "any maskable",
},
],
},
workbox: {
navigateFallbackDenylist: [/^\/graphql/, /^\/api/],
maximumFileSizeToCacheInBytes: 12000000,
globPatterns: ["**/*.{js,css,html,json}"],
runtimeCaching: [
{
urlPattern: /\.json$/,
handler: "StaleWhileRevalidate",
options: {
cacheName: "res-cache",
expiration: {
maxEntries: 60,
maxAgeSeconds: 60 * 60 * 24 * 30,
},
},
},
{
urlPattern: /\.(?:png|jpg|jpeg|svg|gif)$/,
handler: "CacheFirst",
options: {
cacheName: "image-cache",
expiration: {
maxEntries: 60,
maxAgeSeconds: 60 * 60 * 24 * 30,
},
},
},
],
},
}),
],
// 这些依赖只被懒加载 chunk 首次引入:它们由 unplugin-vue-components 自动注册的组件
// (DNAGameInfo、CharHPCurve、GameSimulator、CodeEditor、ResourceTreeGraph、Sidebar 等)在运行时才带入,
// Vite 启动时的依赖扫描器无法跟踪这种插件注入的 import,会在用户首次进入对应页面时才"新发现"依赖,
// 触发 "optimized dependencies changed. reloading page" 整页刷新。
// 这里显式预声明,让它们从启动时就进入预构建缓存(node_modules/.vite),避免进页面被强制 reload。
optimizeDeps: {
include: [
"html-to-image",
"echarts",
"three",
"@antv/g",
"@antv/g6",
"@codemirror/commands",
"@codemirror/lang-javascript",
"@codemirror/language",
"@codemirror/state",
"@codemirror/view",
"@lezer/highlight",
"gsap",
"js-ini",
"@tauri-apps/plugin-updater",
"@tauri-apps/plugin-process",
"@tauri-apps/api/path",
"@langchain/core/messages",
"@langchain/openai",
"langchain",
"zod",
"@chenglou/pretext",
],
},
build: {
rollupOptions: {
input: {
index: resolve(__dirname, "index.html"),
login: resolve(__dirname, "login_jjj.html"),
},
},
},
// Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
//
// 1. prevent Vite from obscuring rust errors
clearScreen: false,
// 2. tauri expects a fixed port, fail if that port is not available
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true,
},
},
},
server: {
port: 1420,
strictPort: true,
host: host || false,
hmr: host
? {
protocol: "ws",
host,
port: 1421,
}
: undefined,
proxy: {
// 代理API请求到后端服务器
"/api/v1": {
target: "http://localhost:8887",
changeOrigin: true,
secure: false,
},
},
fs: {
allow: ["."],
},
middlewareMode: false,
configureServer(server: {
middlewares: {
use: (arg0: string, arg1: { (req: any, res: any, next: any): void; (req: any, res: any, next: any): void }) => void
}
}) {
server.middlewares.use("/mock/data-pack", (req, res, next) => {
const url = req.url || "/"
const filePath = url === "/" ? "latest.json" : url.replace(/^\//, "")
const absPath = resolve(mockDataPackDir, filePath)
if (!absPath.startsWith(mockDataPackDir)) {
res.statusCode = 403
res.end("Forbidden")
return
}
fs.readFile(absPath, (error, buffer) => {
if (error) {
next()
return
}
res.statusCode = 200
if (absPath.endsWith(".json")) {
res.setHeader("Content-Type", "application/json; charset=utf-8")
} else if (absPath.endsWith(".zip")) {
res.setHeader("Content-Type", "application/zip")
}
res.end(buffer)
})
})
},
watch: {
// 3. tell Vite to ignore watching `src-tauri`
ignored: [
"**/src-tauri/**",
"**/*.md",
"**/*.test.ts",
"**/mcp_server/**",
"**/server/**",
// "**/externals/**",
// ...
],
},
},
}))