vite-plugin-app-build-info 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # vite-plugin-app-build-info
2
+
3
+ Vite 插件:构建时生成应用版本信息,并同时注入到 JS、HTML 和 `version.json`。
4
+
5
+ ## 安装
6
+
7
+ ```sh
8
+ pnpm add -D vite-plugin-app-build-info
9
+ ```
10
+
11
+ 也可以使用 npm:
12
+
13
+ ```sh
14
+ npm install -D vite-plugin-app-build-info
15
+ ```
16
+
17
+ ## 使用
18
+
19
+ ```ts
20
+ import { defineConfig } from 'vite'
21
+ import buildInfo from 'vite-plugin-app-build-info'
22
+
23
+ export default defineConfig({
24
+ plugins: [buildInfo()],
25
+ })
26
+ ```
27
+
28
+ 默认会生成三类输出:
29
+
30
+ - 前端代码可读取 `__APP_INFO__`
31
+ - 构建产物包含 `dist/version.json`
32
+ - `index.html` 的 `head` 内包含 `app-build-info` meta 标签
33
+
34
+ ## 本地开发
35
+
36
+ 在仓库根目录安装依赖:
37
+
38
+ ```sh
39
+ pnpm install
40
+ ```
41
+
42
+ 构建插件:
43
+
44
+ ```sh
45
+ pnpm --filter vite-plugin-app-build-info build
46
+ ```
47
+
48
+ 发布前检查 npm 包内容:
49
+
50
+ ```sh
51
+ pnpm --filter vite-plugin-app-build-info pack:dry-run
52
+ ```
53
+
54
+ ## 发布到 npmjs
55
+
56
+ 首次发布前先登录 npmjs:
57
+
58
+ ```sh
59
+ npm login --registry https://registry.npmjs.org/
60
+ ```
61
+
62
+ 确认版本号后,在仓库根目录执行:
63
+
64
+ ```sh
65
+ pnpm publish:plugin
66
+ ```
67
+
68
+ 发布脚本会先自动重新构建 `dist` 后再发布公开包。
69
+
70
+ 如果你本机默认 npm registry 是公司内网源,发布时不用手动切换;包内 `publishConfig` 和根目录发布脚本已经显式指定 `https://registry.npmjs.org/`。
71
+
72
+ ## 类型声明
73
+
74
+ 如果要在前端代码里读取默认的 `__APP_INFO__`,可以在项目的 `env.d.ts` 中声明:
75
+
76
+ ```ts
77
+ declare const __APP_INFO__: {
78
+ name: string
79
+ version: string
80
+ commit: string
81
+ branch: string
82
+ commitTime: string
83
+ buildTime: string
84
+ builder?: string
85
+ }
86
+ ```
87
+
88
+ ## 配置
89
+
90
+ ```ts
91
+ buildInfo({
92
+ globalName: '__APP_INFO__',
93
+ jsonFileName: 'version.json',
94
+ metaName: 'app-build-info',
95
+ includeBuilder: true,
96
+ })
97
+ ```
98
+
99
+ - `globalName`:注入到 JS bundle 的全局常量名
100
+ - `jsonFileName`:输出的 JSON 文件名
101
+ - `metaName`:HTML meta 标签的 `name`
102
+ - `includeBuilder`:是否记录本机打包用户和主机名
@@ -0,0 +1,18 @@
1
+ import type { Plugin } from 'vite';
2
+ export type AppBuildInfo = {
3
+ name: string;
4
+ version: string;
5
+ commit: string;
6
+ branch: string;
7
+ commitTime: string;
8
+ buildTime: string;
9
+ builder?: string;
10
+ };
11
+ export type BuildInfoPluginOptions = {
12
+ globalName?: string;
13
+ jsonFileName?: string;
14
+ metaName?: string;
15
+ includeBuilder?: boolean;
16
+ };
17
+ export default function buildInfoPlugin(options?: BuildInfoPluginOptions): Plugin;
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAElC,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,sBAAsB,GAAG;IACnC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,cAAc,CAAC,EAAE,OAAO,CAAA;CACzB,CAAA;AAyDD,MAAM,CAAC,OAAO,UAAU,eAAe,CAAC,OAAO,GAAE,sBAA2B,GAAG,MAAM,CA4CpF"}
package/dist/index.js ADDED
@@ -0,0 +1,90 @@
1
+ import { execSync } from 'node:child_process';
2
+ import { readFileSync } from 'node:fs';
3
+ import { hostname, userInfo } from 'node:os';
4
+ import { resolve } from 'node:path';
5
+ const defaultOptions = {
6
+ globalName: '__APP_INFO__',
7
+ jsonFileName: 'version.json',
8
+ metaName: 'app-build-info',
9
+ includeBuilder: true,
10
+ };
11
+ function git(cmd, cwd) {
12
+ try {
13
+ return execSync(cmd, { cwd, encoding: 'utf-8' }).trim();
14
+ }
15
+ catch {
16
+ return 'unknown';
17
+ }
18
+ }
19
+ function formatGitTime(value) {
20
+ if (value === 'unknown')
21
+ return value;
22
+ const date = new Date(value);
23
+ if (Number.isNaN(date.getTime()))
24
+ return 'unknown';
25
+ return date.toLocaleString('sv');
26
+ }
27
+ function readPackageJson(root) {
28
+ try {
29
+ return JSON.parse(readFileSync(resolve(root, 'package.json'), 'utf-8'));
30
+ }
31
+ catch {
32
+ return {};
33
+ }
34
+ }
35
+ function createBuildInfo(root, includeBuilder) {
36
+ const pkg = readPackageJson(root);
37
+ const info = {
38
+ name: pkg.name ?? 'unknown',
39
+ version: pkg.version ?? 'unknown',
40
+ commit: git('git rev-parse --short=8 HEAD', root),
41
+ branch: git('git rev-parse --abbrev-ref HEAD', root),
42
+ commitTime: formatGitTime(git('git log -1 --format=%cI', root)),
43
+ buildTime: new Date().toLocaleString('sv'),
44
+ };
45
+ if (includeBuilder) {
46
+ info.builder = `${userInfo().username}@${hostname()}`;
47
+ }
48
+ return info;
49
+ }
50
+ export default function buildInfoPlugin(options = {}) {
51
+ const resolvedOptions = { ...defaultOptions, ...options };
52
+ let buildInfo;
53
+ function getBuildInfo(root = process.cwd()) {
54
+ buildInfo ??= createBuildInfo(root, resolvedOptions.includeBuilder);
55
+ return buildInfo;
56
+ }
57
+ return {
58
+ name: 'vite-plugin-app-build-info',
59
+ config(config) {
60
+ const root = config.root ? resolve(config.root) : process.cwd();
61
+ const info = getBuildInfo(root);
62
+ return {
63
+ define: {
64
+ [resolvedOptions.globalName]: JSON.stringify(info),
65
+ },
66
+ };
67
+ },
68
+ transformIndexHtml() {
69
+ const info = getBuildInfo();
70
+ const content = [info.commit, info.buildTime, info.builder].filter(Boolean).join(' | ');
71
+ return [
72
+ {
73
+ tag: 'meta',
74
+ attrs: {
75
+ name: resolvedOptions.metaName,
76
+ content,
77
+ },
78
+ injectTo: 'head',
79
+ },
80
+ ];
81
+ },
82
+ generateBundle() {
83
+ this.emitFile({
84
+ type: 'asset',
85
+ fileName: resolvedOptions.jsonFileName,
86
+ source: JSON.stringify(getBuildInfo(), null, 2),
87
+ });
88
+ },
89
+ };
90
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "vite-plugin-app-build-info",
3
+ "version": "0.1.0",
4
+ "description": "Inject app build metadata into Vite bundles, HTML, and version.json.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "vite",
9
+ "vite-plugin",
10
+ "build-info",
11
+ "version",
12
+ "metadata"
13
+ ],
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "publishConfig": {
26
+ "access": "public",
27
+ "registry": "https://registry.npmjs.org/"
28
+ },
29
+ "scripts": {
30
+ "build": "tsc -p tsconfig.json"
31
+ },
32
+ "peerDependencies": {
33
+ "vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^24.13.2",
37
+ "typescript": "~6.0.0",
38
+ "vite": "^8.0.16"
39
+ }
40
+ }