# git-revision-webpack-plugin

> [![npm version](https://badge.fury.io/js/git-revision-webpack-plugin.svg)](https://badge.fury.io/js/git-revision-webpack-plugin) [![downloads](https://img.shields.io/npm/dm/git-revision-webpack-plugin.svg?style=flat-square)](https://www.npmjs.com/package/

Latest version **5.0.0** (published 2021-05-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install git-revision-webpack-plugin
pnpm add git-revision-webpack-plugin
yarn add git-revision-webpack-plugin
bun add git-revision-webpack-plugin
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2021-05-05 |
| First published | 2016-02-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 53.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 359 |
| Author | Paulo Ragonha |
| Maintainers | pirelenito |

## Links

- npm: https://www.npmjs.com/package/git-revision-webpack-plugin
- Repository: https://github.com/pirelenito/git-revision-webpack-plugin
- Issues: https://github.com/pirelenito/git-revision-webpack-plugin/issues
- npm.io page: https://npm.io/package/git-revision-webpack-plugin

## Recent versions

- 5.0.0 (latest) — 2021-05-05
- 5.0.0-1 (beta) — 2021-05-05
- 5.0.0-0 — 2021-05-05
- 4.0.1 — 2021-05-04
- 4.0.0 — 2021-05-02
- 3.0.6 — 2020-04-14
- 3.0.5 — 2020-04-14
- 3.0.4 — 2019-09-24
- 3.0.3 — 2018-05-08
- 3.0.2 — 2018-05-02
- 3.0.1 — 2018-04-28
- 3.0.0 — 2018-04-22
- 2.5.1 — 2017-05-11
- 2.5.0 — 2017-05-02
- 2.4.1 — 2017-01-04
- … 11 more at https://npm.io/package/git-revision-webpack-plugin/versions

## README

# git-revision-webpack-plugin

[![npm version](https://badge.fury.io/js/git-revision-webpack-plugin.svg)](https://badge.fury.io/js/git-revision-webpack-plugin)
[![downloads](https://img.shields.io/npm/dm/git-revision-webpack-plugin.svg?style=flat-square)](https://www.npmjs.com/package/git-revision-webpack-plugin)
[![Code Climate](https://codeclimate.com/github/pirelenito/git-revision-webpack-plugin/badges/gpa.svg)](https://codeclimate.com/github/pirelenito/git-revision-webpack-plugin)

Simple [webpack](https://webpack.js.org/) plugin that generates `VERSION` and `COMMITHASH` files during build based on a local [git](http://www.git-scm.com/) repository.

## Usage

Given a **webpack 5** project ([check below](#outdated-webpack) for **old webpack versions**), install it as a local development dependency:

```bash
npm install --save-dev git-revision-webpack-plugin
```

Then, simply configure it as a plugin in the webpack config:

```javascript
const { GitRevisionPlugin } = require('git-revision-webpack-plugin')

module.exports = {
  plugins: [new GitRevisionPlugin()],
}
```

It outputs a `VERSION` based on [git-describe](http://www.git-scm.com/docs/git-describe) such as:

```
v0.0.0-34-g7c16d8b
```

A `COMMITHASH` such as:

```
7c16d8b1abeced419c14eb9908baeb4229ac0542
```

And (optionally [when branch is enabled](#branch-false)) a `BRANCH` such as:

```
master
```

## Path Substitutions

It is also possible to use [path substitutions](https://webpack.js.org/configuration/output/#output-filename) 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](#branch-false))
- `[git-revision-last-commit-datetime]`

Example:

```javascript
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](https://webpack.js.org/plugins/define-plugin/#usage):

```javascript
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](https://git-scm.com/book/en/v2/Git-Basics-Tagging#_lightweight_tags) support, you may turn on `lightweightTags` option in this way:

```javascript
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:

```javascript
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.

```javascript
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.

```javascript
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.

```javascript
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.

```javascript
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](https://github.com/pirelenito/git-revision-webpack-plugin/issues/29) for more information.

---
_Source: https://npm.io/package/git-revision-webpack-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
