v1.2.98 - 修复前端构建版本号不更新问题

This commit is contained in:
甲辰生产 2026-04-19 21:11:49 +08:00
parent d578289c7c
commit fb987dc5b9
1 changed files with 19 additions and 17 deletions

View File

@ -22,28 +22,30 @@ function getVersion() {
const APP_VERSION = getVersion() const APP_VERSION = getVersion()
console.log('📦 构建版本v' + APP_VERSION) console.log('📦 构建版本v' + APP_VERSION)
// 构建时自动更新 index.html 的 title // 构建后执行 - 更新 dist/index.html 的 title
function updateHtmlTitle() { function updateHtmlTitle() {
try { return {
const htmlPath = join(__dirname, 'index.html') name: 'update-html-title',
let htmlContent = readFileSync(htmlPath, 'utf-8') closeBundle() {
// 替换 <title>甲辰收藏 vXXX</title> try {
htmlContent = htmlContent.replace( const htmlPath = join(__dirname, 'dist', 'index.html')
/<title>甲辰收藏 v[\d.]+<\/title>/, let htmlContent = readFileSync(htmlPath, 'utf-8')
'<title>甲辰收藏 v' + APP_VERSION + '</title>' // 替换 <title>甲辰收藏 vXXX</title>
) htmlContent = htmlContent.replace(
writeFileSync(htmlPath, htmlContent, 'utf-8') /<title>甲辰收藏 v[\d.]+<\/title>/,
console.log('✅ 已更新 index.html title: 甲辰收藏 v' + APP_VERSION) '<title>甲辰收藏 v' + APP_VERSION + '</title>'
} catch (e) { )
console.error('更新 index.html 失败:', e.message) writeFileSync(htmlPath, htmlContent, 'utf-8')
console.log('✅ 已更新 dist/index.html title: 甲辰收藏 v' + APP_VERSION)
} catch (e) {
console.error('更新 dist/index.html 失败:', e.message)
}
}
} }
} }
// 构建前执行
updateHtmlTitle()
export default defineConfig({ export default defineConfig({
plugins: [react()], plugins: [react(), updateHtmlTitle()],
define: { define: {
'import.meta.env.APP_VERSION': JSON.stringify(APP_VERSION) 'import.meta.env.APP_VERSION': JSON.stringify(APP_VERSION)
}, },