npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-unified-version

v1.0.0

Published

A Vite plugin to inject Git commit ID and build time into HTML meta tags and window object

Readme

vite-plugin-unified-version

一个 Vite 插件,用于自动注入 Git commit ID 和构建时间到 HTML 的 meta 标签和 window 对象中。

特性

  • 🚀 自动获取当前 Git 仓库的 commit hash(简短版)
  • ⏰ 自动记录构建时间(本地时间格式)
  • 🔧 可自定义 meta 标签和 window 对象的键名
  • 🎛️ 可控制是否注入 meta 标签
  • 📦 兼容 Vite 3.x, 4.x, 5.x, 7.x+

安装

npm install vite-plugin-unified-version --save-dev
# 或
yarn add vite-plugin-unified-version -D
# 或
pnpm add vite-plugin-unified-version -D

使用方法

基础用法

vite.config.jsvite.config.ts 中引入并配置插件:

import { defineConfig } from 'vite';
import unifiedVersion from 'vite-plugin-unified-version';

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

高级配置

插件支持以下配置选项:

| 参数 | 类型 | 默认值 | 说明 | |------|------|--------|------| | app_version | string | 'app_version' | 自定义版本号的键名 | | app_build_time | string | 'app_build_time' | 自定义构建时间的键名 | | inject_meta | boolean | true | 是否注入 meta 标签到 HTML |

自定义键名

import unifiedVersion from 'vite-plugin-unified-version';

export default defineConfig({
  plugins: [
    unifiedVersion({
      app_version: 'myAppVersion',
      app_build_time: 'myBuildTime'
    })
  ]
});

禁用 meta 标签注入

import unifiedVersion from 'vite-plugin-unified-version';

export default defineConfig({
  plugins: [
    unifiedVersion({
      inject_meta: false
    })
  ]
});

注入效果

插件会在构建时自动注入以下内容:

Meta 标签(默认启用)

<head>
  <!-- Injected by vite-plugin-unified-version -->
  <meta name="app_version" content="a1b2c3d" />
  <meta name="app_build_time" content="2026/2/10 14:30:00" />
  <script>
    // 注入到 window 对象,全局可访问
    window.app_version = "a1b2c3d";
    window.app_build_time = "2026/2/10 14:30:00";
  </script>
</head>

Window 对象

注入后可以在 JavaScript 中直接访问:

console.log(window.app_version);      // 输出: "a1b2c3d"
console.log(window.app_build_time);   // 输出: "2026/2/10 14:30:00"

编译时变量

插件还会注入编译时变量,可以在代码中使用:

console.log(__app_version__);      // 输出: "a1b2c3d"
console.log(__app_build_time__);   // 输出: "2026/2/10 14:30:00"

构建输出

构建完成后,控制台会显示注入的信息:

✅ [vite-plugin-unified-version] 版本 a1b2c3d 已注入 (构建于: 2026/2/10 14:30:00)
   使用键名: app_version, app_build_time
   Meta标签: 已添加

常见问题

Q: 如果不在 Git 仓库中运行会怎样?

A: 插件会自动检测 Git 环境,如果不在 Git 仓库中,commit ID 会显示为 unknown,并在控制台输出提示信息。

Q: 如何在 TypeScript 中使用注入的变量?

A: 需要在 src/vite-env.d.ts 或全局类型声明文件中添加类型声明:

declare global {
  interface Window {
    app_version: string;
    app_build_time: string;
  }
}

export {};

许可证

MIT

作者

jywud

仓库

https://github.com/jywud/vite-plugin-unified-version