第一种,生成version文件和COMMITHASH
第一步:先安装 插件 git-revision-webpack-plugin
如果webpack 的版本高于4.0 则直接运行 npm install --save-dev git-revision-webpack-plugin
如果webpack 版本低于4.0 则 npm install --save-dev git-revision-webpack-plugin@2.5.1
第二步:修改webpack.prod.conf.js 文件
'use strict' const path = require('path') //....省略 const GitRevisionPlugin = require('git-revision-webpack-plugin') const webpackConfig = merge(baseWebpackConfig, { plugins: [ new GitRevisionPlugin(), //....省略 ] }) module.exports = webpackConfig
这样build 之后在 dist 文件中会有VERSION 和COMMITHASH 两个文件
第二种,直接在包或者包名上带上git 版本号 这样生成出来的 文件 类似于 :app.c35df58fa07381d84d3877ff86f623ce.gitVersion1.0.1.js
这种也需要安装依赖包npm install --save-dev git-revision-webpack-plugin
然后 修改webpack.prod.conf.js ,
const GitRevisionPlugin = require("git-revision-webpack-plugin"); const gitRevisionPlugin = new GitRevisionPlugin(); const resolve = dir => path.join(__dirname, dir); gitRevisionPlugin.versionCommand = "describe --always --tags"; const outputDirData = gitRevisionPlugin.version(); // 获取版本号 const webpackConfig = merge(baseWebpackConfig, { output: { path: config.build.assetsRoot, filename: utils.assetsPath('js/[name].[chunkhash].' + outputDirData + '.js'), chunkFilename: utils.assetsPath('js/[id].[chunkhash].' + outputDirData + '.js') }, })
————————————————
版权声明:本文为CSDN博主「weixin_42284453」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42284453/article/details/107940943
------------------------
插件官网,目前最新版本5.0,语法有所变化:
https://www.npmjs.com/package/git-revision-webpack-plugin
README:
git-revision-webpack-plugin
Simple webpack plugin that generates VERSION
and COMMITHASH
files during build based on a local git repository.
Usage
Given a webpack 5 project (check below for old webpack versions), install it as a local development dependency:
npm install --save-dev git-revision-webpack-plugin
Then, simply configure it as a plugin in the webpack config:
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
module.exports = {
plugins: [new GitRevisionPlugin()],
}
It outputs a VERSION
based on git-describe such as:
v0.0.0-34-g7c16d8b
A COMMITHASH
such as:
7c16d8b1abeced419c14eb9908baeb4229ac0542
And (optionally when branch is enabled) a BRANCH
such as:
master
Path Substitutions
It is also possible to use path substitutions on build to get the revision, version or branch as part of output paths.
[git-revision-version]
[git-revision-hash]
[git-revision-branch]
(only when branch is enabled)[git-revision-last-commit-datetime]
Example:
module.exports = {
output: {
publicPath: 'http://my-fancy-cdn.com/[git-revision-version]/',
filename: '[name]-[git-revision-hash].js',
},
}
Plugin API
The VERSION
, COMMITHASH
, LASTCOMMITDATETIME
and BRANCH
are also exposed through a public API.
Example using the DefinePlugin:
const webpack = require('webpack')
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
const gitRevisionPlugin = new GitRevisionPlugin()
module.exports = {
plugins: [
gitRevisionPlugin,
new webpack.DefinePlugin({
VERSION: JSON.stringify(gitRevisionPlugin.version()),
COMMITHASH: JSON.stringify(gitRevisionPlugin.commithash()),
BRANCH: JSON.stringify(gitRevisionPlugin.branch()),
LASTCOMMITDATETIME: JSON.stringify(gitRevisionPlugin.lastcommitdatetime()),
}),
],
}
Configuration
The plugin requires no configuration by default, but it is possible to configure it to support custom git workflows.
lightweightTags: false
If you need lightweight tags support, you may turn on lightweightTags
option in this way:
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
module.exports = {
plugins: [
new GitRevisionPlugin({
lightweightTags: true,
}),
],
}
branch: false
If you need branch name support, you may turn on branch
option in this way:
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
module.exports = {
plugins: [
new GitRevisionPlugin({
branch: true,
}),
],
}
commithashCommand: 'rev-parse HEAD'
To change the default git
command used to read the value of COMMITHASH
.
This configuration is not not meant to accept arbitrary user input and it is executed by the plugin without any sanitization.
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
module.exports = {
plugins: [
new GitRevisionPlugin({
commithashCommand: 'rev-list --max-count=1 --no-merges HEAD',
}),
],
}
versionCommand: 'describe --always'
To change the default git
command used to read the value of VERSION
.
This configuration is not not meant to accept arbitrary user input and it is executed by the plugin without any sanitization.
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
module.exports = {
plugins: [
new GitRevisionPlugin({
versionCommand: 'describe --always --tags --dirty',
}),
],
}
branchCommand: 'rev-parse --abbrev-ref HEAD'
To change the default git
command used to read the value of BRANCH
.
This configuration is not not meant to accept arbitrary user input and it is executed by the plugin without any sanitization.
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
module.exports = {
plugins: [
new GitRevisionPlugin({
branchCommand: 'rev-parse --symbolic-full-name HEAD',
}),
],
}
lastCommitDateTimeCommand: 'log -1 --format=%cI'
To change the default git
command used to read the value of LASTCOMMITDATETIME
.
This configuration is not not meant to accept arbitrary user input and it is executed by the plugin without any sanitization.
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')
module.exports = {
plugins: [
new GitRevisionPlugin({
branchCommand: 'log -1 --format=%ci',
}),
],
}
Outdated webpack
If your project is not running on Webpack 5, you will need older versions of this package.
Webpack 4
npm install git-revision-webpack-plugin@3.0.6
Webpack 3 or older
npm install git-revision-webpack-plugin@2.5.1
Check issue 29 for more information.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2014-07-13 使用MyEclipse可视化开发Hibernate实例