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

unplugin-build-stamp

v0.2.0

Published

Inject git hash, package version and build time into your app as a constant, powered by unplugin

Readme

unplugin-build-stamp

把 git hash、package 版本号和构建时间注入成应用里的一个全局常量。基于 unplugin,Vite / Rollup / Webpack / Rspack / Rsbuild / esbuild 等构建器通用。

Features

  • 🏷️ 零配置:自动向上查找 package.json,自动读取 git 信息
  • 🔌 多构建器:Vite、Rollup、Rolldown、Webpack、Rspack、Rsbuild、esbuild、Farm…
  • 🪶 零运行时:构建时直接替换为字面量,不引入任何运行时代码
  • 🧩 可扩展:extra 可以附加 CI 编号、部署环境等自定义字段
  • 🛡️ 健壮:非 git 目录、无 git 命令、浅克隆等场景不会中断构建

Installation

pnpm i -D unplugin-build-stamp

Usage

Vite

// vite.config.ts
import buildStamp from 'unplugin-build-stamp/vite'

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

Rollup

// rollup.config.js
import buildStamp from 'unplugin-build-stamp/rollup'

export default {
  plugins: [
    buildStamp()
  ]
}

Webpack

// webpack.config.js
module.exports = {
  plugins: [
    require('unplugin-build-stamp/webpack')()
  ]
}

Rsbuild / Rspack / esbuild / Farm

import buildStamp from 'unplugin-build-stamp/rspack' // 或 /rsbuild /esbuild /farm /rolldown

也可以用 <pkg>.raw(options, meta) 自行接入其他构建器。

Vite 与 Nitro 共用

前端 Vite 和使用 Vite 构建的 Nitro 服务可以使用同一个 Vite 插件:

import buildStamp from 'unplugin-build-stamp/vite'

export default defineConfig({
  plugins: [
    buildStamp(),
    nitro()
  ]
})

业务代码可以继续使用全局常量,也可以显式导入虚拟模块:

import { buildStamp } from 'virtual:build-stamp'

如果项目直接把插件放进 Nitro 的 buildPlugins,使用对应的适配入口:

import buildStamp from 'unplugin-build-stamp/nitro'

export default defineNitroConfig({
  buildPlugins: [
    buildStamp()
  ]
})

两种入口共用同一份指纹解析和 BUILD_STAMP 环境变量优先级。

What you get

注入后的全局常量默认叫 __BUILD_STAMP__:

// 业务代码里直接使用
console.info(__BUILD_STAMP__.version, __BUILD_STAMP__.shortHash)

构建产物中它会被替换成:

console.info('1.2.3', 'a1b2c3d')

TypeScript

引用本插件后,__BUILD_STAMP__ 的全局类型会自动生效,无需额外配置:

import buildStamp from 'unplugin-build-stamp/vite'

// __BUILD_STAMP__ 已被识别为 BuildStamp
const version: string = __BUILD_STAMP__.version

如果业务代码与插件配置不在同一处(例如常量只在某个 .ts 里用,而插件配在 vite.config.ts),可用三斜线引用显式引入:

/// <reference types="unplugin-build-stamp/client" />

也可以直接从这里取类型:

import type { BuildStamp, BuildStampOptions } from 'unplugin-build-stamp/client'

virtual:build-stamp 默认也提供类型声明;如果自定义了 moduleId,需要在业务侧补充对应的模块声明。

Options

buildStamp({
  // 模块 id 前缀,只在匹配的模块中注入
  // 默认:虚拟模块 + 任意绝对路径,node_modules 自动跳过
  include: ['\0', 'virtual:', '/'],

  // 计算指纹的目录,用于向上查找 package.json 与 git 仓库
  // 默认:process.cwd()
  cwd: process.cwd(),

  // 注入的全局常量名
  // 默认:'__BUILD_STAMP__'
  name: '__BUILD_STAMP__',

  // 是否在构建时打印最终指纹
  // 默认:true
  log: true,

  // 在默认指纹上附加或覆盖字段
  extra: {
    env: process.env.NODE_ENV,
    ciBuildId: process.env.CI_BUILD_ID
  }
})

Injected shape

interface BuildStamp {
  /** package.json 中的 version */
  version: string
  /** package.json 中的 name */
  name: string
  /** 完整 40 位 commit hash */
  hash: string
  /** 7 位短 hash */
  shortHash: string
  /** 当前分支,detached HEAD 时为 `HEAD` */
  branch: string
  /** 最近一次提交时间,ISO 8601 */
  commitTime: string
  /** 构建时间,ISO 8601 */
  buildTime: string
  /** 工作区是否有未提交改动 */
  dirty: boolean
}

Programmatic API

如果不想走构建器,也可以直接拿到这份数据:

import { createBuildStamp } from 'unplugin-build-stamp'

const stamp = createBuildStamp({
  cwd: process.cwd()
})
console.log(stamp.version, stamp.shortHash)

CI 环境变量

构建时可以把完整指纹作为一个 JSON 字符串传入 BUILD_STAMP。插件会优先读取其中的字段;未提供的字段继续按旧环境变量、git 和默认值回退。

BUILD_STAMP='{"hash":"0123456789abcdef0123456789abcdef01234567","branch":"main","commitTime":"2026-01-02T02:03:04.000Z","buildTime":"2026-01-02T03:04:05.000Z","dirty":false}' pnpm build

支持的 JSON 字段为 name、version、hash、buildHash、shortHash、branch、commitTime、buildTime 和 dirty。buildHash 是 hash 的兼容别名。

旧变量仍然有效:BUILD_HASH、GIT_COMMIT、BUILD_BRANCH、COMMIT_TIME、BUILD_TIME 和 BUILD_DIRTY。extra 选项的显式覆盖优先级最高。

Notes

  • 指纹在一次构建开始时计算一次,构建过程中不会变化。
  • dirty 依赖 git status --porcelain,在超大仓库里可能略慢;可用 extra 覆盖,或在自己的 CI 里先确保工作区干净。
  • 非 git 环境(Docker 镜像、npm 打包产物)下 hash / shortHash 为空字符串,其余字段照常。

License

MIT