unplugin-version-injector 1.1.2 โ 2.0.0-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 +98 -74
- package/README.zh-CN.md +59 -68
- package/dist/rollup.js +74 -0
- package/dist/rollup.mjs +67 -0
- package/dist/vite.d.mts +5 -0
- package/dist/vite.d.ts +5 -0
- package/dist/vite.js +66 -0
- package/dist/vite.mjs +59 -0
- package/dist/webpack.d.mts +7 -0
- package/dist/webpack.d.ts +7 -0
- package/dist/webpack.js +83 -0
- package/dist/webpack.mjs +76 -0
- package/package.json +50 -23
- package/dist/cjs/index.d.ts +0 -3
- package/dist/cjs/index.js +0 -60
- package/dist/cjs/types.d.ts +0 -5
- package/dist/cjs/types.js +0 -2
- package/dist/cjs/utils.d.ts +0 -4
- package/dist/cjs/utils.js +0 -27
- package/dist/esm/index.d.ts +0 -3
- package/dist/esm/index.js +0 -58
- package/dist/esm/types.d.ts +0 -5
- package/dist/esm/types.js +0 -1
- package/dist/esm/utils.d.ts +0 -4
- package/dist/esm/utils.js +0 -19
package/README.md
CHANGED
@@ -1,134 +1,158 @@
|
|
1
|
-
|
1
|
+
# ๐ `unplugin-version-injector` โ Auto Inject Version & Build Time
|
2
2
|
|
3
|
-
[๐จ๐ณ
|
3
|
+
[๐จ๐ณ ไธญๆๆๆกฃ](./README.zh-CN.md) | [๐บ๐ธ English README](./README.md)
|
4
4
|
|
5
5
|
---
|
6
6
|
|
7
|
-
##
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
โ
|
15
|
-
โ
|
9
|
+
**`unplugin-version-injector`** is a lightweight plugin that automatically injects **version number** and **build timestamp** into all your HTML files.
|
10
|
+
|
11
|
+
### โจ Features
|
12
|
+
|
13
|
+
- โ
Auto-injects `<meta name="version">` into `<head>`
|
14
|
+
- โ
Auto-injects `<script>` for version & build log
|
15
|
+
- โ
Supports **Vite**, **Webpack 4/5**, and **Rollup**
|
16
|
+
- โ
Works with **SPA / MPA**
|
17
|
+
- โ
Fully configurable and tree-shakable
|
16
18
|
|
17
19
|
---
|
18
20
|
|
19
|
-
##
|
20
|
-
```sh
|
21
|
-
# Using Yarn
|
22
|
-
yarn add -D unplugin-version-injector
|
21
|
+
## ๐ฆ Installation
|
23
22
|
|
24
|
-
|
23
|
+
```bash
|
24
|
+
# npm
|
25
25
|
npm install -D unplugin-version-injector
|
26
|
+
|
27
|
+
# yarn
|
28
|
+
yarn add -D unplugin-version-injector
|
29
|
+
|
30
|
+
# pnpm
|
31
|
+
pnpm add -D unplugin-version-injector
|
26
32
|
```
|
27
33
|
|
28
34
|
---
|
29
35
|
|
30
|
-
##
|
36
|
+
## ๐ Usage
|
31
37
|
|
32
|
-
###
|
33
|
-
Modify your `webpack.config.js`:
|
34
|
-
```js
|
35
|
-
const versionInjectorPlugin = require('unplugin-version-injector');
|
38
|
+
### โถ Vite
|
36
39
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
],
|
43
|
-
}
|
40
|
+
```ts
|
41
|
+
// vite.config.ts
|
42
|
+
import versionInjector from 'unplugin-version-injector/vite'
|
43
|
+
|
44
|
+
export default {
|
45
|
+
plugins: [versionInjector()],
|
46
|
+
}
|
44
47
|
```
|
45
48
|
|
46
49
|
---
|
47
50
|
|
48
|
-
###
|
49
|
-
|
51
|
+
### โถ Webpack (4 or 5)
|
52
|
+
|
50
53
|
```js
|
51
|
-
|
54
|
+
// webpack.config.js
|
55
|
+
const versionInjector = require('unplugin-version-injector/webpack')
|
52
56
|
|
53
|
-
|
54
|
-
plugins: [
|
55
|
-
|
57
|
+
module.exports = {
|
58
|
+
plugins: [
|
59
|
+
versionInjector({
|
60
|
+
version: '1.2.3', // Optional custom version
|
61
|
+
}),
|
62
|
+
],
|
63
|
+
}
|
56
64
|
```
|
57
65
|
|
58
66
|
---
|
59
67
|
|
60
|
-
###
|
61
|
-
|
68
|
+
### โถ Rollup
|
69
|
+
|
62
70
|
```js
|
63
|
-
|
71
|
+
// rollup.config.js
|
72
|
+
import versionInjector from 'unplugin-version-injector/rollup'
|
64
73
|
|
65
74
|
export default {
|
66
|
-
plugins: [
|
67
|
-
}
|
75
|
+
plugins: [versionInjector()],
|
76
|
+
}
|
68
77
|
```
|
69
78
|
|
70
79
|
---
|
71
80
|
|
72
|
-
##
|
73
|
-
|
81
|
+
## ๐ Output Example
|
82
|
+
|
83
|
+
### Injected into HTML
|
84
|
+
|
74
85
|
```html
|
75
86
|
<head>
|
76
87
|
<meta name="version" content="1.2.3">
|
77
|
-
<meta charset="UTF-8">
|
78
|
-
<title>My App</title>
|
79
88
|
</head>
|
80
89
|
<body>
|
81
|
-
|
82
|
-
<script>
|
83
|
-
console.log("%c Version: 1.2.3 ", "background
|
84
|
-
console.log("%c Build Time: 2024-03-01T12:00:
|
90
|
+
...
|
91
|
+
<script data-injected="unplugin-version-injector">
|
92
|
+
console.log("%c Version: 1.2.3 ", "background:#222; color:#00ff00;");
|
93
|
+
console.log("%c Build Time: 2024-03-01T12:00:00Z ", "background:#222; color:#ffcc00;");
|
85
94
|
</script>
|
86
95
|
</body>
|
87
96
|
```
|
88
97
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
98
|
+
---
|
99
|
+
|
100
|
+
## ๐ง Options
|
101
|
+
|
102
|
+
| Option | Type | Description | Default |
|
103
|
+
|--------------|-----------|------------------------------------------|--------------------------|
|
104
|
+
| `version` | `string` | Custom version string | From `package.json` |
|
105
|
+
| `log` | `boolean` | Whether to inject `<script>` console log | `true` |
|
106
|
+
| `dateFormat` | `string` | Use `dayjs` format string (if installed) | ISO timestamp string |
|
107
|
+
| `formatDate` | `fn` | Custom function for formatting date | `date.toISOString()` |
|
108
|
+
|
109
|
+
```ts
|
110
|
+
versionInjector({
|
111
|
+
version: '2.0.0',
|
112
|
+
log: false,
|
113
|
+
dateFormat: 'YYYY-MM-DD HH:mm:ss', // requires dayjs
|
114
|
+
})
|
93
115
|
```
|
94
116
|
|
95
117
|
---
|
96
118
|
|
97
|
-
##
|
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` |
|
119
|
+
## ๐ก Why Use This?
|
103
120
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
log: false, // Disable console logs
|
109
|
-
});
|
110
|
-
```
|
121
|
+
- ๐ Track build info in production
|
122
|
+
- ๐ Know what you deployed & when
|
123
|
+
- โ
No manual version updates
|
124
|
+
- ๐ Great for debugging and multi-page apps
|
111
125
|
|
112
126
|
---
|
113
127
|
|
114
|
-
##
|
115
|
-
|
116
|
-
|
117
|
-
- ๐
**Track build times**: Useful for monitoring deployments
|
118
|
-
- ๐ฏ **Lightweight & fast**: Minimal overhead with maximum benefits
|
128
|
+
## ๐ License
|
129
|
+
|
130
|
+
MIT ยฉ [@nianyi778](https://github.com/nianyi778)
|
119
131
|
|
120
132
|
---
|
121
133
|
|
122
|
-
##
|
123
|
-
|
134
|
+
## ๐ Project Structure
|
135
|
+
|
136
|
+
```
|
137
|
+
unplugin-version-injector/
|
138
|
+
โโโ dist/
|
139
|
+
โโโ src/
|
140
|
+
โ โโโ vite.ts
|
141
|
+
โ โโโ webpack.ts
|
142
|
+
โ โโโ rollup.ts
|
143
|
+
โ โโโ shared/
|
144
|
+
โโโ tsup.config.ts
|
145
|
+
โโโ package.json
|
146
|
+
```
|
124
147
|
|
125
148
|
---
|
126
149
|
|
127
|
-
##
|
128
|
-
|
150
|
+
## ๐ค Contributing
|
151
|
+
|
152
|
+
Feel free to submit issues or PRs ๐
|
129
153
|
|
130
|
-
|
154
|
+
GitHub โ https://github.com/nianyi778/unplugin-version-injector
|
131
155
|
|
132
156
|
---
|
133
157
|
|
134
|
-
๐ฅ
|
158
|
+
๐ฅ Simple, reliable version & build timestamp injection โ that just works!
|
package/README.zh-CN.md
CHANGED
@@ -1,25 +1,26 @@
|
|
1
|
-
|
1
|
+
### **๐ `unplugin-version-injector` - ่ชๅจๆณจๅ
ฅ็ๆฌๅทไธๆๅปบๆถ้ด**
|
2
2
|
|
3
|
-
[๐ฌ๐ง English README](./README.md) | [๐จ๐ณ
|
3
|
+
[๐ฌ๐ง English README](./README.md) | [๐จ๐ณ ไธญๆๆๆกฃ](./README.zh-CN.md)
|
4
4
|
|
5
5
|
---
|
6
6
|
|
7
|
-
## **๐
|
8
|
-
`unplugin-version-injector`
|
9
|
-
ๆฏๆ **Webpack 4/5ใVite ๅ Rollup**๏ผ้็จไบ **ๅ้กตๅบ็จ (SPA) ๅ ๅค้กตๅบ็จ (MPA)**ใ
|
7
|
+
## **๐ ๆไปถ็ฎไป**
|
8
|
+
`unplugin-version-injector` ๆฏไธไธช่ฝป้็บงๆไปถ๏ผๅฏๅจๆๅปบๆถ่ชๅจๅๆๆ HTML ๆไปถๆณจๅ
ฅ **็ๆฌๅท** ๅ **ๆๅปบๆถ้ดๆณ**ใๆฏๆ **Webpack 4/5ใVite ๅ Rollup**๏ผ้ๅธธ้ๅ **SPA / MPA ้กน็ฎ**ไฝฟ็จใ
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
โ
|
15
|
-
โ
|
16
|
-
โ
|
10
|
+
---
|
11
|
+
|
12
|
+
## **โจ ๅ่ฝไบฎ็น**
|
13
|
+
โ
่ชๅจๆณจๅ
ฅ `<meta name="version">` ๅฐ HTML ็ `<head>` ไธญ
|
14
|
+
โ
่ชๅจๆณจๅ
ฅ `<script>`๏ผๆงๅถๅฐ่พๅบ `็ๆฌๅท` ๅ `ๆๅปบๆถ้ด`
|
15
|
+
โ
ๆฏๆ Webpack 4 / 5ใViteใRollup
|
16
|
+
โ
ๅฎ็พๅ
ผๅฎนๅค้กต้ขๅบ็จ๏ผMPA๏ผ
|
17
|
+
โ
ๆฏๆ่ชๅฎไน็ๆฌๅทใๆถ้ดๆ ผๅผ๏ผ้ป่ฎค่ฏปๅ `package.json`
|
17
18
|
|
18
19
|
---
|
19
20
|
|
20
21
|
## **๐ฆ ๅฎ่ฃ
**
|
21
|
-
```
|
22
|
-
# ไฝฟ็จ
|
22
|
+
```bash
|
23
|
+
# ไฝฟ็จ yarn
|
23
24
|
yarn add -D unplugin-version-injector
|
24
25
|
|
25
26
|
# ไฝฟ็จ npm
|
@@ -30,106 +31,96 @@ npm install -D unplugin-version-injector
|
|
30
31
|
|
31
32
|
## **๐ ไฝฟ็จๆนๆณ**
|
32
33
|
|
33
|
-
### **๐
|
34
|
-
|
35
|
-
```
|
36
|
-
|
34
|
+
### **๐ Vite**
|
35
|
+
`vite.config.ts` ไธญ้
็ฝฎ๏ผ
|
36
|
+
```ts
|
37
|
+
import versionInjector from 'unplugin-version-injector/vite';
|
37
38
|
|
38
|
-
|
39
|
-
plugins: [
|
40
|
-
versionInjectorPlugin.webpack({
|
41
|
-
version: '1.2.3', // ๏ผๅฏ้๏ผๆๅจๆๅฎ็ๆฌๅท
|
42
|
-
})
|
43
|
-
],
|
39
|
+
export default {
|
40
|
+
plugins: [versionInjector()],
|
44
41
|
};
|
45
42
|
```
|
46
43
|
|
47
44
|
---
|
48
45
|
|
49
|
-
### **๐
|
50
|
-
|
46
|
+
### **๐ Webpack 4/5**
|
47
|
+
`webpack.config.js` ไธญ้
็ฝฎ๏ผ
|
51
48
|
```js
|
52
|
-
|
49
|
+
const versionInjector = require('unplugin-version-injector/webpack');
|
53
50
|
|
54
|
-
|
55
|
-
plugins: [
|
51
|
+
module.exports = {
|
52
|
+
plugins: [
|
53
|
+
versionInjector({
|
54
|
+
version: '1.2.3', // ๅฏ้๏ผ่ชๅฎไน็ๆฌๅท
|
55
|
+
}),
|
56
|
+
],
|
56
57
|
};
|
57
58
|
```
|
58
59
|
|
59
60
|
---
|
60
61
|
|
61
62
|
### **๐ Rollup**
|
62
|
-
|
63
|
+
`rollup.config.js` ไธญ้
็ฝฎ๏ผ
|
63
64
|
```js
|
64
|
-
import
|
65
|
+
import versionInjector from 'unplugin-version-injector/rollup';
|
65
66
|
|
66
67
|
export default {
|
67
|
-
plugins: [
|
68
|
+
plugins: [versionInjector()],
|
68
69
|
};
|
69
70
|
```
|
70
71
|
|
71
72
|
---
|
72
73
|
|
73
|
-
##
|
74
|
-
|
74
|
+
## **๐งช ็คบไพ่พๅบ**
|
75
|
+
|
76
|
+
ๆๅปบๅ็ HTML ๆไปถไธญๅฐ่ชๅจๆณจๅ
ฅ๏ผ
|
77
|
+
|
75
78
|
```html
|
76
79
|
<head>
|
77
80
|
<meta name="version" content="1.2.3">
|
78
|
-
|
79
|
-
<title>ๆ็ๅบ็จ</title>
|
81
|
+
...
|
80
82
|
</head>
|
81
83
|
<body>
|
82
|
-
|
84
|
+
...
|
83
85
|
<script>
|
84
|
-
console.log("%c
|
85
|
-
console.log("%c
|
86
|
+
console.log("%c Version: 1.2.3 ", "background: #222; color: #00ff00;");
|
87
|
+
console.log("%c Build Time: 2024-04-01T12:00:00.000Z ", "background: #222; color: #ffcc00;");
|
86
88
|
</script>
|
87
89
|
</body>
|
88
90
|
```
|
89
91
|
|
90
|
-
|
92
|
+
ๆงๅถๅฐ่พๅบ๏ผ
|
93
|
+
|
91
94
|
```
|
92
|
-
๐ข
|
93
|
-
๐ก
|
95
|
+
๐ข Version: 1.2.3
|
96
|
+
๐ก Build Time: 2024-04-01T12:00:00.000Z
|
94
97
|
```
|
95
98
|
|
96
99
|
---
|
97
100
|
|
98
|
-
## **๐ง
|
99
|
-
| **้้กน** | **็ฑปๅ** | **ๆ่ฟฐ** | **้ป่ฎคๅผ** |
|
100
|
-
|---------|--------|---------|---------|
|
101
|
-
| `version` | `string` | ๆๅจๆๅฎ็ๆฌๅท (ๅฆ `1.2.3`) | ่ชๅจ่ฏปๅ `package.json` |
|
102
|
-
| `log` | `boolean` | ๆฏๅฆๅจๆงๅถๅฐๆๅฐ็ๆฌไฟกๆฏ | `true` |
|
103
|
-
| `dateFormat` | `string` | ่ชๅฎไนๆๅปบๆถ้ดๆ ผๅผ | `ISO 8601` |
|
101
|
+
## **๐ง ้
็ฝฎ้กน่ฏดๆ**
|
104
102
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
});
|
111
|
-
```
|
103
|
+
| ้้กน | ็ฑปๅ | ่ฏดๆ | ้ป่ฎคๅผ |
|
104
|
+
|----------------|-----------|-------------------------------|------------------|
|
105
|
+
| `version` | `string` | ๆๅฎ็ๆฌๅท | ่ชๅจ่ฏปๅ package.json |
|
106
|
+
| `log` | `boolean` | ๆฏๅฆ่พๅบๆงๅถๅฐๆฅๅฟ | `true` |
|
107
|
+
| `dateFormat` | `string` | ไฝฟ็จ dayjs ่ชๅฎไนๆถ้ดๆ ผๅผ | `ISO ๆ ผๅผ` |
|
112
108
|
|
113
109
|
---
|
114
110
|
|
115
|
-
## **๐
|
116
|
-
- ๐ **ๅผ็ฎฑๅณ็จ**๏ผๅฎ่ฃ
ๅ็ซๅณ็ๆ๏ผๆ ้้ขๅค้
็ฝฎ
|
117
|
-
- ๐ **ๆๅ่ฐ่ฏๆ็**๏ผ่ฝปๆพๆฅ็ๅฝๅ็ๆฌไฟกๆฏ
|
118
|
-
- ๐
**่ฟฝ่ธชๆๅปบๆถ้ด**๏ผๆนไพฟ็ๆงไธๅ็ๆฌ็ๅๅธๆถ้ด
|
119
|
-
- ๐ฏ **่ฝป้้ซๆ**๏ผๅ ไนไธไผๅฝฑๅๆๅปบ้ๅบฆ
|
111
|
+
## **๐ ไธบไปไน้ๆฉ่ฟไธชๆไปถ๏ผ**
|
120
112
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
113
|
+
- ๐ ้ถ้
็ฝฎ๏ผๅผ็ฎฑๅณ็จ
|
114
|
+
- ๐ ๅฟซ้ๅฎไฝ็ๆฌ้ฎ้ข
|
115
|
+
- ๐
่ท่ธชๆๅปบๆถ้ด
|
116
|
+
- ๐ฏ ๆฏๆๅคๆๅปบๅทฅๅ
ท
|
117
|
+
- ๐งฉ ้ซๅบฆๅฏ้
็ฝฎ๏ผ็ตๆดปๆๆ
|
125
118
|
|
126
119
|
---
|
127
120
|
|
128
|
-
##
|
129
|
-
|
130
|
-
|
131
|
-
**GitHub ไปๅบ**๏ผ[๐ unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
|
121
|
+
## **๐ ๅผๆบ่ฎธๅฏ**
|
122
|
+
MIT License ยฉ 2024 [Nian YI](https://github.com/nianyi778)
|
132
123
|
|
133
124
|
---
|
134
125
|
|
135
|
-
๐ฅ
|
126
|
+
๐ฅ `unplugin-version-injector` โโ ๆ่ฝปๅทง็็ๆฌไฟกๆฏๆณจๅ
ฅ่งฃๅณๆนๆก๏ผ
|
package/dist/rollup.js
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var fs = require('fs');
|
4
|
+
var path = require('path');
|
5
|
+
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
7
|
+
|
8
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
9
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
10
|
+
|
11
|
+
// src/shared/utils.ts
|
12
|
+
function getPackageVersion(startDir) {
|
13
|
+
try {
|
14
|
+
let dir = startDir || process.cwd();
|
15
|
+
while (dir !== path__default.default.parse(dir).root) {
|
16
|
+
const pkgPath = path__default.default.join(dir, "package.json");
|
17
|
+
if (fs__default.default.existsSync(pkgPath)) {
|
18
|
+
const pkg = JSON.parse(fs__default.default.readFileSync(pkgPath, "utf-8"));
|
19
|
+
return pkg.version || "0.0.0";
|
20
|
+
}
|
21
|
+
dir = path__default.default.dirname(dir);
|
22
|
+
}
|
23
|
+
console.warn("[VersionInjector] package.json not found");
|
24
|
+
return "0.0.0";
|
25
|
+
} catch (err) {
|
26
|
+
console.warn("[VersionInjector] Failed to read package.json:", err);
|
27
|
+
return "0.0.0";
|
28
|
+
}
|
29
|
+
}
|
30
|
+
function defaultFormatDate(date) {
|
31
|
+
return date.toISOString();
|
32
|
+
}
|
33
|
+
|
34
|
+
// src/core.ts
|
35
|
+
function createVersionInjector(options = {}) {
|
36
|
+
var _a, _b;
|
37
|
+
const version = (_a = options.version) != null ? _a : getPackageVersion();
|
38
|
+
((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
|
39
|
+
const metaTag = `<meta name="version" content="${version}">
|
40
|
+
`;
|
41
|
+
const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
|
42
|
+
return function processHtml(html) {
|
43
|
+
if (!html.includes('<meta name="version"')) {
|
44
|
+
html = html.replace(/<head>/, `<head>
|
45
|
+
${metaTag}`);
|
46
|
+
}
|
47
|
+
if (!html.includes('<script data-injected="unplugin-version-injector"')) {
|
48
|
+
html = html.replace("</body>", ` ${logScript}
|
49
|
+
</body>`);
|
50
|
+
}
|
51
|
+
return html;
|
52
|
+
};
|
53
|
+
}
|
54
|
+
|
55
|
+
// src/rollup.ts
|
56
|
+
function versionInjector(options = {}) {
|
57
|
+
const shouldInject = options.log !== false;
|
58
|
+
if (!shouldInject) {
|
59
|
+
return { name: "unplugin-version-injector" };
|
60
|
+
}
|
61
|
+
const processHtml = createVersionInjector(options);
|
62
|
+
return {
|
63
|
+
name: "unplugin-version-injector",
|
64
|
+
generateBundle(_, bundle) {
|
65
|
+
for (const file of Object.values(bundle)) {
|
66
|
+
if (file.type === "asset" && file.fileName.endsWith(".html")) {
|
67
|
+
file.source = processHtml(file.source);
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
71
|
+
};
|
72
|
+
}
|
73
|
+
|
74
|
+
module.exports = versionInjector;
|
package/dist/rollup.mjs
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
import fs from 'fs';
|
2
|
+
import path from 'path';
|
3
|
+
|
4
|
+
// src/shared/utils.ts
|
5
|
+
function getPackageVersion(startDir) {
|
6
|
+
try {
|
7
|
+
let dir = startDir || process.cwd();
|
8
|
+
while (dir !== path.parse(dir).root) {
|
9
|
+
const pkgPath = path.join(dir, "package.json");
|
10
|
+
if (fs.existsSync(pkgPath)) {
|
11
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
12
|
+
return pkg.version || "0.0.0";
|
13
|
+
}
|
14
|
+
dir = path.dirname(dir);
|
15
|
+
}
|
16
|
+
console.warn("[VersionInjector] package.json not found");
|
17
|
+
return "0.0.0";
|
18
|
+
} catch (err) {
|
19
|
+
console.warn("[VersionInjector] Failed to read package.json:", err);
|
20
|
+
return "0.0.0";
|
21
|
+
}
|
22
|
+
}
|
23
|
+
function defaultFormatDate(date) {
|
24
|
+
return date.toISOString();
|
25
|
+
}
|
26
|
+
|
27
|
+
// src/core.ts
|
28
|
+
function createVersionInjector(options = {}) {
|
29
|
+
var _a, _b;
|
30
|
+
const version = (_a = options.version) != null ? _a : getPackageVersion();
|
31
|
+
((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
|
32
|
+
const metaTag = `<meta name="version" content="${version}">
|
33
|
+
`;
|
34
|
+
const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
|
35
|
+
return function processHtml(html) {
|
36
|
+
if (!html.includes('<meta name="version"')) {
|
37
|
+
html = html.replace(/<head>/, `<head>
|
38
|
+
${metaTag}`);
|
39
|
+
}
|
40
|
+
if (!html.includes('<script data-injected="unplugin-version-injector"')) {
|
41
|
+
html = html.replace("</body>", ` ${logScript}
|
42
|
+
</body>`);
|
43
|
+
}
|
44
|
+
return html;
|
45
|
+
};
|
46
|
+
}
|
47
|
+
|
48
|
+
// src/rollup.ts
|
49
|
+
function versionInjector(options = {}) {
|
50
|
+
const shouldInject = options.log !== false;
|
51
|
+
if (!shouldInject) {
|
52
|
+
return { name: "unplugin-version-injector" };
|
53
|
+
}
|
54
|
+
const processHtml = createVersionInjector(options);
|
55
|
+
return {
|
56
|
+
name: "unplugin-version-injector",
|
57
|
+
generateBundle(_, bundle) {
|
58
|
+
for (const file of Object.values(bundle)) {
|
59
|
+
if (file.type === "asset" && file.fileName.endsWith(".html")) {
|
60
|
+
file.source = processHtml(file.source);
|
61
|
+
}
|
62
|
+
}
|
63
|
+
}
|
64
|
+
};
|
65
|
+
}
|
66
|
+
|
67
|
+
export { versionInjector as default };
|
package/dist/vite.d.mts
ADDED
package/dist/vite.d.ts
ADDED
package/dist/vite.js
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var fs = require('fs');
|
4
|
+
var path = require('path');
|
5
|
+
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
7
|
+
|
8
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
9
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
10
|
+
|
11
|
+
// src/shared/utils.ts
|
12
|
+
function getPackageVersion(startDir) {
|
13
|
+
try {
|
14
|
+
let dir = startDir || process.cwd();
|
15
|
+
while (dir !== path__default.default.parse(dir).root) {
|
16
|
+
const pkgPath = path__default.default.join(dir, "package.json");
|
17
|
+
if (fs__default.default.existsSync(pkgPath)) {
|
18
|
+
const pkg = JSON.parse(fs__default.default.readFileSync(pkgPath, "utf-8"));
|
19
|
+
return pkg.version || "0.0.0";
|
20
|
+
}
|
21
|
+
dir = path__default.default.dirname(dir);
|
22
|
+
}
|
23
|
+
console.warn("[VersionInjector] package.json not found");
|
24
|
+
return "0.0.0";
|
25
|
+
} catch (err) {
|
26
|
+
console.warn("[VersionInjector] Failed to read package.json:", err);
|
27
|
+
return "0.0.0";
|
28
|
+
}
|
29
|
+
}
|
30
|
+
function defaultFormatDate(date) {
|
31
|
+
return date.toISOString();
|
32
|
+
}
|
33
|
+
|
34
|
+
// src/core.ts
|
35
|
+
function createVersionInjector(options = {}) {
|
36
|
+
var _a, _b;
|
37
|
+
const version = (_a = options.version) != null ? _a : getPackageVersion();
|
38
|
+
((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
|
39
|
+
const metaTag = `<meta name="version" content="${version}">
|
40
|
+
`;
|
41
|
+
const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
|
42
|
+
return function processHtml(html) {
|
43
|
+
if (!html.includes('<meta name="version"')) {
|
44
|
+
html = html.replace(/<head>/, `<head>
|
45
|
+
${metaTag}`);
|
46
|
+
}
|
47
|
+
if (!html.includes('<script data-injected="unplugin-version-injector"')) {
|
48
|
+
html = html.replace("</body>", ` ${logScript}
|
49
|
+
</body>`);
|
50
|
+
}
|
51
|
+
return html;
|
52
|
+
};
|
53
|
+
}
|
54
|
+
|
55
|
+
// src/vite.ts
|
56
|
+
function versionInjectorPlugin(options = {}) {
|
57
|
+
const inject = createVersionInjector(options);
|
58
|
+
return {
|
59
|
+
name: "vite-version-injector",
|
60
|
+
transformIndexHtml(html) {
|
61
|
+
return inject(html);
|
62
|
+
}
|
63
|
+
};
|
64
|
+
}
|
65
|
+
|
66
|
+
module.exports = versionInjectorPlugin;
|
package/dist/vite.mjs
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
import fs from 'fs';
|
2
|
+
import path from 'path';
|
3
|
+
|
4
|
+
// src/shared/utils.ts
|
5
|
+
function getPackageVersion(startDir) {
|
6
|
+
try {
|
7
|
+
let dir = startDir || process.cwd();
|
8
|
+
while (dir !== path.parse(dir).root) {
|
9
|
+
const pkgPath = path.join(dir, "package.json");
|
10
|
+
if (fs.existsSync(pkgPath)) {
|
11
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
12
|
+
return pkg.version || "0.0.0";
|
13
|
+
}
|
14
|
+
dir = path.dirname(dir);
|
15
|
+
}
|
16
|
+
console.warn("[VersionInjector] package.json not found");
|
17
|
+
return "0.0.0";
|
18
|
+
} catch (err) {
|
19
|
+
console.warn("[VersionInjector] Failed to read package.json:", err);
|
20
|
+
return "0.0.0";
|
21
|
+
}
|
22
|
+
}
|
23
|
+
function defaultFormatDate(date) {
|
24
|
+
return date.toISOString();
|
25
|
+
}
|
26
|
+
|
27
|
+
// src/core.ts
|
28
|
+
function createVersionInjector(options = {}) {
|
29
|
+
var _a, _b;
|
30
|
+
const version = (_a = options.version) != null ? _a : getPackageVersion();
|
31
|
+
((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
|
32
|
+
const metaTag = `<meta name="version" content="${version}">
|
33
|
+
`;
|
34
|
+
const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
|
35
|
+
return function processHtml(html) {
|
36
|
+
if (!html.includes('<meta name="version"')) {
|
37
|
+
html = html.replace(/<head>/, `<head>
|
38
|
+
${metaTag}`);
|
39
|
+
}
|
40
|
+
if (!html.includes('<script data-injected="unplugin-version-injector"')) {
|
41
|
+
html = html.replace("</body>", ` ${logScript}
|
42
|
+
</body>`);
|
43
|
+
}
|
44
|
+
return html;
|
45
|
+
};
|
46
|
+
}
|
47
|
+
|
48
|
+
// src/vite.ts
|
49
|
+
function versionInjectorPlugin(options = {}) {
|
50
|
+
const inject = createVersionInjector(options);
|
51
|
+
return {
|
52
|
+
name: "vite-version-injector",
|
53
|
+
transformIndexHtml(html) {
|
54
|
+
return inject(html);
|
55
|
+
}
|
56
|
+
};
|
57
|
+
}
|
58
|
+
|
59
|
+
export { versionInjectorPlugin as default };
|
package/dist/webpack.js
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var fs = require('fs');
|
4
|
+
var path = require('path');
|
5
|
+
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
7
|
+
|
8
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
9
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
10
|
+
|
11
|
+
// src/shared/utils.ts
|
12
|
+
function getPackageVersion(startDir) {
|
13
|
+
try {
|
14
|
+
let dir = startDir || process.cwd();
|
15
|
+
while (dir !== path__default.default.parse(dir).root) {
|
16
|
+
const pkgPath = path__default.default.join(dir, "package.json");
|
17
|
+
if (fs__default.default.existsSync(pkgPath)) {
|
18
|
+
const pkg = JSON.parse(fs__default.default.readFileSync(pkgPath, "utf-8"));
|
19
|
+
return pkg.version || "0.0.0";
|
20
|
+
}
|
21
|
+
dir = path__default.default.dirname(dir);
|
22
|
+
}
|
23
|
+
console.warn("[VersionInjector] package.json not found");
|
24
|
+
return "0.0.0";
|
25
|
+
} catch (err) {
|
26
|
+
console.warn("[VersionInjector] Failed to read package.json:", err);
|
27
|
+
return "0.0.0";
|
28
|
+
}
|
29
|
+
}
|
30
|
+
function defaultFormatDate(date) {
|
31
|
+
return date.toISOString();
|
32
|
+
}
|
33
|
+
|
34
|
+
// src/core.ts
|
35
|
+
function createVersionInjector(options = {}) {
|
36
|
+
var _a, _b;
|
37
|
+
const version = (_a = options.version) != null ? _a : getPackageVersion();
|
38
|
+
((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
|
39
|
+
const metaTag = `<meta name="version" content="${version}">
|
40
|
+
`;
|
41
|
+
const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
|
42
|
+
return function processHtml(html) {
|
43
|
+
if (!html.includes('<meta name="version"')) {
|
44
|
+
html = html.replace(/<head>/, `<head>
|
45
|
+
${metaTag}`);
|
46
|
+
}
|
47
|
+
if (!html.includes('<script data-injected="unplugin-version-injector"')) {
|
48
|
+
html = html.replace("</body>", ` ${logScript}
|
49
|
+
</body>`);
|
50
|
+
}
|
51
|
+
return html;
|
52
|
+
};
|
53
|
+
}
|
54
|
+
|
55
|
+
// src/webpack.ts
|
56
|
+
function versionInjectorPlugin(options = {}) {
|
57
|
+
const inject = createVersionInjector(options);
|
58
|
+
return {
|
59
|
+
apply(compiler) {
|
60
|
+
var _a, _b;
|
61
|
+
const isWebpack5 = (_b = (_a = compiler.webpack) == null ? void 0 : _a.version) == null ? void 0 : _b.startsWith("5");
|
62
|
+
compiler.hooks.compilation.tap("webpack-version-injector", (compilation) => {
|
63
|
+
if (isWebpack5) {
|
64
|
+
const { Compilation, sources } = compiler.webpack;
|
65
|
+
compilation.hooks.processAssets.tap(
|
66
|
+
{ name: "webpack-version-injector", stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE },
|
67
|
+
(assets) => {
|
68
|
+
for (const name in assets) {
|
69
|
+
if (name.endsWith(".html")) {
|
70
|
+
const html = assets[name].source().toString();
|
71
|
+
const result = inject(html);
|
72
|
+
compilation.updateAsset(name, new sources.RawSource(result));
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}
|
76
|
+
);
|
77
|
+
}
|
78
|
+
});
|
79
|
+
}
|
80
|
+
};
|
81
|
+
}
|
82
|
+
|
83
|
+
module.exports = versionInjectorPlugin;
|
package/dist/webpack.mjs
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
import fs from 'fs';
|
2
|
+
import path from 'path';
|
3
|
+
|
4
|
+
// src/shared/utils.ts
|
5
|
+
function getPackageVersion(startDir) {
|
6
|
+
try {
|
7
|
+
let dir = startDir || process.cwd();
|
8
|
+
while (dir !== path.parse(dir).root) {
|
9
|
+
const pkgPath = path.join(dir, "package.json");
|
10
|
+
if (fs.existsSync(pkgPath)) {
|
11
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
12
|
+
return pkg.version || "0.0.0";
|
13
|
+
}
|
14
|
+
dir = path.dirname(dir);
|
15
|
+
}
|
16
|
+
console.warn("[VersionInjector] package.json not found");
|
17
|
+
return "0.0.0";
|
18
|
+
} catch (err) {
|
19
|
+
console.warn("[VersionInjector] Failed to read package.json:", err);
|
20
|
+
return "0.0.0";
|
21
|
+
}
|
22
|
+
}
|
23
|
+
function defaultFormatDate(date) {
|
24
|
+
return date.toISOString();
|
25
|
+
}
|
26
|
+
|
27
|
+
// src/core.ts
|
28
|
+
function createVersionInjector(options = {}) {
|
29
|
+
var _a, _b;
|
30
|
+
const version = (_a = options.version) != null ? _a : getPackageVersion();
|
31
|
+
((_b = options.formatDate) != null ? _b : defaultFormatDate)(/* @__PURE__ */ new Date());
|
32
|
+
const metaTag = `<meta name="version" content="${version}">
|
33
|
+
`;
|
34
|
+
const logScript = `<script data-injected="unplugin-version-injector">console.log(...)</script>`;
|
35
|
+
return function processHtml(html) {
|
36
|
+
if (!html.includes('<meta name="version"')) {
|
37
|
+
html = html.replace(/<head>/, `<head>
|
38
|
+
${metaTag}`);
|
39
|
+
}
|
40
|
+
if (!html.includes('<script data-injected="unplugin-version-injector"')) {
|
41
|
+
html = html.replace("</body>", ` ${logScript}
|
42
|
+
</body>`);
|
43
|
+
}
|
44
|
+
return html;
|
45
|
+
};
|
46
|
+
}
|
47
|
+
|
48
|
+
// src/webpack.ts
|
49
|
+
function versionInjectorPlugin(options = {}) {
|
50
|
+
const inject = createVersionInjector(options);
|
51
|
+
return {
|
52
|
+
apply(compiler) {
|
53
|
+
var _a, _b;
|
54
|
+
const isWebpack5 = (_b = (_a = compiler.webpack) == null ? void 0 : _a.version) == null ? void 0 : _b.startsWith("5");
|
55
|
+
compiler.hooks.compilation.tap("webpack-version-injector", (compilation) => {
|
56
|
+
if (isWebpack5) {
|
57
|
+
const { Compilation, sources } = compiler.webpack;
|
58
|
+
compilation.hooks.processAssets.tap(
|
59
|
+
{ name: "webpack-version-injector", stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE },
|
60
|
+
(assets) => {
|
61
|
+
for (const name in assets) {
|
62
|
+
if (name.endsWith(".html")) {
|
63
|
+
const html = assets[name].source().toString();
|
64
|
+
const result = inject(html);
|
65
|
+
compilation.updateAsset(name, new sources.RawSource(result));
|
66
|
+
}
|
67
|
+
}
|
68
|
+
}
|
69
|
+
);
|
70
|
+
}
|
71
|
+
});
|
72
|
+
}
|
73
|
+
};
|
74
|
+
}
|
75
|
+
|
76
|
+
export { versionInjectorPlugin as default };
|
package/package.json
CHANGED
@@ -1,41 +1,68 @@
|
|
1
1
|
{
|
2
2
|
"name": "unplugin-version-injector",
|
3
|
-
"version": "
|
3
|
+
"version": "2.0.0-beta.1",
|
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)",
|
7
7
|
"repository": {
|
8
|
-
|
9
|
-
|
8
|
+
"type": "git",
|
9
|
+
"url": "https://github.com/nianyi778/unplugin-version-injector.git"
|
10
|
+
},
|
11
|
+
"keywords": [
|
12
|
+
"version",
|
13
|
+
"injector",
|
14
|
+
"webpack",
|
15
|
+
"vite",
|
16
|
+
"rollup"
|
17
|
+
],
|
18
|
+
"typesVersions": {
|
19
|
+
"*": {
|
20
|
+
"vite": [
|
21
|
+
"./dist/vite.d.ts"
|
22
|
+
],
|
23
|
+
"webpack": [
|
24
|
+
"./dist/webpack.d.ts"
|
25
|
+
],
|
26
|
+
"rollup": [
|
27
|
+
"./dist/rollup.d.ts"
|
28
|
+
]
|
29
|
+
}
|
10
30
|
},
|
11
|
-
"keywords": ["unplugin", "version", "injector", "webpack", "vite", "rollup"],
|
12
|
-
"main": "./dist/cjs/index.js",
|
13
|
-
"module": "./dist/esm/index.js",
|
14
31
|
"exports": {
|
15
|
-
|
16
|
-
"import": "./dist/
|
17
|
-
"
|
32
|
+
"./vite": {
|
33
|
+
"import": "./dist/vite.mjs",
|
34
|
+
"require": "./dist/vite.js"
|
35
|
+
},
|
36
|
+
"./webpack": {
|
37
|
+
"import": "./dist/webpack.mjs",
|
38
|
+
"require": "./dist/webpack.js"
|
39
|
+
},
|
40
|
+
"./rollup": {
|
41
|
+
"import": "./dist/rollup.mjs",
|
42
|
+
"require": "./dist/rollup.js"
|
43
|
+
}
|
18
44
|
},
|
19
|
-
"types": "./dist/index.d.ts",
|
20
45
|
"scripts": {
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
"build": "npm run clean && npm run build:esm && npm run build:cjs",
|
25
|
-
"prepublishOnly": "npm run build"
|
46
|
+
"clean": "rm -rf dist",
|
47
|
+
"build": "tsup",
|
48
|
+
"prepublishOnly": "npm run build"
|
26
49
|
},
|
27
50
|
"dependencies": {
|
28
|
-
|
51
|
+
"tsup": "^8.4.0"
|
29
52
|
},
|
30
53
|
"devDependencies": {
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
54
|
+
"typescript": "^4.9.5",
|
55
|
+
"webpack": "^5",
|
56
|
+
"vite": "^4",
|
57
|
+
"rollup": "^3"
|
35
58
|
},
|
36
59
|
"publishConfig": {
|
37
|
-
|
60
|
+
"access": "public"
|
38
61
|
},
|
39
|
-
"files": [
|
62
|
+
"files": [
|
63
|
+
"dist",
|
64
|
+
"README.md",
|
65
|
+
"README.zh-CN.md"
|
66
|
+
],
|
40
67
|
"packageManager": "pnpm@10.5.2+sha512.da9dc28cd3ff40d0592188235ab25d3202add8a207afbedc682220e4a0029ffbff4562102b9e6e46b4e3f9e8bd53e6d05de48544b0c57d4b0179e22c76d1199b"
|
41
|
-
}
|
68
|
+
}
|
package/dist/cjs/index.d.ts
DELETED
package/dist/cjs/index.js
DELETED
@@ -1,60 +0,0 @@
|
|
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;
|
package/dist/cjs/types.d.ts
DELETED
package/dist/cjs/types.js
DELETED
package/dist/cjs/utils.d.ts
DELETED
package/dist/cjs/utils.js
DELETED
@@ -1,27 +0,0 @@
|
|
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;
|
package/dist/esm/index.d.ts
DELETED
package/dist/esm/index.js
DELETED
@@ -1,58 +0,0 @@
|
|
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;
|
package/dist/esm/types.d.ts
DELETED
package/dist/esm/types.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
package/dist/esm/utils.d.ts
DELETED
package/dist/esm/utils.js
DELETED
@@ -1,19 +0,0 @@
|
|
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
|
-
}
|