A Vite plugin that injects the latest Git commit hash as a global `GIT_COMMIT_HASH` variable at build time. Current stable version is 1.0.7, released under MIT license. It is lightweight, has zero runtime dependencies (only peer dependency on Vite >=3), and ships TypeScript declarations. The plugin is ESM and CommonJS compatible. Key differentiator: it provides a simple, declarative way to expose git hash without complex build scripts or environment variables.
npm install vite-plugin-git-commit-hashVerified import paths — ran on the pinned version, not inferred.
Shows how to add the plugin to Vite config and use the global GIT_COMMIT_HASH variable in your code.
Avoid using GIT_COMMIT_HASH as a variable name elsewhere, or use a different config option (if available).
Check process.env.NODE_ENV or use Vite's import.meta.env.DEV to conditionally access the hash.
Upgrade Vite to version 3 or higher.
Add `declare const GIT_COMMIT_HASH: string;` to a .d.ts file (e.g., env.d.ts).
Ensure the plugin is added to the Vite config's plugins array. In development, consider that GIT_COMMIT_HASH is replaced only after build.