https://github.com/tinywaves/vite-plugin-version-injection
A Vite plugin that injects version information into your application.
https://github.com/tinywaves/vite-plugin-version-injection
vite vite-plugin
Last synced: 7 months ago
JSON representation
A Vite plugin that injects version information into your application.
- Host: GitHub
- URL: https://github.com/tinywaves/vite-plugin-version-injection
- Owner: tinywaves
- License: mit
- Created: 2025-03-18T13:18:51.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-03-18T13:25:13.000Z (11 months ago)
- Last Synced: 2025-03-18T14:27:49.856Z (11 months ago)
- Topics: vite, vite-plugin
- Language: TypeScript
- Homepage:
- Size: 64.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vite-plugin-version-injection
A Vite plugin that injects version information into your vite application. This plugin automatically injects your app version into HTML, making it available at runtime.
[中文简体](./README.zh-CN.md)
## Features
- Automatically reads version from `package.json`
- Supports custom version resolution
- Configurable injection position (head or body)
- Customizable global variable name
- Supports Vite 6.0 and above
## Installation
```bash
# npm
npm install vite-plugin-version-injection -D
# pnpm
pnpm add vite-plugin-version-injection -D
# yarn
yarn add vite-plugin-version-injection -D
```
## Usage
Add the plugin to your Vite config:
```ts
// vite.config.ts
import { defineConfig } from 'vite';
import versionInjection from 'vite-plugin-version-injection';
export default defineConfig({
plugins: [
versionInjection()
]
});
```
This will inject a script tag with your version (default: current `package.json`'s version) into the HTML:
```html
window.__APP_VERSION__='1.0.0';
```
You can then access the version in your application:
```ts
console.log(window.__APP_VERSION__); // outputs: 1.0.0
```
## Configuration
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `injectTo` | `'head' \| 'body'` | `'head'` | Specify where to inject the script |
| `versionVarName` | `string` | `'__APP_VERSION__'` | Specify the global variable name |
| `versionResolve` | `() => string` | Reads from `package.json` | Custom version resolution function |
### Configuration Example
```ts
import { defineConfig } from 'vite';
import versionInjection from 'vite-plugin-version-injection';
export default defineConfig({
plugins: [
versionInjection({
injectTo: 'body',
versionVarName: '__MY_APP_VERSION__',
versionResolve: () => '1.2.3' // custom version
})
]
});
```
## License
[MIT](./LICENSE)