2026-03-16 11:39:39 +08:00
|
|
|
|
import { defineConfig } from 'vite'
|
|
|
|
|
|
import react from '@vitejs/plugin-react'
|
|
|
|
|
|
import { readFileSync, writeFileSync } from 'fs'
|
|
|
|
|
|
import { join } from 'path'
|
|
|
|
|
|
|
|
|
|
|
|
// 从 config/VERSION 文件读取版本号
|
|
|
|
|
|
function getVersion() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const versionFile = join(__dirname, '..', 'config', 'VERSION')
|
2026-04-08 22:17:55 +08:00
|
|
|
|
const content = readFileSync(versionFile, 'utf-8').trim()
|
|
|
|
|
|
// 移除 VERSION= 前缀
|
|
|
|
|
|
if (content.startsWith('VERSION=')) {
|
|
|
|
|
|
return content.substring(7).trim()
|
|
|
|
|
|
}
|
|
|
|
|
|
return content || '0.0.0'
|
2026-03-16 11:39:39 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error('读取 VERSION 文件失败:', e.message)
|
|
|
|
|
|
return '0.0.0'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 14:01:02 +08:00
|
|
|
|
const APP_VERSION = getVersion()
|
2026-04-08 22:17:55 +08:00
|
|
|
|
console.log('📦 构建版本:v' + APP_VERSION)
|
2026-03-16 11:39:39 +08:00
|
|
|
|
|
2026-04-19 21:11:49 +08:00
|
|
|
|
// 构建后执行 - 更新 dist/index.html 的 title
|
2026-03-16 11:39:39 +08:00
|
|
|
|
function updateHtmlTitle() {
|
2026-04-19 21:11:49 +08:00
|
|
|
|
return {
|
|
|
|
|
|
name: 'update-html-title',
|
|
|
|
|
|
closeBundle() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const htmlPath = join(__dirname, 'dist', 'index.html')
|
|
|
|
|
|
let htmlContent = readFileSync(htmlPath, 'utf-8')
|
|
|
|
|
|
// 替换 <title>甲辰收藏 vXXX</title>
|
|
|
|
|
|
htmlContent = htmlContent.replace(
|
|
|
|
|
|
/<title>甲辰收藏 v[\d.]+<\/title>/,
|
|
|
|
|
|
'<title>甲辰收藏 v' + APP_VERSION + '</title>'
|
|
|
|
|
|
)
|
|
|
|
|
|
writeFileSync(htmlPath, htmlContent, 'utf-8')
|
|
|
|
|
|
console.log('✅ 已更新 dist/index.html title: 甲辰收藏 v' + APP_VERSION)
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error('更新 dist/index.html 失败:', e.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-03-16 11:39:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default defineConfig({
|
2026-04-19 21:11:49 +08:00
|
|
|
|
plugins: [react(), updateHtmlTitle()],
|
2026-03-16 11:39:39 +08:00
|
|
|
|
define: {
|
|
|
|
|
|
'import.meta.env.APP_VERSION': JSON.stringify(APP_VERSION)
|
|
|
|
|
|
},
|
|
|
|
|
|
build: {
|
|
|
|
|
|
rollupOptions: {
|
|
|
|
|
|
output: {
|
2026-04-08 22:17:55 +08:00
|
|
|
|
entryFileNames: 'assets/[name]-[hash]-[name].js',
|
|
|
|
|
|
chunkFileNames: 'assets/[name]-[hash].js',
|
|
|
|
|
|
assetFileNames: 'assets/[name]-[hash].[ext]'
|
2026-03-16 11:39:39 +08:00
|
|
|
|
}
|
2026-04-08 22:17:55 +08:00
|
|
|
|
}
|
2026-03-16 11:39:39 +08:00
|
|
|
|
}
|
2026-04-08 22:17:55 +08:00
|
|
|
|
})
|