unplugin-version-injector 1.1.1-alpha.5 โ†’ 1.1.2

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,3 @@
1
+ import type { VersionInjectorOptions } from './types';
2
+ declare const VersionInjectorPlugin: import("unplugin").UnpluginInstance<VersionInjectorOptions | undefined, boolean>;
3
+ export default VersionInjectorPlugin;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const unplugin_1 = require("unplugin");
4
+ const webpack_1 = require("webpack");
5
+ const utils_1 = require("./utils");
6
+ const VersionInjectorPlugin = (0, unplugin_1.createUnplugin)((options = {}) => {
7
+ const shouldInject = options.log !== false; // โœ… ๅชๆœ‰ log: false ๆ—ถไธๆณจๅ…ฅ
8
+ if (!shouldInject) {
9
+ return { name: 'unplugin-version-injector' }; // โŒ ็›ดๆŽฅ่ฟ”ๅ›ž็ฉบๆ’ไปถ๏ผŒ้ฟๅ…ๆ— ๆ„ไน‰็š„ๆ“ไฝœ
10
+ }
11
+ const version = options.version || (0, utils_1.getPackageVersion)();
12
+ const buildTime = options.formatDate ? options.formatDate(new Date()) : (0, utils_1.defaultFormatDate)(new Date());
13
+ const metaTag = `<meta name="version" content="${version}">\n`;
14
+ const logScript = `
15
+ <script>
16
+ console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
17
+ console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
18
+ </script>`;
19
+ function processHtml(html) {
20
+ if (!html.includes('<meta name="version"')) {
21
+ html = html.replace(/<head>/, `<head>\n ${metaTag}`);
22
+ }
23
+ if (!html.includes('<script>console.log("%c Version:')) {
24
+ html = html.replace('</body>', ` ${logScript}\n</body>`);
25
+ }
26
+ return html;
27
+ }
28
+ return {
29
+ name: 'unplugin-version-injector',
30
+ // โœ… Vite ้€‚้…
31
+ vite: {
32
+ transformIndexHtml(html) {
33
+ return processHtml(html);
34
+ }
35
+ },
36
+ // โœ… Webpack ้€‚้…
37
+ webpack(compiler) {
38
+ compiler.hooks.compilation.tap('unplugin-version-injector', (compilation) => {
39
+ compilation.hooks.processAssets.tap({
40
+ name: 'unplugin-version-injector',
41
+ stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
42
+ }, (assets) => {
43
+ Object.keys(assets).forEach((filename) => {
44
+ if (filename.endsWith('.html')) {
45
+ let source = assets[filename].source().toString();
46
+ source = processHtml(source);
47
+ compilation.updateAsset(filename, new webpack_1.sources.RawSource(source) // โœ… ไฟฎๆญฃ updateAsset ็ฑปๅž‹
48
+ );
49
+ }
50
+ });
51
+ });
52
+ });
53
+ }
54
+ };
55
+ });
56
+ // โœ… ๅ…ผๅฎน Webpack / Vite / Rollup
57
+ if (typeof module !== 'undefined' && module.exports) {
58
+ module.exports = VersionInjectorPlugin; // โœ… ่ฎฉ CommonJS ็›ดๆŽฅๆ‹ฟๅˆฐ
59
+ }
60
+ exports.default = VersionInjectorPlugin;
@@ -0,0 +1,5 @@
1
+ export interface VersionInjectorOptions {
2
+ version?: string;
3
+ log?: boolean;
4
+ formatDate?: (date: Date) => string;
5
+ }
@@ -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,58 @@
1
+ import { createUnplugin } from 'unplugin';
2
+ import { Compilation, sources } from 'webpack';
3
+ import { getPackageVersion, defaultFormatDate } from './utils';
4
+ const VersionInjectorPlugin = createUnplugin((options = {}) => {
5
+ const shouldInject = options.log !== false; // โœ… ๅชๆœ‰ log: false ๆ—ถไธๆณจๅ…ฅ
6
+ if (!shouldInject) {
7
+ return { name: 'unplugin-version-injector' }; // โŒ ็›ดๆŽฅ่ฟ”ๅ›ž็ฉบๆ’ไปถ๏ผŒ้ฟๅ…ๆ— ๆ„ไน‰็š„ๆ“ไฝœ
8
+ }
9
+ const version = options.version || getPackageVersion();
10
+ const buildTime = options.formatDate ? options.formatDate(new Date()) : defaultFormatDate(new Date());
11
+ const metaTag = `<meta name="version" content="${version}">\n`;
12
+ const logScript = `
13
+ <script>
14
+ console.log("%c Version: ${version} ", "background: #222; color: #00ff00; font-size: 12px; padding: 4px; border-radius: 4px;");
15
+ console.log("%c Build Time: ${buildTime} ", "background: #222; color: #ffcc00; font-size: 12px; padding: 4px; border-radius: 4px;");
16
+ </script>`;
17
+ function processHtml(html) {
18
+ if (!html.includes('<meta name="version"')) {
19
+ html = html.replace(/<head>/, `<head>\n ${metaTag}`);
20
+ }
21
+ if (!html.includes('<script>console.log("%c Version:')) {
22
+ html = html.replace('</body>', ` ${logScript}\n</body>`);
23
+ }
24
+ return html;
25
+ }
26
+ return {
27
+ name: 'unplugin-version-injector',
28
+ // โœ… Vite ้€‚้…
29
+ vite: {
30
+ transformIndexHtml(html) {
31
+ return processHtml(html);
32
+ }
33
+ },
34
+ // โœ… Webpack ้€‚้…
35
+ webpack(compiler) {
36
+ compiler.hooks.compilation.tap('unplugin-version-injector', (compilation) => {
37
+ compilation.hooks.processAssets.tap({
38
+ name: 'unplugin-version-injector',
39
+ stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
40
+ }, (assets) => {
41
+ Object.keys(assets).forEach((filename) => {
42
+ if (filename.endsWith('.html')) {
43
+ let source = assets[filename].source().toString();
44
+ source = processHtml(source);
45
+ compilation.updateAsset(filename, new sources.RawSource(source) // โœ… ไฟฎๆญฃ updateAsset ็ฑปๅž‹
46
+ );
47
+ }
48
+ });
49
+ });
50
+ });
51
+ }
52
+ };
53
+ });
54
+ // โœ… ๅ…ผๅฎน Webpack / Vite / Rollup
55
+ if (typeof module !== 'undefined' && module.exports) {
56
+ module.exports = VersionInjectorPlugin; // โœ… ่ฎฉ CommonJS ็›ดๆŽฅๆ‹ฟๅˆฐ
57
+ }
58
+ export default VersionInjectorPlugin;