在vite中获取git信息,可以使用node.js中child_process的execSync方法
- 在vite.config.js中引用
import { execSync } from 'child_process';
const _APP_INFO_ = {
// SHA
GIT_SHA:execSync('git rev-parse --short HEAD').toString().trim(),
// 提交时间
GIT_COMMIT_DATE:execSync('git log -1 --format=%cI').toString().trim(),
//HASH
GIT_HASH:execSync('git rev-parse HEAD').toString().trim(),
//最后提交 message
GIT_LAST_COMMIT_MESSAGE:execSync('git show -s --format=%s').toString().trim()
}
export default defineConfig({
//挂在全局
define: {
_APP_INFO_: JSON.stringify(_APP_INFO_),
},
})
- 可以定义个js文件
export const seenLog = () => {
const consoleText = `GIT_SHA :${_APP_INFO.GIT_SHA}\n commitDte:${_APP_INFO.GIT_COMMIT_DATE}\n GIT_HASH:${_APP_INFO.GIT_HASH}\n Message:${_APP_INFO.GIT_LAST_COMMIT_MESSAGE}`
}
console.log(`%c${consoleText}`, 'color: #fc4d50;font-size:14px');
- App.vue文件中引入
<script steup>
import { seenLog } from './seenLog.js';
seenLog()
</script>
衍生: 可以将版本号、打包时间也在控制台输出(同样加到vite.config.js)
//获取package.jso version
import config from './package.json';
const _APP_INFO = {
//版本
APP_VERSION: config.version
BUILD_TIME: dayjs().format('YYYY-MM-DD HH:mm:ss')
}
以上内容仅供参考。。。。。。。