Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
58 lines (54 loc) · 2.31 KB
/
Copy pathvite.config.ts
File metadata and controls
58 lines (54 loc) · 2.31 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
import { buildStaticHtmlHeaderRules } from './worker/lib/assetHeaders'
import { createHash } from 'node:crypto'
import { readdirSync, readFileSync, writeFileSync } from 'node:fs'
import { resolve as resolvePath } from 'node:path'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import { defineConfig, type Plugin } from 'vite'
function versionServiceWorkerCache(): Plugin {
return {
name: 'cf-navs-version-service-worker-cache',
apply: 'build',
closeBundle() {
const outputDir = resolvePath('dist')
// One policy source for Worker routes and directly served HTML assets.
const headersPath = resolvePath(outputDir, '_headers')
writeFileSync(headersPath, readFileSync(resolvePath('public', '_headers'), 'utf8').trimEnd() + '\n\n' + buildStaticHtmlHeaderRules() + '\n')
const serviceWorkerPath = resolvePath(outputDir, 'sw.js')
const source = readFileSync(serviceWorkerPath, 'utf8')
const fingerprint = createHash('sha256')
.update(readFileSync(resolvePath(outputDir, 'index.html')))
.update(source)
.update(readFileSync(headersPath))
for (const file of readdirSync(resolvePath(outputDir, 'assets')).sort()) {
fingerprint.update(file).update(readFileSync(resolvePath(outputDir, 'assets', file)))
}
const buildId = fingerprint.digest('hex').slice(0, 12)
const versioned = source.replace(
/const CACHE = 'cf-navs-v[^']+'/,
`const CACHE = 'cf-navs-v${buildId}'`,
)
if (versioned === source) throw new Error('service worker cache marker is missing')
writeFileSync(serviceWorkerPath, versioned)
},
}
}
// 前端构建到 ./dist,供 wrangler 作为静态资源托管
export default defineConfig(({ mode }) => ({
plugins: [svelte(), versionServiceWorkerCache()],
resolve: {
// Svelte 的 browser 条件提供客户端运行时,default 则落到 SSR 入口。
// 只在测试模式补上 browser,确保 onMount 的键盘、外部点击和滚动监听实际注册。
// 生产构建原本就使用 browser 条件。
conditions: mode === 'test' ? ['browser'] : undefined,
},
build: {
outDir: 'dist',
emptyOutDir: true,
},
server: {
// 本地开发时把 /api 代理到 wrangler dev(默认 8787)
proxy: {
'/api': 'http://127.0.0.1:8788',
},
},
}))