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-version-info

v1.0.1

Published

A webpack/vite plugin to generate build version info

Readme

unplugin-build-version-info

这是一个为 Webpack 和 Vite 构建工具提供统一解决方案的插件,用于在构建产物中生成包含版本信息的 version.json 文件。

项目概览

  • 核心功能: 在构建过程中自动收集 Git 版本信息(分支、提交哈希、提交时间、提交信息)和构建时间,并将其写入输出目录下的 version.json 文件中。
  • 技术栈:
    • Unplugin: 使用 unplugin 库来实现跨构建工具(Webpack, Vite, Rollup)的兼容性。
    • Tsup: 使用 tsup 进行项目自身的构建,生成 CommonJS (cjs) 和 ES Module (esm) 格式的产物。
    • Node.js: 依赖 Node.js 的 child_process 模块执行 Git 命令获取版本信息。

核心文件结构

  • src/index.js: 插件的主入口。
    • 使用 createUnplugin 定义插件。
    • 分别为 webpackvite/rollup 定义了特定的钩子(hooks),确保在不同的构建环境下都能正确生成 version.json
  • src/core.js: 核心逻辑文件。
    • 包含 getBuildInfo 函数,通过 git 命令获取当前代码仓库的状态(分支、Commit ID、时间等)。
    • 包含 formatDate 辅助函数用于格式化时间。
  • package.json:
    • 定义了项目的元数据和依赖。
    • 提供了 build 脚本:tsup src/index.js --format cjs,esm --dts --clean,用于打包插件并生成类型定义文件。

使用方式

安装

npm install unplugin-build-version-info --save-dev
# or
yarn add unplugin-build-version-info -D
# or
pnpm add unplugin-build-version-info -D

Vite

vite.config.tsvite.config.js 中配置:

import { defineConfig } from "vite";
import { BuildInfoPlugin } from "unplugin-build-version-info";

export default defineConfig({
  plugins: [BuildInfoPlugin.vite()],
});

Webpack

webpack.config.js 中配置:

const { BuildInfoPlugin } = require("unplugin-build-version-info");

module.exports = {
  // ...
  plugins: [BuildInfoPlugin.webpack()],
};

输出示例

插件运行后,会在构建输出目录生成类似如下内容的 version.json 文件:

{
  "branch": "main",
  "commit": "a1b2c3d4...",
  "commitTime": "2023-10-27 10:00:00",
  "commitMessage": "feat: add new feature",
  "buildTime": "2023-10-27 10:05:00"
}