webpack 打包增加版本信息
What do we need?
笔者目的是在vue项目打包后的 dist/index.html 文件中写入本次打包git用户、最后一次git提交信息,这样做的目的是便于线上项目的管理和防止同事之间的相互扯皮。
最后打包出的效果如下图:

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 文件内容开头添加 版本信息注释。
<!--
<%= htmlWebpackPlugin.options.buildInfo %>
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
至此大功告成!!!
suggest up-and-coming youngster
同事提议可将版本信息从 index.html 抽出来搞个 version.html,他是这样实现的:
1、项目根目录下新建 version\index.html 文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1,IE=11,IE=10">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" />
<title>版本声明</title>
</head> <body>
<p>commit: <%= htmlWebpackPlugin.options.buildInfo.commit %></p>
<p>commitUserName: <%= htmlWebpackPlugin.options.buildInfo.commitUserName %></p>
<p>commitDate: <%= htmlWebpackPlugin.options.buildInfo.commitDate %></p>
<p>buildUserName: <%= htmlWebpackPlugin.options.buildInfo.buildUserName %></p>
<p>buildUserMail: <%= htmlWebpackPlugin.options.buildInfo.buildUserMail %></p>
<p>buildDate: <%= htmlWebpackPlugin.options.buildInfo.buildDate %></p>
</body> </html>
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
}),
这样打包后会生成 dist\static\version.html ,成功将 版本信息和index.html 文件分离。
本文转载自:https://www.limitcode.com/detail/5e0bf02210dcbf0b1852b374.html
webpack 打包增加版本信息的更多相关文章
- 使用pyinstaller 2.1将python打包并添加版本信息和图标
最近用 wxpython写了一个小的脚本,因为想要发布给没有装python和wxpython的人使用,遂决定使用pyinstaller 2.1进行打包. 其中遇到几个问题: 1,给打包的文件添加图标 ...
- webpack打包vue文件报错,但是cnpm run dev正常,最后我只想说:是我太笨,还是webpack4.4版本太坑
最近做一个项目,需要使用webpack打包 .vue 文件的单页面应用,调试都正常,使用cnpm run dev 都可以,就是webpack打包时报错.如下: ERROR in ./src/App.v ...
- 10. vue之webpack打包详解
一.什么是webpack webpack官网给出的定义是 本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应 ...
- Webpack打包构建太慢了?试试几个方法
Webpack是个很流行的打包工具,但其打包速度却一直被吐槽着 如果不用上一些打包的优化建议,单单打包两三个文件就能花上好几秒,放上几十个入口文件依赖几百上千个包的话,几分钟十几分钟妥妥的 本文整理了 ...
- Less与TypeScript的简单理解与应用,并使用WebPack打包静态页面
既然选择了远方,便只顾风雨兼程 __ HANS许 系列:零基础搭建前后端分离项目 系列:零基础搭建前后端分离项目 创建空项目 使用Less 使用TypeScript 使用WebPack 开始写项目 总 ...
- webpack打包vue
一.原理 webpack 背后的原理其实就是把所有的非 js 资源都转换成 js (如把一个 css 文件转换成“创建一个 style 标签并把它插入 document ”的脚本.把图片转换成一个图片 ...
- webpack打包和gulp打包工具详细教程
30分钟手把手教你学webpack实战 阅读目录 一:什么是webpack? 他有什么优点? 二:如何安装和配置 三:理解webpack加载器 四:理解less-loader加载器的使用 五:理解ba ...
- webpack 打包
React自发布以来吸引了越来越多的开发者,React开发和模块管理的主流工具webpack也被大家所熟知.那么webpack有哪些优势,可以成为最主流的React开发工具呢? webpack是什么 ...
- 如何使用webpack打包前端项目
webpack概述 随着前端体积越来越大,功能越来越丰富,这时候就需要将前端工程化,而 webpack就是用于将前端各种文件打包起来. 一个简单的webpack应该包含以下几个概念 · 入口起点 · ...
随机推荐
- 翻转引起 cocos studio 引擎与cocos2d 代码相同坐标显示不同
使用setFlippedX后,又改变锚点为1.此时代码中坐标需要相对于cocos studio 中增加它本身的width,因为(0.5,0.5)是相对于自己中点的翻转,不变坐标.而(1,0.5)是相对 ...
- 《深入理解java虚拟机》读书笔记八——第九章
第九章 类加载及执行子系统的案例与实战 Q:如果有10个WEB应用程序都是用Spring来进行组织管理的话,可以把Spring放到Common或Shared目录下(Tomcat5.0)让这些程序共享. ...
- numpy学习(一)
(一)基础学习 学习渠道:阿里天池AI学习——Numpy基础(传送门) (二)练习篇 练习渠道:Numpy基础100题(Part 1) 1. Import the numpy package unde ...
- [LGR-054]洛谷10月月赛II
浏览器 结论popcnt(x^y)和popcnt(x)+popcnt(y)的奇偶性相同. 然后就是popcnt为奇数的乘为偶数的.预处理一下\(2^{16}\)次方以内的popcnt,直接\(O(1) ...
- 文本中自动出现的 ​
文本中自动出现的 ​ 所借鉴原页面地址:https://blog.csdn.net/judyc/article/details/53097142 因判断容器内字符长度来做其它处理 ...
- 登录时 按Enter 进入登录界面 或者下一行
function keyLogin() { if (event.keyCode == 13) //回车键的键值为13 $(".btn-submit").click(); //调用登 ...
- vue使用kkfileview文件预览功能
1.环境要求: java 1.8+ 2.部署运行: 本机以及虚拟机上运行: 1.从https://gitee.com/kekingcn/file-online-preview/releases地址下载 ...
- Ubuntu 安装交叉编译器出错问题
安装教程网上有很多,可参考:Ubuntu14.04(64位)下gcc-linaro-arm-linux-gnueabihf交叉编译环境搭建 但是我的问题一直是路径搭好了,就是找不到文件:反复查找,花了 ...
- Web测试控件测试点总结
1. https://www.testwo.com/blog/344 文本输入框测试用例 验证输入框是否trim处理 注:trim()函数作用是 去掉字符序列左边和右边的空格,中间的空格不管.如果字 ...
- 【Python】输入身份证号,输出出生日期
name = input("请输入你的名字:") id = input("请输入你的身份证号码:") year = id[6:10] month = id[10 ...