unplugin-auto-git-log
v1.0.0
Published
Unplugin for automatically generating Git information (repo, branch, commit, etc.) in multiple output formats.
Maintainers
Readme
unplugin-auto-git-log
Unplugin for automatically generating Git information (repo, branch, commit, etc.) in multiple output formats.
Features
- 📦 Automatically extract Git repository information
- 🎯 Support multiple output formats:
- JSON file
- Window global variable (via define replacement and HTML injection)
- 🔧 Works with all major build tools (Vite, Webpack, Rollup, esbuild, Rspack, etc.)
- ⚙️ Configurable fields and output options
- 🚀 Compile-time injection for optimal performance
Installation
npm i -D unplugin-auto-git-logUsage
// vite.config.ts
import AutoGitLog from 'unplugin-auto-git-log/vite'
export default defineConfig({
plugins: [AutoGitLog()],
})// rollup.config.js
import AutoGitLog from 'unplugin-auto-git-log/rollup'
export default {
plugins: [AutoGitLog()],
}// rolldown.config.ts / tsdown.config.ts
import AutoGitLog from 'unplugin-auto-git-log/rolldown'
export default {
plugins: [AutoGitLog()],
}import { build } from 'esbuild'
import AutoGitLog from 'unplugin-auto-git-log/esbuild'
build({
plugins: [AutoGitLog()],
})// webpack.config.js
import AutoGitLog from 'unplugin-auto-git-log/webpack'
export default {
/* ... */
plugins: [AutoGitLog()],
}// rspack.config.js
import AutoGitLog from 'unplugin-auto-git-log/rspack'
export default {
/* ... */
plugins: [AutoGitLog()],
}Configuration
Default Behavior
By default, the plugin will:
- Be enabled (
enable: true) - Extract all available Git fields (repo, branch, commit, commitShort, author, authorEmail, commitTime, commitMessage, isDirty)
- Generate a JSON file at your build output directory (e.g.,
dist/git-log.jsonfor Vite) - Automatically detect the output directory from your build tool configuration
- Run after build completion
You can use the plugin without any configuration:
// vite.config.ts
import AutoGitLog from 'unplugin-auto-git-log/vite'
export default defineConfig({
plugins: [AutoGitLog()], // That's it!
})Advanced Configuration
AutoGitLog({
// Enable/disable the plugin (default: true)
enable: true,
// Git fields to include (default: all)
fields: ['repo', 'branch', 'commit', 'commitShort', 'author', 'authorEmail', 'commitTime', 'commitMessage', 'isDirty'],
// Output options
outputs: {
// Generate JSON file (default: 'git-log.json' in output directory)
json: {
fileName: 'git-log.json', // Relative to build output directory
},
// Generate window global variable (default: '__GIT_LOG__')
// Uses define replacement for compile-time injection
window: {
varName: '__GIT_LOG__', // Global variable name
console: true, // Log Git log to browser console (default: false)
},
},
// Working directory (optional, defaults to process.cwd())
// cwd: './custom-path',
})Disable the Plugin
You can conditionally disable the plugin based on environment:
import AutoGitLog from 'unplugin-auto-git-log/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
AutoGitLog({
enable: process.env.NODE_ENV === 'production', // Only enable in production
}),
],
})Or completely disable it:
AutoGitLog({
enable: false, // Plugin will not generate any files
})Git Fields
The following Git information can be extracted:
repo- Repository name (extracted from remote URL or directory name)branch- Current branch name (handles detached HEAD state)commit- Full commit hashcommitShort- Short commit hash (7 characters)author- Author nameauthorEmail- Author emailcommitTime- Commit timestamp (ISO 8601 format)commitMessage- Commit message (first line, newlines removed)tag- Current tag if HEAD points to a tagisDirty- Whether the working directory has uncommitted changesremoteUrl- Remote repository URL (e.g.,https://github.com/user/repo.git)root- Git repository root directory path
Output Examples
JSON Output
By default, the JSON file is generated at your build output directory (e.g., dist/git-log.json):
{
"repo": "https://github.com/user/repo.git",
"branch": "main",
"commit": "abc123def456...",
"commitShort": "abc123d",
"author": "John Doe",
"authorEmail": "[email protected]",
"commitTime": "2025-01-08T12:00:00.000Z",
"commitMessage": "feat: add new feature",
"isDirty": false
}Window Variable Output
When window output is enabled, the plugin will:
- For Vite: Automatically inject a
<script>tag into your HTML<head>section - For other build tools: Use define replacement to inject the variable at compile time
- Optionally log Git log to browser console (with
console: true)
You can then access the Git log anywhere in your code:
// In your browser code
console.log(window.__GIT_LOG__)
console.log(window.__GIT_LOG__.branch)
console.log(window.__GIT_LOG__.commit)How it works:
- Vite: The plugin injects a
<script>tag in the HTML that setswindow.__GIT_LOG__before your code runs - Other build tools: Uses define replacement - code references to
window.__GIT_LOG__are replaced with the actual Git log object at compile time
Example HTML injection (Vite):
<script>
if (typeof window !== 'undefined') {
window.__GIT_LOG__ = {"repo":"...","branch":"main",...};
// console.log('[Git Log]', window.__GIT_LOG__); // if console: true
}
</script>Note: For non-Vite build tools, you need to reference window.__GIT_LOG__ in your code for the define replacement to work.
