Skip to content

generateVersion

Auto-generate version numbers during Vite build with file output and global variable injection.

Quick Start

typescript
import { defineConfig } from 'vite'
import { generateVersion } from '@meng-xi/vite-plugin'

export default defineConfig({
	plugins: [generateVersion()]
})

Options

OptionTypeDefaultDescription
formatVersionFormat'timestamp'Version format
customFormatstring-Custom format template
semverBasestring'1.0.0'Semantic version base
autoIncrementbooleanfalseAuto-increment patch
outputTypeOutputType'file'Output type
outputFilestring'version.json'Output file path
defineNamestring'__APP_VERSION__'Global variable name
hashLengthnumber8Hash length (1-32)
prefixstring''Version prefix
suffixstring''Version suffix
extraRecord<string, unknown>-Extra info (JSON only)
enabledbooleantrueEnable the plugin
verbosebooleantrueShow detailed logs
errorStrategy'throw' | 'log' | 'ignore''throw'Error handling strategy

Version Formats

FormatDescriptionExample
timestampTimestamp20260203153000
dateDate2026.02.03
datetimeDate-time2026.02.03.153000
semverSemantic version1.0.0
hashRandom hasha1b2c3d4
customCustom template-

Output Types

TypeDescription
fileOutput to JSON file
defineInject global variable
bothBoth file and variable

Custom Format Placeholders

PlaceholderDescriptionExample
{YYYY}Four-digit year2026
{YY}Two-digit year26
{MM}Two-digit month02
{DD}Two-digit day03
{HH}Two-digit hour15
{mm}Two-digit min30
{ss}Two-digit sec00
{SSS}Milliseconds123
{timestamp}Timestamp1738567800
{hash}Random hasha1b2c3d4
{major}Major version1
{minor}Minor version0
{patch}Patch version0

Examples

Date Format with Prefix

typescript
generateVersion({
	format: 'date',
	prefix: 'v'
})
// Output: v2026.02.03

Custom Format

typescript
generateVersion({
	format: 'custom',
	customFormat: '{YYYY}.{MM}.{DD}-{hash}',
	hashLength: 6
})
// Output: 2026.02.03-a1b2c3

Inject Global Variable

typescript
generateVersion({
	outputType: 'define',
	defineName: '__VERSION__'
})

// Use in code
console.log(__VERSION__) // '20260203153000'

Output File and Inject Code

typescript
generateVersion({
	outputType: 'both',
	outputFile: 'build-info.json',
	defineName: '__BUILD_VERSION__',
	extra: {
		environment: 'production',
		author: 'MengXi Studio'
	}
})

Output File Format

json
{
	"version": "v2026.02.03-a1b2c3",
	"buildTime": "2026-02-03T15:30:00.000Z",
	"timestamp": 1738567800000,
	"format": "custom",
	"environment": "production",
	"author": "MengXi Studio"
}

Notes

  • customFormat is required when format is 'custom'
  • hashLength must be between 1-32
  • outputType as 'define' or 'both' injects both defineName and defineName_INFO
  • extra only appears in JSON file, not in version string

Released under the MIT License.