前端打包寫入分支信息 打包時間等信息

在客戶端訪問界面時 控制臺打印版本信息


1686736852(1).png

在vue.config.js添加

// 獲取當(dāng)前git分支信息
function getBranchVersionInfo() {
  let vName = execSync('git name-rev --name-only HEAD').toString().trim();
  return vName
}


module.exports = {
chainWebpack: config => {
    config.resolve.alias.set('@', path.resolve(__dirname, 'src'));
    config.resolve.alias.set('@types', path.resolve(__dirname, 'src/@types'));
    

    // 主要是下面這一段
     // 使用webpack.DefinePlugin
    config.plugin('define').tap(args => {
      args[0]['process.env'].WEB_VERSION = (function () {
        const now = new Date()
        function format (num) {
          return num < 10 ? '0' + num : '' + num
        }
        const v = now.getFullYear() + format(now.getMonth() + 1) + format(now.getDate()) +'-'+ format(now.getHours()) + format(now.getMinutes())
        console.log('process.env.WEB_VERSION', v)
        return JSON.stringify(v)
      })()
      args[0]['process.env'].nowBranch = (function () {
        console.log('process.env.nowBranch', getBranchVersionInfo())
        return JSON.stringify(getBranchVersionInfo())
      })()
      return args
    })
    // end

  },

}

最后在main.js或者入口文件app.vue可以打印

const t = "color:#ccc;font-size:12px;background:#333;padding:2px 4px;border-radius:4px 0 0 4px;"
const e = "color:#fff;background:green;padding:2px 4px;border-radius:0 4px 4px 0;"
const i = "color: green;font-size: 18px;font-weight:bold;";
console.log("%c" + "=".repeat(18) + "版本信息" + "=".repeat(18), i),
console.log(`%c打包時間:%c${process.env.WEB_VERSION}`, t, e),
console.log(`%cbranch:%c${process.env.nowBranch}`, t, e),
console.log("%c" + "=".repeat(18) + "版本信息" + "=".repeat(18), i)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容