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

@frefly/print-build-info

v1.0.6

Published

前端打包信息输出配置

Readme

@frefly/print-build-info

前端打包信息输出插件。构建结束后,将本次构建的元信息(运行指令、分支、提交 id、打包时长、Node/V8 版本、环境变量等)写入项目根目录的 buildInfo.txt,便于排查线上问题对应的构建来源。

环境要求

Node.js >= 20.19.0

安装

pnpm add -D @frefly/print-build-info

无需安装 peer 依赖即可使用(vite / webpack 为可选 peer,由项目自身提供)。

特性

  • 同时支持 Vite(vite.config.js)和 Webpack / Vue CLI(vue.config.js)
  • 仅在构建命令下生效,vite dev / webpack serve 不会触发写入
  • 构建失败时不写入文件,git 信息缺失时留空不中断构建

使用

同时提供 ESM 和 CommonJS 两种格式:

  • ESM:import { printBuildInfo } from '@frefly/print-build-info'
  • CJS:const { WebpackPrintBuildInfo } = require('@frefly/print-build-info')

Vite 项目

在 vite.config.js 中注册插件,并把 loadEnv 返回的环境变量传入:

import { defineConfig, loadEnv } from 'vite'
import { printBuildInfo } from '@frefly/print-build-info'

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())

  return {
    plugins: [printBuildInfo(env)]
  }
})

执行 vite build 后,会在项目根目录生成 buildInfo.txt。

插件仅在 build 命令下生效(apply: 'build'),vite dev 不会注册其钩子。

Vue CLI 项目

在 vue.config.js 中注册 Webpack 插件:

const { WebpackPrintBuildInfo } = require('@frefly/print-build-info')

module.exports = {
  configureWebpack: {
    plugins: [new WebpackPrintBuildInfo({ env: process.env })]
  }
}

执行 vue-cli-service build 后,会在项目根目录生成 buildInfo.txt。

自定义环境变量前缀

两种插件均支持 envPrefix 选项,适配不同项目规范:

// Vite
printBuildInfo(env, { envPrefix: 'APP_' })

// Webpack
new WebpackPrintBuildInfo({ env: process.env, envPrefix: 'APP_' })

输出示例

运行指令:vite build --mode dev
当前运行环境:dev
当前打包分支:main
分支最后提交id:4a4f802fb58f59bc13111f264c401012345e852d
打包开始时间:2026/6/22 17:30:33
打包结束时间:2026/6/22 17:30:35
打包花费时长:2.12s
当前node版本:v22.22.1
当前v8内核版本:12.4.254.21-node.19
NODE_ENV:production

全局变量:
VITE_APP_ENV=dev
VITE_APP_BASE_API=/api

输出字段说明:

| 字段 | 来源 | | -------------- | ----------------------------------------------------------- | | 运行指令 | process.env.npm_lifecycle_script | | 当前运行环境 | env[envPrefix + 'APP_ENV'],回退 env[envPrefix + 'ENV'] | | 当前打包分支 | git rev-parse --abbrev-ref HEAD(失败时为空) | | 分支最后提交id | git rev-parse HEAD(失败时为空) | | 打包开始时间 | Vite buildStart / Webpack apply 时刻 | | 打包结束时间 | Vite buildEnd / Webpack done 钩子触发时刻 | | 打包花费时长 | 结束 - 开始(秒,保留 2 位小数) | | 当前node版本 | process.version | | 当前v8内核版本 | process.versions.v8 | | NODE_ENV | process.env.NODE_ENV | | 全局变量 | env 中所有以 envPrefix 开头的键值对 |

API

printBuildInfo(env?, options?) — Vite 插件

| 参数 | 类型 | 默认值 | 说明 | | ------------------- | ------------------------ | ------------- | --------------------------------------- | | env | Record<string, string> | process.env | 环境变量对象,推荐传 loadEnv() 的结果 | | options.envPrefix | string | 'VITE_' | 环境变量前缀,只有该前缀的键会被写入 |

返回 Vite 插件对象(name / enforce: 'post' / apply: 'build')。

new WebpackPrintBuildInfo(options?) — Webpack 插件

| 参数 | 类型 | 默认值 | 说明 | | ------------------- | ------------------------ | ------------- | ------------------------------------ | | options.env | Record<string, string> | process.env | 环境变量对象 | | options.envPrefix | string | 'VUE_APP_' | 环境变量前缀,Vue CLI 默认前缀即为此 |

输出位置

buildInfo.txt 写入 Vite 的 config.root / Webpack 的 compiler.context(即调用方项目根目录)。不依赖 process.cwd(),因此在 monorepo、通过 --config 指定外部配置文件等场景下也能写到正确位置。

类型提示

本包内置 index.d.ts 类型声明,使用方无需额外安装类型包即可获得完整提示(TS 项目或开启类型检查的 JS 项目均生效):

import { printBuildInfo } from '@frefly/print-build-info'
//      ^ 函数签名、env/envPrefix 配置项均有类型提示

注意事项

  • 非 git 仓库或未安装 git 时,分支与提交 id 字段会留空,不会中断构建。
  • Vite 的 buildEnd 在同一次构建中可能被多次触发,插件内部做了幂等保护,文件只会写一次。
  • 构建失败时(Vite buildEnd(err) / Webpack stats.hasErrors())不会写入文件。
  • 建议将 buildInfo.txt 加入 .gitignore,避免构建产物提交到仓库。