创建获取版本信息文件 version.js
const execSync = require('child_process').execSync;
module.exports.getVersion = function() {
if(process.env.NODE_ENV === 'production') {
let outputVersion = ''
try {
const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim()
const version = execSync('git rev-parse HEAD').toString().trim()
const date = new Date()
const buildDate = date.toLocaleDateString() + ' ' + date.toLocaleTimeString()
outputVersion = buildDate + '-' + branch + '-' + version
} catch (error) {
}
return outputVersion
} else {
return ''
}
}
在vue.config.js配置文件中引入并使用
const version = require('./version.js')
module.exports = {
pages: {
index: {
entry: 'src/main.js',
template: 'public/index.html',
filename: 'index.html',
_version: version.getVersion()
}
}
}
在index.html中使用上面定义的变量就好了
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Expires" content="0"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Cache-control" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="Cache" content="no-cache"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link href="favicon.ico" rel="shortcut icon">
</head>
<body>
<noscript>
<strong>
We're sorry but page doesn't work properly without JavaScript enabled. Please enable it to continue.
</strong>
</noscript>
<div id="app"></div>
<script>
console.log('version: <%= htmlWebpackPlugin.options._version %>')
</script>
</body>
</html>
配置完成后,打包部署后在页面控制台就能看到版本信息了