Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

unplugin-build-version-info

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unplugin-build-version-info

A webpack/vite plugin to generate build version info

latest
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

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"
}

Keywords

webpack

FAQs

Package last updated on 05 Dec 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts