unplugin-version-injector 1.1.1 → 1.1.2-beta.1

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 CHANGED
@@ -1,178 +1,134 @@
1
- # 🚀 `unplugin-version-injector` - Auto Inject Version & Build Time into HTML
1
+ ### **🚀 `unplugin-version-injector` - Auto Inject Version & Build Time**
2
2
 
3
- [🇨🇳 中文文档](./README.zh-CN.md) | [🇺🇸 English README](./README.md)
3
+ [🇨🇳 中文 README](./README.zh-CN.md) | [🇬🇧 English README](./README.md)
4
4
 
5
5
  ---
6
6
 
7
- ## 📌 Introduction
7
+ ## **📌 Introduction**
8
+ `unplugin-version-injector` is a powerful and lightweight plugin that automatically injects the **version number** and **build timestamp** into all HTML files. It supports **Webpack 4/5, Vite, and Rollup**, making it ideal for both **Single Page Applications (SPA)** and **Multi-Page Applications (MPA)**.
8
9
 
9
- `unplugin-version-injector` is a lightweight universal plugin that automatically injects **version number** and **build timestamp** into HTML files during build.
10
- It supports **Webpack 4/5**, **Vite**, and **Rollup**, and works perfectly in **SPA** and **MPA** projects.
10
+ ### **✨ Features**
11
+ **Auto-injects** `<meta name="version">` into all HTML `<head>` sections
12
+ ✅ **Auto-injects a `<script>`** that logs `version` & `build time` in the browser console
13
+ ✅ **Supports Webpack 4 & 5, Vite, and Rollup**
14
+ ✅ **Works in Multi-Page Applications (MPA)**
15
+ ✅ **Highly configurable**: Supports manually specifying the version or using `package.json`
11
16
 
12
17
  ---
13
18
 
14
- ## Features
15
-
16
- - Auto-injects `<meta name="version">` into HTML `<head>`
17
- - Auto-injects `<script>` to log version/build time to the browser console
18
- - ✅ Supports Webpack 4, Webpack 5, Vite, and Rollup
19
- - ✅ Supports Multi-Page Applications (MPA)
20
- - ✅ Supports custom version & timestamp format
21
- - ✅ Tiny, fast, and zero-runtime dependency
22
-
23
- ---
24
-
25
- ## 📦 Installation
19
+ ## **📦 Installation**
20
+ ```sh
21
+ # Using Yarn
22
+ yarn add -D unplugin-version-injector
26
23
 
27
- ```bash
28
- # With npm
24
+ # Using npm
29
25
  npm install -D unplugin-version-injector
30
-
31
- # With yarn
32
- yarn add -D unplugin-version-injector
33
26
  ```
34
27
 
35
28
  ---
36
29
 
37
- ## 🚀 Usage
38
-
39
- ### ✅ Vite
30
+ ## **🚀 Usage**
40
31
 
41
- ```ts
42
- // vite.config.ts
43
- import versionInjector from 'unplugin-version-injector/vite';
32
+ ### **📌 Webpack 4/5**
33
+ Modify your `webpack.config.js`:
34
+ ```js
35
+ const versionInjectorPlugin = require('unplugin-version-injector');
44
36
 
45
- export default {
46
- plugins: [versionInjector()],
37
+ module.exports = {
38
+ plugins: [
39
+ versionInjectorPlugin.webpack({
40
+ version: '1.2.3', // (Optional) Manually specify version
41
+ })
42
+ ],
47
43
  };
48
44
  ```
49
45
 
50
46
  ---
51
47
 
52
- ### Webpack 4 / 5
53
-
48
+ ### **📌 Vite**
49
+ Modify your `vite.config.js`:
54
50
  ```js
55
- // webpack.config.js
56
- const versionInjector = require('unplugin-version-injector/webpack');
51
+ import versionInjectorPlugin from 'unplugin-version-injector';
57
52
 
58
- module.exports = {
59
- plugins: [
60
- versionInjector({
61
- version: '1.2.3',
62
- injectToHead: true,
63
- injectToBody: true,
64
- }),
65
- ],
53
+ export default {
54
+ plugins: [versionInjectorPlugin.vite()]
66
55
  };
67
56
  ```
68
57
 
69
58
  ---
70
59
 
71
- ### Rollup
72
-
60
+ ### **📌 Rollup**
61
+ Modify your `rollup.config.js`:
73
62
  ```js
74
- // rollup.config.js
75
- import versionInjector from 'unplugin-version-injector/rollup';
63
+ import versionInjectorPlugin from 'unplugin-version-injector';
76
64
 
77
65
  export default {
78
- plugins: [
79
- versionInjector({
80
- dateFormat: 'YYYY-MM-DD HH:mm:ss',
81
- }),
82
- ],
66
+ plugins: [versionInjectorPlugin.rollup()]
83
67
  };
84
68
  ```
85
69
 
86
70
  ---
87
71
 
88
- ## 📜 Output Example
89
-
72
+ ## **📜 Example Output**
73
+ After building, all HTML files will include the following:
90
74
  ```html
91
75
  <head>
92
76
  <meta name="version" content="1.2.3">
93
77
  <meta charset="UTF-8">
78
+ <title>My App</title>
94
79
  </head>
95
80
  <body>
96
- <h1>Hello</h1>
81
+ <h1>Hello World</h1>
97
82
  <script>
98
- console.log("%c Version: 1.2.3 ", "background: #222; color: #00ff00; font-size: 12px;");
99
- console.log("%c Build Time: 2025-04-10 14:00:00 ", "background: #222; color: #ffcc00; font-size: 12px;");
83
+ console.log("%c Version: 1.2.3 ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
84
+ console.log("%c Build Time: 2024-03-01T12:00:00.000Z ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
100
85
  </script>
101
86
  </body>
102
87
  ```
103
88
 
104
- ✅ Console output example:
105
-
89
+ **Console Output (Colored Logs)**
106
90
  ```
107
- 🟢 Version: 1.2.3
108
- 🟡 Build Time: 2025-04-10 14:00:00
91
+ 🟢 Version: 1.2.3 (Green)
92
+ 🟡 Build Time: 2024-03-01T12:00:00.000Z (Yellow)
109
93
  ```
110
94
 
111
95
  ---
112
96
 
113
- ## 🔧 Configuration Options
114
-
115
- | Option | Type | Default | Description |
116
- |----------------|-----------------------------|-----------------------------------|-------------|
117
- | `version` | `string` | Auto from `package.json` | Manually specify a version |
118
- | `formatDate` | `(date: Date) => string` | `YYYY-MM-DD HH:mm:ss` | Custom date formatter |
119
- | `dateFormat` | `string` | None | Uses `dayjs` to format time (requires installation) |
120
- | `injectToHead` | `boolean` | `true` | Injects `<meta name="version">` into `<head>` |
121
- | `injectToBody` | `boolean` | `true` | Injects version log `<script>` into `<body>` |
122
-
123
- 📦 If using `dateFormat`, please install `dayjs` manually:
124
-
125
- ```bash
126
- npm install dayjs
127
- ```
128
-
129
- ---
97
+ ## **🔧 Configuration Options**
98
+ | **Option** | **Type** | **Description** | **Default** |
99
+ |------------|---------|----------------|-------------|
100
+ | `version` | `string` | Custom version (e.g., `1.2.3`) | Auto-read from `package.json` |
101
+ | `log` | `boolean` | Enable/Disable console logs | `true` |
102
+ | `dateFormat` | `string` | Format for build time | `ISO 8601` |
130
103
 
131
- ## 📌 Custom Example
132
-
133
- ```ts
134
- versionInjector({
135
- version: '2.0.0',
136
- injectToHead: true,
137
- injectToBody: false,
138
- dateFormat: 'YYYY/MM/DD HH:mm:ss',
104
+ ### **Example: Custom Config**
105
+ ```js
106
+ versionInjectorPlugin.webpack({
107
+ version: '2.0.0',
108
+ log: false, // Disable console logs
139
109
  });
140
110
  ```
141
111
 
142
112
  ---
143
113
 
144
- ## 🧪 Supported Build Tools
145
-
146
- | Build Tool | Supported | Description |
147
- |--------------|-----------|----------------------------------|
148
- | **Vite** | ✅ | Uses `transformIndexHtml` hook |
149
- | **Webpack 5**| ✅ | Uses `processAssets` hook |
150
- | **Webpack 4**| ✅ | Uses `emit` hook |
151
- | **Rollup** | ✅ | Uses `generateBundle` hook |
114
+ ## **🌍 Why Use This Plugin?**
115
+ - 🛠 **Works out of the box**: No extra setup needed
116
+ - 🚀 **Improves debugging**: Always know what version is running in production
117
+ - 📅 **Track build times**: Useful for monitoring deployments
118
+ - 🎯 **Lightweight & fast**: Minimal overhead with maximum benefits
152
119
 
153
120
  ---
154
121
 
155
- ## 💡 Use Cases
156
-
157
- - 🧪 Quickly identify deployed version & build time
158
- - 📅 Useful for deployment tracking / diagnostics
159
- - ⚡️ No runtime cost – build-time only
122
+ ## **📜 License**
123
+ MIT License © 2024 [Nian YI](https://github.com/nianyi778)
160
124
 
161
125
  ---
162
126
 
163
- ## 📄 License
164
-
165
- MIT © [Nian YI](https://github.com/nianyi778)
166
-
167
- ---
168
-
169
- ## 🤝 Contributing
170
-
171
- Contributions are welcome!
127
+ ## **💡 Contributing**
128
+ Pull requests are welcome! If you encounter any issues, feel free to open an issue on GitHub.
172
129
 
173
- GitHub Repo:
174
- [https://github.com/nianyi778/unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
130
+ **GitHub Repository:** [🔗 unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
175
131
 
176
132
  ---
177
133
 
178
- 🔥 `unplugin-version-injector` – The simplest way to track version and build time in your HTML!
134
+ 🔥 **`unplugin-version-injector` – The simplest way to keep track of your app's version & build time!** 🚀
package/README.zh-CN.md CHANGED
@@ -1,178 +1,135 @@
1
- # 🚀 `unplugin-version-injector` - 自动注入版本号 & 构建时间
1
+ # **🚀 `unplugin-version-injector` - 自动注入版本号 & 构建时间**
2
2
 
3
- [🇬🇧 English README](./README.md) | [🇨🇳 中文 README](./README.zh-CN.md)
3
+ [🇬🇧 English README](./README.md) | [🇨🇳 中文 README](./README.zh-CN.md)
4
4
 
5
5
  ---
6
6
 
7
- ## 📌 简介
7
+ ## **📌 简介**
8
+ `unplugin-version-injector` 是一个 **轻量级** 插件,可自动将 **版本号** 和 **构建时间** 注入到所有 HTML 文件中。
9
+ 支持 **Webpack 4/5、Vite 和 Rollup**,适用于 **单页应用 (SPA) 和 多页应用 (MPA)**。
8
10
 
9
- `unplugin-version-injector` 是一个 **轻量级通用插件**,可在构建时自动将 **版本号** 和 **构建时间** 注入 HTML 文件中。
10
- 兼容 **Webpack 4/5、Vite Rollup**,适用于 **SPA / MPA** 多种项目结构。
11
+ ### **✨ 功能特点**
12
+ **自动注入** `<meta name="version">` 到所有 HTML `<head>` 部分
13
+ ✅ **自动注入 `<script>`**,在浏览器控制台打印 `版本号` & `构建时间`
14
+ ✅ **兼容 Webpack 4 & 5、Vite 和 Rollup**
15
+ ✅ **支持多页应用 (MPA)**,不会遗漏任何 HTML
16
+ ✅ **支持手动指定版本号**,默认读取 `package.json`
11
17
 
12
18
  ---
13
19
 
14
- ## 功能特点
15
-
16
- - 自动注入 `<meta name="version">` 到 HTML `<head>`
17
- - 自动注入 `<script>`,在浏览器控制台打印版本号与构建时间
18
- - ✅ 支持 Webpack 4、Webpack 5、Vite、Rollup
19
- - ✅ 支持多页应用(MPA),自动处理所有 HTML 文件
20
- - ✅ 支持自定义版本号、自定义构建时间格式
21
- - ✅ 体积小、零运行时依赖
22
-
23
- ---
24
-
25
- ## 📦 安装
20
+ ## **📦 安装**
21
+ ```sh
22
+ # 使用 Yarn
23
+ yarn add -D unplugin-version-injector
26
24
 
27
- ```bash
28
25
  # 使用 npm
29
26
  npm install -D unplugin-version-injector
30
-
31
- # 使用 yarn
32
- yarn add -D unplugin-version-injector
33
27
  ```
34
28
 
35
29
  ---
36
30
 
37
- ## 🚀 使用方法
38
-
39
- ### ✅ Vite
31
+ ## **🚀 使用方法**
40
32
 
41
- ```ts
42
- // vite.config.ts
43
- import versionInjector from 'unplugin-version-injector/vite';
33
+ ### **📌 Webpack 4/5**
34
+ 修改 `webpack.config.js`:
35
+ ```js
36
+ const versionInjectorPlugin = require('unplugin-version-injector');
44
37
 
45
- export default {
46
- plugins: [versionInjector()],
38
+ module.exports = {
39
+ plugins: [
40
+ versionInjectorPlugin.webpack({
41
+ version: '1.2.3', // (可选)手动指定版本号
42
+ })
43
+ ],
47
44
  };
48
45
  ```
49
46
 
50
47
  ---
51
48
 
52
- ### Webpack 4 / 5
53
-
49
+ ### **📌 Vite**
50
+ 修改 `vite.config.js`:
54
51
  ```js
55
- // webpack.config.js
56
- const versionInjector = require('unplugin-version-injector/webpack');
52
+ import versionInjectorPlugin from 'unplugin-version-injector';
57
53
 
58
- module.exports = {
59
- plugins: [
60
- versionInjector({
61
- version: '1.2.3',
62
- injectToHead: true,
63
- injectToBody: true,
64
- }),
65
- ],
54
+ export default {
55
+ plugins: [versionInjectorPlugin.vite()]
66
56
  };
67
57
  ```
68
58
 
69
59
  ---
70
60
 
71
- ### Rollup
72
-
61
+ ### **📌 Rollup**
62
+ 修改 `rollup.config.js`:
73
63
  ```js
74
- // rollup.config.js
75
- import versionInjector from 'unplugin-version-injector/rollup';
64
+ import versionInjectorPlugin from 'unplugin-version-injector';
76
65
 
77
66
  export default {
78
- plugins: [
79
- versionInjector({
80
- dateFormat: 'YYYY-MM-DD HH:mm:ss',
81
- }),
82
- ],
67
+ plugins: [versionInjectorPlugin.rollup()]
83
68
  };
84
69
  ```
85
70
 
86
71
  ---
87
72
 
88
- ## 📜 注入效果示例
89
-
73
+ ## **📜 生成的 HTML 示例**
74
+ 构建完成后,所有 HTML 文件将包含以下内容:
90
75
  ```html
91
76
  <head>
92
77
  <meta name="version" content="1.2.3">
93
78
  <meta charset="UTF-8">
79
+ <title>我的应用</title>
94
80
  </head>
95
81
  <body>
96
- <h1>Hello</h1>
82
+ <h1>Hello World</h1>
97
83
  <script>
98
- console.log("%c 版本号: 1.2.3 ", "background: #222; color: #00ff00; font-size: 12px;");
99
- console.log("%c 构建时间: 2025-04-10 14:00:00 ", "background: #222; color: #ffcc00; font-size: 12px;");
84
+ console.log("%c 版本号: 1.2.3 ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
85
+ console.log("%c 构建时间: 2024-03-01T12:00:00.000Z ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
100
86
  </script>
101
87
  </body>
102
88
  ```
103
89
 
104
- 控制台输出示例:
105
-
90
+ **浏览器控制台输出 (带颜色日志)**
106
91
  ```
107
- 🟢 版本号: 1.2.3
108
- 🟡 构建时间: 2025-04-10 14:00:00
92
+ 🟢 版本号: 1.2.3 (绿色)
93
+ 🟡 构建时间: 2024-03-01T12:00:00.000Z (黄色)
109
94
  ```
110
95
 
111
96
  ---
112
97
 
113
- ## 🔧 配置选项
114
-
115
- | 选项 | 类型 | 默认值 | 说明 |
116
- |-----------------|-------------------------------|-------------------------------|------|
117
- | `version` | `string` | 自动读取 `package.json` | 手动指定版本号 |
118
- | `formatDate` | `(date: Date) => string` | `YYYY-MM-DD HH:mm:ss` | 自定义时间格式函数 |
119
- | `dateFormat` | `string` | 无 | 使用 `dayjs` 格式化(需用户安装) |
120
- | `injectToHead` | `boolean` | `true` | 是否注入 `<meta>` 标签 |
121
- | `injectToBody` | `boolean` | `true` | 是否注入控制台日志脚本 |
122
-
123
- 📦 如果使用 `dateFormat`,请先安装:
124
-
125
- ```bash
126
- npm install dayjs
127
- ```
128
-
129
- ---
98
+ ## **🔧 配置选项**
99
+ | **选项** | **类型** | **描述** | **默认值** |
100
+ |---------|--------|---------|---------|
101
+ | `version` | `string` | 手动指定版本号 (如 `1.2.3`) | 自动读取 `package.json` |
102
+ | `log` | `boolean` | 是否在控制台打印版本信息 | `true` |
103
+ | `dateFormat` | `string` | 自定义构建时间格式 | `ISO 8601` |
130
104
 
131
- ## 📌 自定义配置示例
132
-
133
- ```ts
134
- versionInjector({
135
- version: '2.0.0',
136
- injectToHead: true,
137
- injectToBody: false,
138
- dateFormat: 'YYYY/MM/DD HH:mm:ss',
105
+ ### **📌 自定义配置示例**
106
+ ```js
107
+ versionInjectorPlugin.webpack({
108
+ version: '2.0.0',
109
+ log: false, // 关闭控制台日志
139
110
  });
140
111
  ```
141
112
 
142
113
  ---
143
114
 
144
- ## 🧪 支持的构建工具
145
-
146
- | 构建工具 | 状态 | 说明 |
147
- |--------------|----------|------|
148
- | **Vite** | ✅ 支持 | 使用 `transformIndexHtml` |
149
- | **Webpack 5**| ✅ 支持 | 使用 `processAssets` 钩子 |
150
- | **Webpack 4**| ✅ 支持 | 使用 `emit` 钩子 |
151
- | **Rollup** | ✅ 支持 | 使用 `generateBundle` 钩子 |
115
+ ## **🌍 为什么选择 `unplugin-version-injector`?**
116
+ - 🛠 **开箱即用**:安装后立即生效,无需额外配置
117
+ - 🚀 **提升调试效率**:轻松查看当前版本信息
118
+ - 📅 **追踪构建时间**:方便监控不同版本的发布时间
119
+ - 🎯 **轻量高效**:几乎不会影响构建速度
152
120
 
153
121
  ---
154
122
 
155
- ## 💡 应用场景
156
-
157
- - 🧪 快速定位部署版本与构建时间
158
- - 📅 部署监控 / 运维可见性
159
- - ⚡️ 零运行时依赖,构建时注入
123
+ ## **📜 许可证**
124
+ MIT License © 2024 [Nian YI](https://github.com/nianyi778)
160
125
 
161
126
  ---
162
127
 
163
- ## 📄 许可证
164
-
165
- MIT © [Nian YI](https://github.com/nianyi778)
166
-
167
- ---
168
-
169
- ## 🤝 参与贡献
170
-
171
- 欢迎提交 PR 和 issue!
128
+ ## **💡 贡献**
129
+ 欢迎 PR!如有问题,欢迎在 GitHub 提交 issue。
172
130
 
173
- GitHub 仓库:
174
- [https://github.com/nianyi778/unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
131
+ **GitHub 仓库**:[🔗 unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
175
132
 
176
133
  ---
177
134
 
178
- 🔥 `unplugin-version-injector` —— 让你的应用版本信息透明、可追踪!
135
+ 🔥 **`unplugin-version-injector` - 让你的应用版本管理更简单!** 🚀🚀🚀
@@ -0,0 +1,11 @@
1
+ import { Plugin } from 'rollup';
2
+
3
+ interface VersionInjectorOptions {
4
+ version?: string;
5
+ log?: boolean;
6
+ formatDate?: (date: Date) => string;
7
+ }
8
+
9
+ declare function versionInjector(options?: VersionInjectorOptions): Plugin;
10
+
11
+ export { versionInjector as default };
@@ -0,0 +1,11 @@
1
+ import { Plugin } from 'rollup';
2
+
3
+ interface VersionInjectorOptions {
4
+ version?: string;
5
+ log?: boolean;
6
+ formatDate?: (date: Date) => string;
7
+ }
8
+
9
+ declare function versionInjector(options?: VersionInjectorOptions): Plugin;
10
+
11
+ export { versionInjector as default };
package/dist/rollup.js ADDED
@@ -0,0 +1,85 @@
1
+ 'use strict';
2
+
3
+ var fs = require('fs');
4
+ var path = require('path');
5
+ var url = require('url');
6
+
7
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
+
9
+ var fs__default = /*#__PURE__*/_interopDefault(fs);
10
+ var path__default = /*#__PURE__*/_interopDefault(path);
11
+
12
+ // src/shared/utils.ts
13
+ var import_meta = {};
14
+ function getPackageVersion() {
15
+ try {
16
+ let currentDir;
17
+ const isESM = typeof __dirname === "undefined";
18
+ if (isESM) {
19
+ const metaUrl = import_meta.url || "";
20
+ currentDir = path__default.default.dirname(url.fileURLToPath(metaUrl));
21
+ } else {
22
+ currentDir = __dirname;
23
+ }
24
+ let dir = currentDir;
25
+ while (dir !== path__default.default.parse(dir).root) {
26
+ const pkgPath = path__default.default.join(dir, "package.json");
27
+ if (fs__default.default.existsSync(pkgPath)) {
28
+ const content = fs__default.default.readFileSync(pkgPath, "utf-8");
29
+ const pkg = JSON.parse(content);
30
+ return pkg.version || "0.0.0";
31
+ }
32
+ dir = path__default.default.dirname(dir);
33
+ }
34
+ console.warn("[VersionInjector] package.json not found");
35
+ return "0.0.0";
36
+ } catch (err) {
37
+ console.warn("[VersionInjector] Failed to read package.json:", err);
38
+ return "0.0.0";
39
+ }
40
+ }
41
+ function defaultFormatDate(date) {
42
+ return date.toISOString();
43
+ }
44
+
45
+ // src/core.ts
46
+ function createVersionInjector(options = {}) {
47
+ var _a, _b;
48
+ const version = (_a = options.version) != null ? _a : getPackageVersion();
49
+ ((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
50
+ const metaTag = `<meta name="version" content="${version}">
51
+ `;
52
+ const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
53
+ return function processHtml(html) {
54
+ if (!html.includes('<meta name="version"')) {
55
+ html = html.replace(/<head>/, `<head>
56
+ ${metaTag}`);
57
+ }
58
+ if (!html.includes('<script data-injected="unplugin-version-injector"')) {
59
+ html = html.replace("</body>", ` ${logScript}
60
+ </body>`);
61
+ }
62
+ return html;
63
+ };
64
+ }
65
+
66
+ // src/rollup.ts
67
+ function versionInjector(options = {}) {
68
+ const shouldInject = options.log !== false;
69
+ if (!shouldInject) {
70
+ return { name: "unplugin-version-injector" };
71
+ }
72
+ const processHtml = createVersionInjector(options);
73
+ return {
74
+ name: "unplugin-version-injector",
75
+ generateBundle(_, bundle) {
76
+ for (const file of Object.values(bundle)) {
77
+ if (file.type === "asset" && file.fileName.endsWith(".html")) {
78
+ file.source = processHtml(file.source);
79
+ }
80
+ }
81
+ }
82
+ };
83
+ }
84
+
85
+ module.exports = versionInjector;
@@ -0,0 +1,78 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { fileURLToPath } from 'node:url';
4
+
5
+ // src/shared/utils.ts
6
+ var import_meta = {};
7
+ function getPackageVersion() {
8
+ try {
9
+ let currentDir;
10
+ const isESM = typeof __dirname === "undefined";
11
+ if (isESM) {
12
+ const metaUrl = import_meta.url || "";
13
+ currentDir = path.dirname(fileURLToPath(metaUrl));
14
+ } else {
15
+ currentDir = __dirname;
16
+ }
17
+ let dir = currentDir;
18
+ while (dir !== path.parse(dir).root) {
19
+ const pkgPath = path.join(dir, "package.json");
20
+ if (fs.existsSync(pkgPath)) {
21
+ const content = fs.readFileSync(pkgPath, "utf-8");
22
+ const pkg = JSON.parse(content);
23
+ return pkg.version || "0.0.0";
24
+ }
25
+ dir = path.dirname(dir);
26
+ }
27
+ console.warn("[VersionInjector] package.json not found");
28
+ return "0.0.0";
29
+ } catch (err) {
30
+ console.warn("[VersionInjector] Failed to read package.json:", err);
31
+ return "0.0.0";
32
+ }
33
+ }
34
+ function defaultFormatDate(date) {
35
+ return date.toISOString();
36
+ }
37
+
38
+ // src/core.ts
39
+ function createVersionInjector(options = {}) {
40
+ var _a, _b;
41
+ const version = (_a = options.version) != null ? _a : getPackageVersion();
42
+ ((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
43
+ const metaTag = `<meta name="version" content="${version}">
44
+ `;
45
+ const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
46
+ return function processHtml(html) {
47
+ if (!html.includes('<meta name="version"')) {
48
+ html = html.replace(/<head>/, `<head>
49
+ ${metaTag}`);
50
+ }
51
+ if (!html.includes('<script data-injected="unplugin-version-injector"')) {
52
+ html = html.replace("</body>", ` ${logScript}
53
+ </body>`);
54
+ }
55
+ return html;
56
+ };
57
+ }
58
+
59
+ // src/rollup.ts
60
+ function versionInjector(options = {}) {
61
+ const shouldInject = options.log !== false;
62
+ if (!shouldInject) {
63
+ return { name: "unplugin-version-injector" };
64
+ }
65
+ const processHtml = createVersionInjector(options);
66
+ return {
67
+ name: "unplugin-version-injector",
68
+ generateBundle(_, bundle) {
69
+ for (const file of Object.values(bundle)) {
70
+ if (file.type === "asset" && file.fileName.endsWith(".html")) {
71
+ file.source = processHtml(file.source);
72
+ }
73
+ }
74
+ }
75
+ };
76
+ }
77
+
78
+ export { versionInjector as default };