What is git-revision-webpack-plugin?
The git-revision-webpack-plugin is a Webpack plugin that allows you to embed Git revision information into your build. This can be useful for versioning, debugging, and tracking the state of your codebase at the time of the build.
What are git-revision-webpack-plugin's main functionalities?
Embed Git Revision Information
This feature allows you to embed the current Git revision information into your Webpack build. The code sample shows how to include the GitRevisionPlugin in your Webpack configuration.
const GitRevisionPlugin = require('git-revision-webpack-plugin');
const gitRevisionPlugin = new GitRevisionPlugin();
module.exports = {
plugins: [
gitRevisionPlugin
]
};
Access Git Information in Code
This feature allows you to access Git information such as the version, commit hash, and branch name directly in your code. The code sample demonstrates how to log this information to the console.
const GitRevisionPlugin = require('git-revision-webpack-plugin');
const gitRevisionPlugin = new GitRevisionPlugin();
console.log('Version:', gitRevisionPlugin.version());
console.log('Commit Hash:', gitRevisionPlugin.commithash());
console.log('Branch:', gitRevisionPlugin.branch());
Define Global Constants
This feature allows you to define global constants in your Webpack configuration that hold Git information. The code sample shows how to use the DefinePlugin to create global constants for the version, commit hash, and branch name.
const GitRevisionPlugin = require('git-revision-webpack-plugin');
const gitRevisionPlugin = new GitRevisionPlugin();
module.exports = {
plugins: [
new webpack.DefinePlugin({
'VERSION': JSON.stringify(gitRevisionPlugin.version()),
'COMMITHASH': JSON.stringify(gitRevisionPlugin.commithash()),
'BRANCH': JSON.stringify(gitRevisionPlugin.branch())
})
]
};
Other packages similar to git-revision-webpack-plugin
webpack-build-info-plugin
The webpack-build-info-plugin is a Webpack plugin that provides build information including Git commit hash, branch name, and build timestamp. It is similar to git-revision-webpack-plugin but also includes additional build metadata.
webpack-git-revision
The webpack-git-revision plugin is another alternative that focuses on embedding Git revision information into your Webpack build. It provides similar features to git-revision-webpack-plugin but with a different API.
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 project, 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:
var 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
And a COMMITHASH
such as:
7c16d8b1abeced419c14eb9908baeb4229ac0542
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 lighweithTags
option in this way:
var GitRevisionPlugin = require('git-revision-webpack-plugin')
module.exports = {
plugins: [
new GitRevisionPlugin({
lightweightTags: true
})
]
}
commithashCommand: 'rev-parse HEAD'
To change the default git
command used to read the value of COMMITHASH
:
var 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
:
var GitRevisionPlugin = require('git-revision-webpack-plugin')
module.exports = {
plugins: [
new GitRevisionPlugin({
versionCommand: 'describe --always --tags --dirty'
})
]
}
Path Substitutions
It is also possible to use two path substituitions on build to get either the revision or version as part of output paths.
[git-revision-version]
[git-revision-hash]
Example:
module.exports = {
output: {
publicPath: 'http://my-fancy-cdn.com/[git-revision-version]/',
filename: '[name]-[git-revision-hash].js'
}
}
Plugin API
The VERSION
and COMMITHASH
are also exposed through a public API.
Example using the DefinePlugin:
var gitRevisionPlugin = new GitRevisionPlugin()
module.exports = {
plugins: [
new DefinePlugin({
'VERSION': JSON.stringify(gitRevisionPlugin.version()),
'COMMITHASH': JSON.stringify(gitRevisionPlugin.commithash()),
})
]
}