GitRevisionWebpackPlugin

raw JSON →
5.0.0 verified Sat Apr 25 auth: no javascript

A webpack plugin (v5.0.0, latest stable) that generates VERSION, COMMITHASH, and optionally BRANCH files during build by executing local git commands. Supports path substitutions (e.g., [git-revision-version]) in webpack output filenames and exposes a public API for use with DefinePlugin. Differentiators: lightweight, TypeScript types included, supports lightweight tags and custom git commands, minimal configuration. Active development, with breaking changes in v5 requiring named exports.

error TypeError: GitRevisionPlugin is not a constructor
cause Using default import or wrong require pattern in v5.
fix
Use import { GitRevisionPlugin } or const { GitRevisionPlugin } = require(...).
error Unable to load 4.0 using require
cause v4 had default export issues, fixed in v4.0.1.
fix
Upgrade to v4.0.1 or v5; use destructured import.
error Module not found: Error: Can't resolve 'git-revision-webpack-plugin'
cause Plugin not installed or misconfigured in webpack config.
fix
Run npm install --save-dev git-revision-webpack-plugin and ensure config is correct.
breaking v5 changed exports: named export GitRevisionPlugin instead of default export. CJS require must use destructuring.
fix Use `const { GitRevisionPlugin } = require('git-revision-webpack-plugin')` or `import { GitRevisionPlugin } from 'git-revision-webpack-plugin'`.
deprecated v3.0.6 reverted LASTCOMMITDATETIME from patch — it was accidentally included and removed; will be available in v4.
fix Upgrade to v4+ for LASTCOMMITDATETIME support.
gotcha Configuration commands are not sanitized. Avoid passing user input to `commithashCommand` or `versionCommand` to prevent command injection.
fix Hardcode commands; never interpolate user input.
gotcha Lightweight tags are disabled by default. If your git tags are lightweight, set `lightweightTags: true` to get version output.
fix Add `new GitRevisionPlugin({ lightweightTags: true })`.
breaking v4.0.0 added Webpack 5 support and migrated to TypeScript (types included). Dropped support for Webpack 4?
fix Check peer deps: requires webpack ^5.0.0 from v5.0.0.
npm install git-revision-webpack-plugin
yarn add git-revision-webpack-plugin
pnpm add git-revision-webpack-plugin

Basic webpack config using GitRevisionPlugin to generate version files and use path substitutions for cache-busting filenames.

// webpack.config.js
import { GitRevisionPlugin } from 'git-revision-webpack-plugin';

export default {
  plugins: [
    new GitRevisionPlugin({
      branch: true,
      lightweightTags: false,
    }),
  ],
  output: {
    filename: '[name]-[git-revision-hash].js',
    publicPath: 'http://cdn.example.com/[git-revision-version]/',
  },
};