unplugin-version-injector 1.0.2 โ†’ 1.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 CHANGED
@@ -1,134 +1,178 @@
1
- ### **๐Ÿš€ `unplugin-version-injector` - Auto Inject Version & Build Time**
1
+ # ๐Ÿš€ `unplugin-version-injector` - Auto Inject Version & Build Time into HTML
2
2
 
3
- [๐Ÿ‡จ๐Ÿ‡ณ ไธญๆ–‡ README](./README.zh-CN.md) | [๐Ÿ‡ฌ๐Ÿ‡ง English README](./README.md)
3
+ [๐Ÿ‡จ๐Ÿ‡ณ ไธญๆ–‡ๆ–‡ๆกฃ](./README.zh-CN.md) | [๐Ÿ‡บ๐Ÿ‡ธ English README](./README.md)
4
4
 
5
5
  ---
6
6
 
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)**.
7
+ ## ๐Ÿ“Œ Introduction
9
8
 
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`
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.
16
11
 
17
12
  ---
18
13
 
19
- ## **๐Ÿ“ฆ Installation**
20
- ```sh
21
- # Using Yarn
22
- yarn add -D unplugin-version-injector
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
23
26
 
24
- # Using npm
27
+ ```bash
28
+ # With npm
25
29
  npm install -D unplugin-version-injector
30
+
31
+ # With yarn
32
+ yarn add -D unplugin-version-injector
26
33
  ```
27
34
 
28
35
  ---
29
36
 
30
- ## **๐Ÿš€ Usage**
37
+ ## ๐Ÿš€ Usage
31
38
 
32
- ### **๐Ÿ“Œ Webpack 4/5**
33
- Modify your `webpack.config.js`:
34
- ```js
35
- const versionInjectorPlugin = require('unplugin-version-injector');
39
+ ### โœ… Vite
36
40
 
37
- module.exports = {
38
- plugins: [
39
- versionInjectorPlugin.webpack({
40
- version: '1.2.3', // (Optional) Manually specify version
41
- })
42
- ],
41
+ ```ts
42
+ // vite.config.ts
43
+ import versionInjector from 'unplugin-version-injector/vite';
44
+
45
+ export default {
46
+ plugins: [versionInjector()],
43
47
  };
44
48
  ```
45
49
 
46
50
  ---
47
51
 
48
- ### **๐Ÿ“Œ Vite**
49
- Modify your `vite.config.js`:
52
+ ### โœ… Webpack 4 / 5
53
+
50
54
  ```js
51
- import versionInjectorPlugin from 'unplugin-version-injector';
55
+ // webpack.config.js
56
+ const versionInjector = require('unplugin-version-injector/webpack');
52
57
 
53
- export default {
54
- plugins: [versionInjectorPlugin.vite()]
58
+ module.exports = {
59
+ plugins: [
60
+ versionInjector({
61
+ version: '1.2.3',
62
+ injectToHead: true,
63
+ injectToBody: true,
64
+ }),
65
+ ],
55
66
  };
56
67
  ```
57
68
 
58
69
  ---
59
70
 
60
- ### **๐Ÿ“Œ Rollup**
61
- Modify your `rollup.config.js`:
71
+ ### โœ… Rollup
72
+
62
73
  ```js
63
- import versionInjectorPlugin from 'unplugin-version-injector';
74
+ // rollup.config.js
75
+ import versionInjector from 'unplugin-version-injector/rollup';
64
76
 
65
77
  export default {
66
- plugins: [versionInjectorPlugin.rollup()]
78
+ plugins: [
79
+ versionInjector({
80
+ dateFormat: 'YYYY-MM-DD HH:mm:ss',
81
+ }),
82
+ ],
67
83
  };
68
84
  ```
69
85
 
70
86
  ---
71
87
 
72
- ## **๐Ÿ“œ Example Output**
73
- After building, all HTML files will include the following:
88
+ ## ๐Ÿ“œ Output Example
89
+
74
90
  ```html
75
91
  <head>
76
92
  <meta name="version" content="1.2.3">
77
93
  <meta charset="UTF-8">
78
- <title>My App</title>
79
94
  </head>
80
95
  <body>
81
- <h1>Hello World</h1>
96
+ <h1>Hello</h1>
82
97
  <script>
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;");
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;");
85
100
  </script>
86
101
  </body>
87
102
  ```
88
103
 
89
- โœ… **Console Output (Colored Logs)**
104
+ โœ… Console output example:
105
+
90
106
  ```
91
- ๐ŸŸข Version: 1.2.3 (Green)
92
- ๐ŸŸก Build Time: 2024-03-01T12:00:00.000Z (Yellow)
107
+ ๐ŸŸข Version: 1.2.3
108
+ ๐ŸŸก Build Time: 2025-04-10 14:00:00
93
109
  ```
94
110
 
95
111
  ---
96
112
 
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` |
113
+ ## ๐Ÿ”ง Configuration Options
103
114
 
104
- ### **Example: Custom Config**
105
- ```js
106
- versionInjectorPlugin.webpack({
107
- version: '2.0.0',
108
- log: false, // Disable console logs
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
+ ---
130
+
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',
109
139
  });
110
140
  ```
111
141
 
112
142
  ---
113
143
 
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
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 |
119
152
 
120
153
  ---
121
154
 
122
- ## **๐Ÿ“œ License**
123
- MIT License ยฉ 2024 [Nian YI](https://github.com/nianyi778)
155
+ ## ๐Ÿ’ก Use Cases
156
+
157
+ - ๐Ÿงช Quickly identify deployed version & build time
158
+ - ๐Ÿ“… Useful for deployment tracking / diagnostics
159
+ - โšก๏ธ No runtime cost โ€“ build-time only
124
160
 
125
161
  ---
126
162
 
127
- ## **๐Ÿ’ก Contributing**
128
- Pull requests are welcome! If you encounter any issues, feel free to open an issue on GitHub.
163
+ ## ๐Ÿ“„ License
164
+
165
+ MIT ยฉ [Nian YI](https://github.com/nianyi778)
166
+
167
+ ---
168
+
169
+ ## ๐Ÿค Contributing
170
+
171
+ Contributions are welcome!
129
172
 
130
- **GitHub Repository:** [๐Ÿ”— unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
173
+ GitHub Repo:
174
+ [https://github.com/nianyi778/unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
131
175
 
132
176
  ---
133
177
 
134
- ๐Ÿ”ฅ **`unplugin-version-injector` โ€“ The simplest way to keep track of your app's version & build time!** ๐Ÿš€
178
+ ๐Ÿ”ฅ `unplugin-version-injector` โ€“ The simplest way to track version and build time in your HTML!
package/README.zh-CN.md CHANGED
@@ -1,135 +1,178 @@
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
- ## **๐Ÿ“Œ ็ฎ€ไป‹**
8
- `unplugin-version-injector` ๆ˜ฏไธ€ไธช **่ฝป้‡็บง** ๆ’ไปถ๏ผŒๅฏ่‡ชๅŠจๅฐ† **็‰ˆๆœฌๅท** ๅ’Œ **ๆž„ๅปบๆ—ถ้—ด** ๆณจๅ…ฅๅˆฐๆ‰€ๆœ‰ HTML ๆ–‡ไปถไธญใ€‚
9
- ๆ”ฏๆŒ **Webpack 4/5ใ€Vite ๅ’Œ Rollup**๏ผŒ้€‚็”จไบŽ **ๅ•้กตๅบ”็”จ (SPA) ๅ’Œ ๅคš้กตๅบ”็”จ (MPA)**ใ€‚
7
+ ## ๐Ÿ“Œ ็ฎ€ไป‹
10
8
 
11
- ### **โœจ ๅŠŸ่ƒฝ็‰น็‚น**
12
- โœ… **่‡ชๅŠจๆณจๅ…ฅ** `<meta name="version">` ๅˆฐๆ‰€ๆœ‰ HTML `<head>` ้ƒจๅˆ†
13
- โœ… **่‡ชๅŠจๆณจๅ…ฅ `<script>`**๏ผŒๅœจๆต่งˆๅ™จๆŽงๅˆถๅฐๆ‰“ๅฐ `็‰ˆๆœฌๅท` & `ๆž„ๅปบๆ—ถ้—ด`
14
- โœ… **ๅ…ผๅฎน Webpack 4 & 5ใ€Vite ๅ’Œ Rollup**
15
- โœ… **ๆ”ฏๆŒๅคš้กตๅบ”็”จ (MPA)**๏ผŒไธไผš้—ๆผไปปไฝ• HTML
16
- โœ… **ๆ”ฏๆŒๆ‰‹ๅŠจๆŒ‡ๅฎš็‰ˆๆœฌๅท**๏ผŒ้ป˜่ฎค่ฏปๅ– `package.json`
9
+ `unplugin-version-injector` ๆ˜ฏไธ€ไธช **่ฝป้‡็บง้€š็”จๆ’ไปถ**๏ผŒๅฏๅœจๆž„ๅปบๆ—ถ่‡ชๅŠจๅฐ† **็‰ˆๆœฌๅท** ๅ’Œ **ๆž„ๅปบๆ—ถ้—ด** ๆณจๅ…ฅ HTML ๆ–‡ไปถไธญใ€‚
10
+ ๅ…ผๅฎน **Webpack 4/5ใ€Vite ๅ’Œ Rollup**๏ผŒ้€‚็”จไบŽ **SPA / MPA** ๅคš็ง้กน็›ฎ็ป“ๆž„ใ€‚
17
11
 
18
12
  ---
19
13
 
20
- ## **๐Ÿ“ฆ ๅฎ‰่ฃ…**
21
- ```sh
22
- # ไฝฟ็”จ Yarn
23
- yarn add -D unplugin-version-injector
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
+ ## ๐Ÿ“ฆ ๅฎ‰่ฃ…
24
26
 
27
+ ```bash
25
28
  # ไฝฟ็”จ npm
26
29
  npm install -D unplugin-version-injector
30
+
31
+ # ไฝฟ็”จ yarn
32
+ yarn add -D unplugin-version-injector
27
33
  ```
28
34
 
29
35
  ---
30
36
 
31
- ## **๐Ÿš€ ไฝฟ็”จๆ–นๆณ•**
37
+ ## ๐Ÿš€ ไฝฟ็”จๆ–นๆณ•
32
38
 
33
- ### **๐Ÿ“Œ Webpack 4/5**
34
- ไฟฎๆ”น `webpack.config.js`๏ผš
35
- ```js
36
- const versionInjectorPlugin = require('unplugin-version-injector');
39
+ ### โœ… Vite
37
40
 
38
- module.exports = {
39
- plugins: [
40
- versionInjectorPlugin.webpack({
41
- version: '1.2.3', // ๏ผˆๅฏ้€‰๏ผ‰ๆ‰‹ๅŠจๆŒ‡ๅฎš็‰ˆๆœฌๅท
42
- })
43
- ],
41
+ ```ts
42
+ // vite.config.ts
43
+ import versionInjector from 'unplugin-version-injector/vite';
44
+
45
+ export default {
46
+ plugins: [versionInjector()],
44
47
  };
45
48
  ```
46
49
 
47
50
  ---
48
51
 
49
- ### **๐Ÿ“Œ Vite**
50
- ไฟฎๆ”น `vite.config.js`๏ผš
52
+ ### โœ… Webpack 4 / 5
53
+
51
54
  ```js
52
- import versionInjectorPlugin from 'unplugin-version-injector';
55
+ // webpack.config.js
56
+ const versionInjector = require('unplugin-version-injector/webpack');
53
57
 
54
- export default {
55
- plugins: [versionInjectorPlugin.vite()]
58
+ module.exports = {
59
+ plugins: [
60
+ versionInjector({
61
+ version: '1.2.3',
62
+ injectToHead: true,
63
+ injectToBody: true,
64
+ }),
65
+ ],
56
66
  };
57
67
  ```
58
68
 
59
69
  ---
60
70
 
61
- ### **๐Ÿ“Œ Rollup**
62
- ไฟฎๆ”น `rollup.config.js`๏ผš
71
+ ### โœ… Rollup
72
+
63
73
  ```js
64
- import versionInjectorPlugin from 'unplugin-version-injector';
74
+ // rollup.config.js
75
+ import versionInjector from 'unplugin-version-injector/rollup';
65
76
 
66
77
  export default {
67
- plugins: [versionInjectorPlugin.rollup()]
78
+ plugins: [
79
+ versionInjector({
80
+ dateFormat: 'YYYY-MM-DD HH:mm:ss',
81
+ }),
82
+ ],
68
83
  };
69
84
  ```
70
85
 
71
86
  ---
72
87
 
73
- ## **๐Ÿ“œ ็”Ÿๆˆ็š„ HTML ็คบไพ‹**
74
- ๆž„ๅปบๅฎŒๆˆๅŽ๏ผŒๆ‰€ๆœ‰ HTML ๆ–‡ไปถๅฐ†ๅŒ…ๅซไปฅไธ‹ๅ†…ๅฎน๏ผš
88
+ ## ๐Ÿ“œ ๆณจๅ…ฅๆ•ˆๆžœ็คบไพ‹
89
+
75
90
  ```html
76
91
  <head>
77
92
  <meta name="version" content="1.2.3">
78
93
  <meta charset="UTF-8">
79
- <title>ๆˆ‘็š„ๅบ”็”จ</title>
80
94
  </head>
81
95
  <body>
82
- <h1>Hello World</h1>
96
+ <h1>Hello</h1>
83
97
  <script>
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;");
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;");
86
100
  </script>
87
101
  </body>
88
102
  ```
89
103
 
90
- โœ… **ๆต่งˆๅ™จๆŽงๅˆถๅฐ่พ“ๅ‡บ (ๅธฆ้ขœ่‰ฒๆ—ฅๅฟ—)**
104
+ โœ… ๆŽงๅˆถๅฐ่พ“ๅ‡บ็คบไพ‹๏ผš
105
+
91
106
  ```
92
- ๐ŸŸข ็‰ˆๆœฌๅท: 1.2.3 (็ปฟ่‰ฒ)
93
- ๐ŸŸก ๆž„ๅปบๆ—ถ้—ด: 2024-03-01T12:00:00.000Z (้ป„่‰ฒ)
107
+ ๐ŸŸข ็‰ˆๆœฌๅท: 1.2.3
108
+ ๐ŸŸก ๆž„ๅปบๆ—ถ้—ด: 2025-04-10 14:00:00
94
109
  ```
95
110
 
96
111
  ---
97
112
 
98
- ## **๐Ÿ”ง ้…็ฝฎ้€‰้กน**
99
- | **้€‰้กน** | **็ฑปๅž‹** | **ๆ่ฟฐ** | **้ป˜่ฎคๅ€ผ** |
100
- |---------|--------|---------|---------|
101
- | `version` | `string` | ๆ‰‹ๅŠจๆŒ‡ๅฎš็‰ˆๆœฌๅท (ๅฆ‚ `1.2.3`) | ่‡ชๅŠจ่ฏปๅ– `package.json` |
102
- | `log` | `boolean` | ๆ˜ฏๅฆๅœจๆŽงๅˆถๅฐๆ‰“ๅฐ็‰ˆๆœฌไฟกๆฏ | `true` |
103
- | `dateFormat` | `string` | ่‡ชๅฎšไน‰ๆž„ๅปบๆ—ถ้—ดๆ ผๅผ | `ISO 8601` |
113
+ ## ๐Ÿ”ง ้…็ฝฎ้€‰้กน
104
114
 
105
- ### **๐Ÿ“Œ ่‡ชๅฎšไน‰้…็ฝฎ็คบไพ‹**
106
- ```js
107
- versionInjectorPlugin.webpack({
108
- version: '2.0.0',
109
- log: false, // ๅ…ณ้—ญๆŽงๅˆถๅฐๆ—ฅๅฟ—
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
+ ---
130
+
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',
110
139
  });
111
140
  ```
112
141
 
113
142
  ---
114
143
 
115
- ## **๐ŸŒ ไธบไป€ไนˆ้€‰ๆ‹ฉ `unplugin-version-injector`๏ผŸ**
116
- - ๐Ÿ›  **ๅผ€็ฎฑๅณ็”จ**๏ผšๅฎ‰่ฃ…ๅŽ็ซ‹ๅณ็”Ÿๆ•ˆ๏ผŒๆ— ้œ€้ขๅค–้…็ฝฎ
117
- - ๐Ÿš€ **ๆๅ‡่ฐƒ่ฏ•ๆ•ˆ็އ**๏ผš่ฝปๆพๆŸฅ็œ‹ๅฝ“ๅ‰็‰ˆๆœฌไฟกๆฏ
118
- - ๐Ÿ“… **่ฟฝ่ธชๆž„ๅปบๆ—ถ้—ด**๏ผšๆ–นไพฟ็›‘ๆŽงไธๅŒ็‰ˆๆœฌ็š„ๅ‘ๅธƒๆ—ถ้—ด
119
- - ๐ŸŽฏ **่ฝป้‡้ซ˜ๆ•ˆ**๏ผšๅ‡ ไนŽไธไผšๅฝฑๅ“ๆž„ๅปบ้€Ÿๅบฆ
144
+ ## ๐Ÿงช ๆ”ฏๆŒ็š„ๆž„ๅปบๅทฅๅ…ท
145
+
146
+ | ๆž„ๅปบๅทฅๅ…ท | ็Šถๆ€ | ่ฏดๆ˜Ž |
147
+ |--------------|----------|------|
148
+ | **Vite** | โœ… ๆ”ฏๆŒ | ไฝฟ็”จ `transformIndexHtml` |
149
+ | **Webpack 5**| โœ… ๆ”ฏๆŒ | ไฝฟ็”จ `processAssets` ้’ฉๅญ |
150
+ | **Webpack 4**| โœ… ๆ”ฏๆŒ | ไฝฟ็”จ `emit` ้’ฉๅญ |
151
+ | **Rollup** | โœ… ๆ”ฏๆŒ | ไฝฟ็”จ `generateBundle` ้’ฉๅญ |
120
152
 
121
153
  ---
122
154
 
123
- ## **๐Ÿ“œ ่ฎธๅฏ่ฏ**
124
- MIT License ยฉ 2024 [Nian YI](https://github.com/nianyi778)
155
+ ## ๐Ÿ’ก ๅบ”็”จๅœบๆ™ฏ
156
+
157
+ - ๐Ÿงช ๅฟซ้€Ÿๅฎšไฝ้ƒจ็ฝฒ็‰ˆๆœฌไธŽๆž„ๅปบๆ—ถ้—ด
158
+ - ๐Ÿ“… ้ƒจ็ฝฒ็›‘ๆŽง / ่ฟ็ปดๅฏ่งๆ€ง
159
+ - โšก๏ธ ้›ถ่ฟ่กŒๆ—ถไพ่ต–๏ผŒๆž„ๅปบๆ—ถๆณจๅ…ฅ
125
160
 
126
161
  ---
127
162
 
128
- ## **๐Ÿ’ก ่ดก็Œฎ**
129
- ๆฌข่ฟŽ PR๏ผๅฆ‚ๆœ‰้—ฎ้ข˜๏ผŒๆฌข่ฟŽๅœจ GitHub ๆไบค issueใ€‚
163
+ ## ๐Ÿ“„ ่ฎธๅฏ่ฏ
164
+
165
+ MIT ยฉ [Nian YI](https://github.com/nianyi778)
166
+
167
+ ---
168
+
169
+ ## ๐Ÿค ๅ‚ไธŽ่ดก็Œฎ
170
+
171
+ ๆฌข่ฟŽๆไบค PR ๅ’Œ issue๏ผ
130
172
 
131
- **GitHub ไป“ๅบ“**๏ผš[๐Ÿ”— unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
173
+ GitHub ไป“ๅบ“๏ผš
174
+ [https://github.com/nianyi778/unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
132
175
 
133
176
  ---
134
177
 
135
- ๐Ÿ”ฅ **`unplugin-version-injector` - ่ฎฉไฝ ็š„ๅบ”็”จ็‰ˆๆœฌ็ฎก็†ๆ›ด็ฎ€ๅ•๏ผ** ๐Ÿš€๐Ÿš€๐Ÿš€
178
+ ๐Ÿ”ฅ `unplugin-version-injector` โ€”โ€” ่ฎฉไฝ ็š„ๅบ”็”จ็‰ˆๆœฌไฟกๆฏ้€ๆ˜Žใ€ๅฏ่ฟฝ่ธช๏ผ
@@ -0,0 +1,3 @@
1
+ import type { VersionInjectorOptions } from './types';
2
+ declare const VersionInjectorPlugin: import("unplugin").UnpluginInstance<VersionInjectorOptions | undefined, boolean>;
3
+ export default VersionInjectorPlugin;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ // Updated version with support for both `formatDate` and `dateFormat`
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const unplugin_1 = require("unplugin");
5
+ const webpack_1 = require("webpack");
6
+ const utils_1 = require("./utils");
7
+ let dayjs;
8
+ const VersionInjectorPlugin = (0, unplugin_1.createUnplugin)((options = {}) => {
9
+ const { version = (0, utils_1.getPackageVersion)(), formatDate, dateFormat, injectToHead = true, injectToBody = true, } = options;
10
+ // resolve date formatter
11
+ let resolvedFormatDate = utils_1.defaultFormatDate;
12
+ if (typeof formatDate === 'function') {
13
+ resolvedFormatDate = formatDate;
14
+ }
15
+ else if (typeof dateFormat === 'string') {
16
+ try {
17
+ dayjs = require('dayjs');
18
+ resolvedFormatDate = (d) => dayjs(d).format(dateFormat);
19
+ }
20
+ catch (err) {
21
+ console.warn('[unplugin-version-injector] To use `dateFormat`, please install `dayjs` manually.');
22
+ }
23
+ }
24
+ if (!injectToHead && !injectToBody) {
25
+ return { name: 'unplugin-version-injector' };
26
+ }
27
+ const buildTime = resolvedFormatDate(new Date());
28
+ const metaTag = `<meta name="version" content="${version}">\n`;
29
+ const logScript = `
30
+ <script>
31
+ console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
32
+ console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
33
+ </script>`;
34
+ function processHtml(html) {
35
+ if (injectToHead && !html.includes('<meta name="version"')) {
36
+ html = html.replace(/<head>/, `<head>\n ${metaTag}`);
37
+ }
38
+ if (injectToBody && !html.includes('<script>console.log("%c Version:')) {
39
+ html = html.replace('</body>', ` ${logScript}\n</body>`);
40
+ }
41
+ return html;
42
+ }
43
+ return {
44
+ name: 'unplugin-version-injector',
45
+ vite: {
46
+ transformIndexHtml(html) {
47
+ return processHtml(html);
48
+ },
49
+ },
50
+ rollup: {
51
+ generateBundle(_, bundle) {
52
+ for (const file of Object.values(bundle)) {
53
+ if (file.type === 'asset' && file.fileName.endsWith('.html')) {
54
+ file.source = processHtml(file.source);
55
+ }
56
+ }
57
+ },
58
+ },
59
+ webpack(compiler) {
60
+ const isWebpack5 = typeof webpack_1.sources !== 'undefined' && webpack_1.sources.RawSource;
61
+ if (isWebpack5) {
62
+ compiler.hooks.compilation.tap('unplugin-version-injector', (compilation) => {
63
+ compilation.hooks.processAssets.tap({
64
+ name: 'unplugin-version-injector',
65
+ stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
66
+ }, (assets) => {
67
+ Object.keys(assets).forEach((filename) => {
68
+ if (filename.endsWith('.html')) {
69
+ let source = assets[filename].source().toString();
70
+ source = processHtml(source);
71
+ compilation.updateAsset(filename, new webpack_1.sources.RawSource(source));
72
+ }
73
+ });
74
+ });
75
+ });
76
+ }
77
+ else {
78
+ compiler.hooks.emit.tapAsync('unplugin-version-injector', (compilation, callback) => {
79
+ Object.keys(compilation.assets).forEach((filename) => {
80
+ if (filename.endsWith('.html')) {
81
+ const rawSource = compilation.assets[filename].source().toString();
82
+ const newSource = processHtml(rawSource);
83
+ compilation.assets[filename] = new webpack_1.sources.RawSource(newSource);
84
+ }
85
+ });
86
+ callback();
87
+ });
88
+ }
89
+ },
90
+ };
91
+ });
92
+ if (typeof module !== 'undefined' && module.exports) {
93
+ module.exports = VersionInjectorPlugin;
94
+ }
95
+ exports.default = VersionInjectorPlugin;
@@ -0,0 +1,7 @@
1
+ export interface VersionInjectorOptions {
2
+ version?: string;
3
+ formatDate?: (date: Date) => string;
4
+ dateFormat?: string;
5
+ injectToHead?: boolean;
6
+ injectToBody?: boolean;
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ /** ่Žทๅ– package.json ็‰ˆๆœฌ */
2
+ export declare function getPackageVersion(): string;
3
+ /** ้ป˜่ฎคๆ ผๅผๅŒ– build time */
4
+ export declare function defaultFormatDate(date: Date): string;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.defaultFormatDate = exports.getPackageVersion = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ /** ่Žทๅ– package.json ็‰ˆๆœฌ */
10
+ function getPackageVersion() {
11
+ try {
12
+ const packageJsonPath = path_1.default.resolve(process.cwd(), 'package.json');
13
+ if (fs_1.default.existsSync(packageJsonPath)) {
14
+ return require(packageJsonPath).version;
15
+ }
16
+ }
17
+ catch (error) {
18
+ console.warn('[VersionInjector] Failed to read package.json:', error);
19
+ }
20
+ return '0.0.0';
21
+ }
22
+ exports.getPackageVersion = getPackageVersion;
23
+ /** ้ป˜่ฎคๆ ผๅผๅŒ– build time */
24
+ function defaultFormatDate(date) {
25
+ return date.toISOString();
26
+ }
27
+ exports.defaultFormatDate = defaultFormatDate;
@@ -0,0 +1,3 @@
1
+ import type { VersionInjectorOptions } from './types';
2
+ declare const VersionInjectorPlugin: import("unplugin").UnpluginInstance<VersionInjectorOptions | undefined, boolean>;
3
+ export default VersionInjectorPlugin;
@@ -0,0 +1,93 @@
1
+ // Updated version with support for both `formatDate` and `dateFormat`
2
+ import { createUnplugin } from 'unplugin';
3
+ import { Compilation, sources } from 'webpack';
4
+ import { getPackageVersion, defaultFormatDate } from './utils';
5
+ let dayjs;
6
+ const VersionInjectorPlugin = createUnplugin((options = {}) => {
7
+ const { version = getPackageVersion(), formatDate, dateFormat, injectToHead = true, injectToBody = true, } = options;
8
+ // resolve date formatter
9
+ let resolvedFormatDate = defaultFormatDate;
10
+ if (typeof formatDate === 'function') {
11
+ resolvedFormatDate = formatDate;
12
+ }
13
+ else if (typeof dateFormat === 'string') {
14
+ try {
15
+ dayjs = require('dayjs');
16
+ resolvedFormatDate = (d) => dayjs(d).format(dateFormat);
17
+ }
18
+ catch (err) {
19
+ console.warn('[unplugin-version-injector] To use `dateFormat`, please install `dayjs` manually.');
20
+ }
21
+ }
22
+ if (!injectToHead && !injectToBody) {
23
+ return { name: 'unplugin-version-injector' };
24
+ }
25
+ const buildTime = resolvedFormatDate(new Date());
26
+ const metaTag = `<meta name="version" content="${version}">\n`;
27
+ const logScript = `
28
+ <script>
29
+ console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
30
+ console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
31
+ </script>`;
32
+ function processHtml(html) {
33
+ if (injectToHead && !html.includes('<meta name="version"')) {
34
+ html = html.replace(/<head>/, `<head>\n ${metaTag}`);
35
+ }
36
+ if (injectToBody && !html.includes('<script>console.log("%c Version:')) {
37
+ html = html.replace('</body>', ` ${logScript}\n</body>`);
38
+ }
39
+ return html;
40
+ }
41
+ return {
42
+ name: 'unplugin-version-injector',
43
+ vite: {
44
+ transformIndexHtml(html) {
45
+ return processHtml(html);
46
+ },
47
+ },
48
+ rollup: {
49
+ generateBundle(_, bundle) {
50
+ for (const file of Object.values(bundle)) {
51
+ if (file.type === 'asset' && file.fileName.endsWith('.html')) {
52
+ file.source = processHtml(file.source);
53
+ }
54
+ }
55
+ },
56
+ },
57
+ webpack(compiler) {
58
+ const isWebpack5 = typeof sources !== 'undefined' && sources.RawSource;
59
+ if (isWebpack5) {
60
+ compiler.hooks.compilation.tap('unplugin-version-injector', (compilation) => {
61
+ compilation.hooks.processAssets.tap({
62
+ name: 'unplugin-version-injector',
63
+ stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
64
+ }, (assets) => {
65
+ Object.keys(assets).forEach((filename) => {
66
+ if (filename.endsWith('.html')) {
67
+ let source = assets[filename].source().toString();
68
+ source = processHtml(source);
69
+ compilation.updateAsset(filename, new sources.RawSource(source));
70
+ }
71
+ });
72
+ });
73
+ });
74
+ }
75
+ else {
76
+ compiler.hooks.emit.tapAsync('unplugin-version-injector', (compilation, callback) => {
77
+ Object.keys(compilation.assets).forEach((filename) => {
78
+ if (filename.endsWith('.html')) {
79
+ const rawSource = compilation.assets[filename].source().toString();
80
+ const newSource = processHtml(rawSource);
81
+ compilation.assets[filename] = new sources.RawSource(newSource);
82
+ }
83
+ });
84
+ callback();
85
+ });
86
+ }
87
+ },
88
+ };
89
+ });
90
+ if (typeof module !== 'undefined' && module.exports) {
91
+ module.exports = VersionInjectorPlugin;
92
+ }
93
+ export default VersionInjectorPlugin;
@@ -0,0 +1,7 @@
1
+ export interface VersionInjectorOptions {
2
+ version?: string;
3
+ formatDate?: (date: Date) => string;
4
+ dateFormat?: string;
5
+ injectToHead?: boolean;
6
+ injectToBody?: boolean;
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ /** ่Žทๅ– package.json ็‰ˆๆœฌ */
2
+ export declare function getPackageVersion(): string;
3
+ /** ้ป˜่ฎคๆ ผๅผๅŒ– build time */
4
+ export declare function defaultFormatDate(date: Date): string;
@@ -0,0 +1,19 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ /** ่Žทๅ– package.json ็‰ˆๆœฌ */
4
+ export function getPackageVersion() {
5
+ try {
6
+ const packageJsonPath = path.resolve(process.cwd(), 'package.json');
7
+ if (fs.existsSync(packageJsonPath)) {
8
+ return require(packageJsonPath).version;
9
+ }
10
+ }
11
+ catch (error) {
12
+ console.warn('[VersionInjector] Failed to read package.json:', error);
13
+ }
14
+ return '0.0.0';
15
+ }
16
+ /** ้ป˜่ฎคๆ ผๅผๅŒ– build time */
17
+ export function defaultFormatDate(date) {
18
+ return date.toISOString();
19
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unplugin-version-injector",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "author": "Nian Yi <nianyi778@gmail.com>",
5
5
  "license": "MIT",
6
6
  "description": "A universal plugin to inject version and build time into HTML (supports Webpack, Vite, Rollup)",
@@ -22,7 +22,7 @@
22
22
  "build:esm": "tsc --module ESNext --outDir dist/esm",
23
23
  "build:cjs": "tsc --module CommonJS --outDir dist/cjs",
24
24
  "build": "npm run clean && npm run build:esm && npm run build:cjs",
25
- "publish": "npm run build && npm publish --access public"
25
+ "prepublishOnly": "npm run build"
26
26
  },
27
27
  "dependencies": {
28
28
  "unplugin": "^1.0.0"
@@ -36,5 +36,6 @@
36
36
  "publishConfig": {
37
37
  "access": "public"
38
38
  },
39
+ "files": ["dist","README.md","README.zh-CN.md"],
39
40
  "packageManager": "pnpm@10.5.2+sha512.da9dc28cd3ff40d0592188235ab25d3202add8a207afbedc682220e4a0029ffbff4562102b9e6e46b4e3f9e8bd53e6d05de48544b0c57d4b0179e22c76d1199b"
40
41
  }
package/src/index.ts DELETED
@@ -1,77 +0,0 @@
1
- import { createUnplugin } from 'unplugin';
2
- import { Compilation, sources } from 'webpack';
3
- import type { VersionInjectorOptions } from './types';
4
- import { getPackageVersion, defaultFormatDate } from './utils';
5
-
6
- const VersionInjectorPlugin = createUnplugin((options: VersionInjectorOptions = {}) => {
7
-
8
- const shouldInject = options.log !== false; // โœ… ๅชๆœ‰ log: false ๆ—ถไธๆณจๅ…ฅ
9
-
10
- if (!shouldInject) {
11
- return { name: 'unplugin-version-injector' }; // โŒ ็›ดๆŽฅ่ฟ”ๅ›ž็ฉบๆ’ไปถ๏ผŒ้ฟๅ…ๆ— ๆ„ไน‰็š„ๆ“ไฝœ
12
- }
13
-
14
- const version = options.version || getPackageVersion();
15
- const buildTime = options.formatDate ? options.formatDate(new Date()) : defaultFormatDate(new Date());
16
-
17
- const metaTag = `<meta name="version" content="${version}">\n`;
18
- const logScript = `
19
- <script>
20
- console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
21
- console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
22
- </script>`;
23
-
24
- function processHtml(html: string): string {
25
- if (!html.includes('<meta name="version"')) {
26
- html = html.replace(/<head>/, `<head>\n ${metaTag}`);
27
- }
28
- if (!html.includes('<script>console.log("%c Version:')) {
29
- html = html.replace('</body>', ` ${logScript}\n</body>`);
30
- }
31
- return html;
32
- }
33
-
34
- return {
35
- name: 'unplugin-version-injector',
36
-
37
- // โœ… Vite ้€‚้…
38
- vite: {
39
- transformIndexHtml(html: string) {
40
- return processHtml(html);
41
- }
42
- },
43
-
44
- // โœ… Webpack ้€‚้…
45
- webpack(compiler) {
46
- compiler.hooks.compilation.tap('unplugin-version-injector', (compilation: Compilation) => {
47
- compilation.hooks.processAssets.tap(
48
- {
49
- name: 'unplugin-version-injector',
50
- stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
51
- },
52
- (assets) => {
53
- Object.keys(assets).forEach((filename) => {
54
- if (filename.endsWith('.html')) {
55
- let source = assets[filename].source().toString();
56
- source = processHtml(source);
57
-
58
- compilation.updateAsset(
59
- filename,
60
- new sources.RawSource(source) // โœ… ไฟฎๆญฃ updateAsset ็ฑปๅž‹
61
- );
62
- }
63
- });
64
- }
65
- );
66
- });
67
- }
68
- };
69
- });
70
-
71
- // โœ… ๅ…ผๅฎน Webpack / Vite / Rollup
72
-
73
- if (typeof module !== 'undefined' && module.exports) {
74
- module.exports = VersionInjectorPlugin; // โœ… ่ฎฉ CommonJS ็›ดๆŽฅๆ‹ฟๅˆฐ
75
- }
76
-
77
- export default VersionInjectorPlugin;
package/src/types.ts DELETED
@@ -1,6 +0,0 @@
1
- export interface VersionInjectorOptions {
2
- version?: string; // ็”จๆˆทๆ‰‹ๅŠจไผ ้€’็š„็‰ˆๆœฌๅท
3
- log?: boolean; // ๆ˜ฏๅฆๆ‰“ๅฐๆ—ฅๅฟ—
4
- formatDate?: (date: Date) => string; // ่‡ชๅฎšไน‰ build time ๆ ผๅผๅŒ–
5
- }
6
-
package/src/utils.ts DELETED
@@ -1,20 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
-
4
- /** ่Žทๅ– package.json ็‰ˆๆœฌ */
5
- export function getPackageVersion(): string {
6
- try {
7
- const packageJsonPath = path.resolve(process.cwd(), 'package.json');
8
- if (fs.existsSync(packageJsonPath)) {
9
- return require(packageJsonPath).version;
10
- }
11
- } catch (error) {
12
- console.warn('[VersionInjector] Failed to read package.json:', error);
13
- }
14
- return '0.0.0';
15
- }
16
-
17
- /** ้ป˜่ฎคๆ ผๅผๅŒ– build time */
18
- export function defaultFormatDate(date: Date): string {
19
- return date.toISOString();
20
- }
package/tsconfig.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES6",
4
- "module": "ESNext",
5
- "lib": ["ESNext", "DOM"],
6
- "moduleResolution": "Node",
7
- "strict": true,
8
- "esModuleInterop": true,
9
- "skipLibCheck": true,
10
- "declaration": true,
11
- "outDir": "dist"
12
- },
13
- "include": ["src"]
14
- }
15
-