git-revision-vite-plugin
raw JSON → 0.0.11 verified Mon Apr 27 auth: no javascript
A Vite plugin that injects git repository information (commit hash, version, branch, last commit datetime) as global constants at build time. Current stable version is 0.0.11, with experimental release cadence. Inspired by git-revision-webpack-plugin, it aims to provide equivalent functionality for Vite projects. Lightweight and zero-config by default, it exposes four globals: GIT_COMMITHASH, GIT_VERSION, GIT_BRANCH, GIT_LASTCOMMITDATETIME. Supports custom variable names, git commands, and lightweight tags. TypeScript types are included.
Common errors
error Error: The option 'lightweightTags' cannot be used together with 'versionCommand'. ↓
cause Both options are mutually exclusive as lightweightTags implies custom git command for tags that conflicts with a custom versionCommand.
fix
Remove versionCommand when lightweightTags: true, or set lightweightTags: false.
error ReferenceError: GIT_COMMITHASH is not defined ↓
cause The global is not available in Node.js runtime or after build in non-Vite processed files (e.g., in a plain .js file served statically).
fix
Only use these globals inside files processed by Vite (like .vue, .tsx, .js imported in src). They are compile-time constants.
error TypeError: GitRevisionVitePlugin is not a constructor or function ↓
cause Named import used instead of default import.
fix
Use default import: import GitRevisionVitePlugin from 'git-revision-vite-plugin'.
Warnings
gotcha Globals are only available at build time, not in runtime after build. They are replaced with string literals via Vite's define. ↓
fix Ensure you use these variables only in code that is processed by Vite (e.g., inside Vue SFCs or imported modules) and not in external scripts.
gotcha The plugin's options for version, branch, commitHash default to true, but lightweightTags defaults to false. Setting lightweightTags: true is mutually exclusive with versionCommand and will throw an error if both are set. ↓
fix If using lightweightTags, do not set versionCommand explicitly. Set version: false if not needed.
deprecated Option 'versionVar' and 'branchVar' defaults are outdated; variable names are hardcoded and cannot be fully customized in earlier versions. ↓
fix Upgrade to 0.0.2 or later to use custom variable names.
breaking In v0.0.2, default global variable names changed from all uppercase to GIT_COMMITHASH etc. Previously they might have been different strings. ↓
fix Update any references to the old global names to the new ones: GIT_COMMITHASH, GIT_VERSION, GIT_BRANCH, GIT_LASTCOMMITDATETIME.
Install
npm install git-revision-vite-plugin yarn add git-revision-vite-plugin pnpm add git-revision-vite-plugin Imports
- GitRevisionVitePlugin wrong
import { GitRevisionVitePlugin } from 'git-revision-vite-plugin'correctimport GitRevisionVitePlugin from 'git-revision-vite-plugin' - GIT_COMMITHASH wrong
console.log(process.env.GIT_COMMITHASH)correctconsole.log(GIT_COMMITHASH) - GIT_VERSION
console.log(GIT_VERSION)
Quickstart
// vite.config.js
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import GitRevisionVitePlugin from 'git-revision-vite-plugin';
export default defineConfig({
plugins: [
vue(),
GitRevisionVitePlugin()
]
});
// src/App.vue
<script setup lang="ts">
console.log(GIT_COMMITHASH);
console.log(GIT_VERSION);
console.log(GIT_BRANCH);
console.log(GIT_LASTCOMMITDATETIME);
</script>
<template>
<div>{{ GIT_COMMITHASH }}</div>
</template>