refactor(vite): 将 updatePackageJson 插件从 scripts 移动到 vite-plugins 目录

This commit is contained in:
feige996
2025-06-17 18:11:41 +08:00
parent 39f4ba793d
commit 968a090876
2 changed files with 1 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
// src/plugins/updatePackageJson.ts
import { Plugin } from 'vite'
import fs from 'fs/promises'
import path from 'path'
const updatePackageJson = (): Plugin => {
return {
name: 'update-package-json',
async buildStart() {
const packageJsonPath = path.resolve(process.cwd(), 'package.json')
try {
// 读取并解析 package.json
const content = await fs.readFile(packageJsonPath, 'utf-8')
const packageJson = JSON.parse(content)
// 更新时间戳(使用 ISO 格式或自定义格式)
packageJson['update-time'] = new Date().toISOString().split('T')[0] // YYYY-MM-DD
// 写回文件(保持 2 空格缩进)
await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n', 'utf-8')
console.log(`[update-package-json] 更新时间戳: ${packageJson['update-time']}`)
} catch (error) {
console.error('[update-package-json] 插件执行失败:', error)
}
},
}
}
export default updatePackageJson