๐Ÿ“ฆ hediet / vite-git-plugin

โ˜… 0 stars โ‘‚ 0 forks ๐Ÿ‘ 0 watching โš–๏ธ MIT License
๐Ÿ“ฅ Clone https://github.com/hediet/vite-git-plugin.git
HTTPS git clone https://github.com/hediet/vite-git-plugin.git
SSH git clone git@github.com:hediet/vite-git-plugin.git
CLI gh repo clone hediet/vite-git-plugin
Henning Dieterichs Henning Dieterichs Sets up NPM_CONFIG_PROVENANCE 23fe1c9 1 months ago ๐Ÿ“ History
๐Ÿ“‚ main View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ scripts
๐Ÿ“ src
๐Ÿ“„ .gitignore
๐Ÿ“„ .nvmrc
๐Ÿ“„ LICENSE.md
๐Ÿ“„ package-lock.json
๐Ÿ“„ package.json
๐Ÿ“„ README.md
๐Ÿ“„ tsconfig.json
๐Ÿ“„ vitest.config.ts
๐Ÿ“„ README.md

vite-git-plugin

A Vite plugin that allows you to load files from git references (branches, tags, commits) instead of the working directory.

Features

  • ๐Ÿ”€ Load any file from a git reference by adding ?git=<ref> query parameter
  • ๐Ÿ”„ Automatic propagation: imported modules inherit the git reference from their importers
  • ๐Ÿท๏ธ Works with branches, tags, and commit hashes
  • ๐Ÿงช Fully tested with unit and integration tests

Installation

npm install vite-git-plugin

Usage

Basic Setup

Add the plugin to your vite.config.ts:

import { defineConfig } from 'vite';
import { viteGitPlugin } from 'vite-git-plugin';

export default defineConfig({
  plugins: [viteGitPlugin()],
});

Loading Files from Git

Load any HTML file with a git reference:

http://localhost:5173/index.html?git=main
http://localhost:5173/page.html?git=v1.0.0
http://localhost:5173/app.html?git=feature/new-feature

When you load page.html?git=main, all modules it imports will automatically use the same git reference:

<!-- page.html?git=main -->
<script type="module" src="/src/index.ts"></script>
<!-- This will load src/index.ts?git=main -->

And if index.ts imports other modules:

// src/index.ts?git=main
import { helper } from './helper.ts';
// This will load src/helper.ts?git=main

Plugin Options

viteGitPlugin({
  debug: true, // Enable debug logging
})

How It Works

  • When a file is requested with a ?git=<ref> query parameter, the plugin uses git show <ref>:<path> to read the file content from that git reference
  • The plugin's resolveId hook propagates the git reference to all imported modules
  • Files are loaded from git history instead of the working directory
  • The plugin runs with enforce: 'pre' to process files before other Vite plugins

Development

# Install dependencies
npm install

# Run tests
npm test

# Run integration tests
npm run test:integration

# Build
npm run build

Requirements

  • Git must be installed and available in PATH
  • The project must be in a git repository

License

MIT