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 +66 -110
- package/README.zh-CN.md +66 -109
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +60 -0
- package/dist/cjs/types.d.ts +5 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/utils.d.ts +4 -0
- package/dist/cjs/utils.js +27 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +58 -0
- package/dist/esm/types.d.ts +5 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/utils.d.ts +4 -0
- package/dist/esm/utils.js +19 -0
- package/package.json +25 -47
- package/dist/core.d.mts +0 -6
- package/dist/core.d.ts +0 -6
- package/dist/core.js +0 -140
- package/dist/core.mjs +0 -112
- package/dist/types-DEOBLqEx.d.mts +0 -9
- package/dist/types-DEOBLqEx.d.ts +0 -9
- package/dist/webpack.d.mts +0 -6
- package/dist/webpack.d.ts +0 -6
- package/dist/webpack.js +0 -146
- package/dist/webpack.mjs +0 -120
package/README.md
CHANGED
@@ -1,178 +1,134 @@
|
|
1
|
-
|
1
|
+
### **๐ `unplugin-version-injector` - Auto Inject Version & Build Time**
|
2
2
|
|
3
|
-
[๐จ๐ณ
|
3
|
+
[๐จ๐ณ ไธญๆ README](./README.zh-CN.md) | [๐ฌ๐ง English README](./README.md)
|
4
4
|
|
5
5
|
---
|
6
6
|
|
7
|
-
##
|
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
|
-
|
10
|
-
|
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
|
-
##
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
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
|
-
##
|
38
|
-
|
39
|
-
### โ
Vite
|
30
|
+
## **๐ Usage**
|
40
31
|
|
41
|
-
|
42
|
-
|
43
|
-
|
32
|
+
### **๐ Webpack 4/5**
|
33
|
+
Modify your `webpack.config.js`:
|
34
|
+
```js
|
35
|
+
const versionInjectorPlugin = require('unplugin-version-injector');
|
44
36
|
|
45
|
-
|
46
|
-
plugins: [
|
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
|
-
###
|
53
|
-
|
48
|
+
### **๐ Vite**
|
49
|
+
Modify your `vite.config.js`:
|
54
50
|
```js
|
55
|
-
|
56
|
-
const versionInjector = require('unplugin-version-injector/webpack');
|
51
|
+
import versionInjectorPlugin from 'unplugin-version-injector';
|
57
52
|
|
58
|
-
|
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
|
-
###
|
72
|
-
|
60
|
+
### **๐ Rollup**
|
61
|
+
Modify your `rollup.config.js`:
|
73
62
|
```js
|
74
|
-
|
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
|
-
##
|
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:
|
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
|
105
|
-
|
89
|
+
โ
**Console Output (Colored Logs)**
|
106
90
|
```
|
107
|
-
๐ข Version: 1.2.3
|
108
|
-
๐ก Build Time:
|
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
|
-
##
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
| `
|
118
|
-
| `
|
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
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
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
|
-
##
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
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
|
-
##
|
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
|
-
##
|
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
|
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
|
-
๐ฅ
|
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
|
-
#
|
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
|
-
|
10
|
-
|
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
|
-
|
17
|
-
|
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
|
-
|
42
|
-
|
43
|
-
|
33
|
+
### **๐ Webpack 4/5**
|
34
|
+
ไฟฎๆน `webpack.config.js`๏ผ
|
35
|
+
```js
|
36
|
+
const versionInjectorPlugin = require('unplugin-version-injector');
|
44
37
|
|
45
|
-
|
46
|
-
plugins: [
|
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
|
-
###
|
53
|
-
|
49
|
+
### **๐ Vite**
|
50
|
+
ไฟฎๆน `vite.config.js`๏ผ
|
54
51
|
```js
|
55
|
-
|
56
|
-
const versionInjector = require('unplugin-version-injector/webpack');
|
52
|
+
import versionInjectorPlugin from 'unplugin-version-injector';
|
57
53
|
|
58
|
-
|
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
|
-
###
|
72
|
-
|
61
|
+
### **๐ Rollup**
|
62
|
+
ไฟฎๆน `rollup.config.js`๏ผ
|
73
63
|
```js
|
74
|
-
|
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 ๆๅปบๆถ้ด:
|
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
|
-
๐ก ๆๅปบๆถ้ด:
|
92
|
+
๐ข ็ๆฌๅท: 1.2.3 (็ปฟ่ฒ)
|
93
|
+
๐ก ๆๅปบๆถ้ด: 2024-03-01T12:00:00.000Z (้ป่ฒ)
|
109
94
|
```
|
110
95
|
|
111
96
|
---
|
112
97
|
|
113
|
-
##
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
| `
|
118
|
-
| `
|
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
|
-
|
134
|
-
|
135
|
-
|
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
|
-
|
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
|
-
๐ฅ
|
135
|
+
๐ฅ **`unplugin-version-injector` - ่ฎฉไฝ ็ๅบ็จ็ๆฌ็ฎก็ๆด็ฎๅ๏ผ** ๐๐๐
|
@@ -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,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,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;
|