java 增加版本号_webpack 打包增加版本信息

本文介绍了如何在Vue项目打包后,在dist/index.html文件中添加git的最后一次提交信息,包括作者、日期等,以便于线上项目管理和问题追踪。通过使用child_process模块执行git命令获取所需信息,然后在webpack配置文件中引入并利用HtmlWebpackPlugin插入到HTML中。
摘要由CSDN通过智能技术生成

What do we need?

笔者目的是在vue项目打包后的 dist/index.html 文件中写入本次打包git用户、最后一次git提交信息,这样做的目的是便于线上项目的管理和防止同事之间的相互扯皮。

最后打包出的效果如下图:

7017f8e23693fe8e47705134fc86cbf7.png

How to do?

版本信息需要记录 git最后一次提交作者(作者名和邮箱邮)、日期、commit HEAD,本次打包git用户和邮箱、日期,这些信息都需要使用 git 命令来获取到。在 node 中,要执行一段命令行脚步需要使用 child_process 模块。

项目 build 目录下新建 version.js 文件,编写如下代码:

const child_process = require('child_process')

// git 最后一次提交的 Head

const commit = child_process.execSync('git show -s --format=%H').toString().trim()

const commitUserName = child_process.execSync('git show -s --format=%cn').toString().trim()

const commitUserMail = child_process.execSync('git show -s --format=%ce').toString().trim()

const commitDateObj = new Date(child_process.execSync(`git show -s --format=%cd`).toString())

const commitDate = `${commitDateObj.getFullYear()+'-'+(commitDateObj.getMonth()+1)+'-'+commitDateObj.getDate()+' '+commitDateObj.getHours()+':'+commitDateObj.getMinutes()}`

const buildUserName = child_process.execSync('git config user.name').toString().trim()

const buildUserMail = child_process.execSync('git config user.email').toString().trim()

const nowDate = new Date()

const buildDate = `${nowDate.getFullYear()+'-'+(nowDate.getMonth()+1)+'-'+nowDate.getDate()+' '+nowDate.getHours()+':'+nowDate.getMinutes()}`

module.exports = {commit, commitUserName, commitUserMail, commitDate, buildUserName, buildUserMail, buildDate}

在 webpack.prod.conf.js 文件中引入 version.js 模块,并修改 HtmlWebpackPlugin 插件的配置。

const BuildInfo = require('./version.js')

new HtmlWebpackPlugin({

filename: config.build.index,

template: 'index.html',

inject: true,

minify: {

removeComments: false, // index.html 保留注释

collapseWhitespace: true,

removeAttributeQuotes: true

// more options:

// https://github.com/kangax/html-minifier#options-quick-reference

},

// necessary to consistently work with multiple chunks via CommonsChunkPlugin

chunksSortMode: 'dependency',

buildInfo: JSON.stringify(BuildInfo)

}),

接着在 index.html 文件内容开头添加 版本信息注释。

html>

至此大功告成!!!

suggest up-and-coming youngster

同事提议可将版本信息从 index.html 抽出来搞个 version.html,他是这样实现的:

1、项目根目录下新建 versionindex.html 文件

html>

content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" />

版本声明

commit: 

commitUserName: 

commitDate: 

buildUserName: 

buildUserMail: 

buildDate: 

2、 webpack.prod.conf.js 文件中新增一个 HtmlWebpackPlugin 配置项

new HtmlWebpackPlugin({

filename: './static/version.html',

template: 'version/index.html',

inject: false,//不插入生成的js 仅用于版本声明

minify: {

removeComments: false,

collapseWhitespace: true,

removeAttributeQuotes: true

},

buildInfo: BuildInfo

}),

这样打包后会生成 diststaticversion.html ,成功将 版本信息和index.html 文件分离。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值