unplugin-version-injector 1.0.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 +134 -0
- package/README.zh-CN.md +135 -0
- package/package.json +40 -0
- package/src/index.ts +77 -0
- package/src/types.ts +6 -0
- package/src/utils.ts +20 -0
- package/tsconfig.json +15 -0
package/README.md
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
### **๐ `unplugin-version-injector` - Auto Inject Version & Build Time**
|
2
|
+
|
3
|
+
[๐จ๐ณ ไธญๆ README](./README.zh-CN.md) | [๐ฌ๐ง English README](./README.md)
|
4
|
+
|
5
|
+
---
|
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)**.
|
9
|
+
|
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`
|
16
|
+
|
17
|
+
---
|
18
|
+
|
19
|
+
## **๐ฆ Installation**
|
20
|
+
```sh
|
21
|
+
# Using Yarn
|
22
|
+
yarn add -D unplugin-version-injector
|
23
|
+
|
24
|
+
# Using npm
|
25
|
+
npm install -D unplugin-version-injector
|
26
|
+
```
|
27
|
+
|
28
|
+
---
|
29
|
+
|
30
|
+
## **๐ Usage**
|
31
|
+
|
32
|
+
### **๐ Webpack 4/5**
|
33
|
+
Modify your `webpack.config.js`:
|
34
|
+
```js
|
35
|
+
const versionInjectorPlugin = require('unplugin-version-injector');
|
36
|
+
|
37
|
+
module.exports = {
|
38
|
+
plugins: [
|
39
|
+
versionInjectorPlugin.webpack({
|
40
|
+
version: '1.2.3', // (Optional) Manually specify version
|
41
|
+
})
|
42
|
+
],
|
43
|
+
};
|
44
|
+
```
|
45
|
+
|
46
|
+
---
|
47
|
+
|
48
|
+
### **๐ Vite**
|
49
|
+
Modify your `vite.config.js`:
|
50
|
+
```js
|
51
|
+
import versionInjectorPlugin from 'unplugin-version-injector';
|
52
|
+
|
53
|
+
export default {
|
54
|
+
plugins: [versionInjectorPlugin.vite()]
|
55
|
+
};
|
56
|
+
```
|
57
|
+
|
58
|
+
---
|
59
|
+
|
60
|
+
### **๐ Rollup**
|
61
|
+
Modify your `rollup.config.js`:
|
62
|
+
```js
|
63
|
+
import versionInjectorPlugin from 'unplugin-version-injector';
|
64
|
+
|
65
|
+
export default {
|
66
|
+
plugins: [versionInjectorPlugin.rollup()]
|
67
|
+
};
|
68
|
+
```
|
69
|
+
|
70
|
+
---
|
71
|
+
|
72
|
+
## **๐ Example Output**
|
73
|
+
After building, all HTML files will include the following:
|
74
|
+
```html
|
75
|
+
<head>
|
76
|
+
<meta name="version" content="1.2.3">
|
77
|
+
<meta charset="UTF-8">
|
78
|
+
<title>My App</title>
|
79
|
+
</head>
|
80
|
+
<body>
|
81
|
+
<h1>Hello World</h1>
|
82
|
+
<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;");
|
85
|
+
</script>
|
86
|
+
</body>
|
87
|
+
```
|
88
|
+
|
89
|
+
โ
**Console Output (Colored Logs)**
|
90
|
+
```
|
91
|
+
๐ข Version: 1.2.3 (Green)
|
92
|
+
๐ก Build Time: 2024-03-01T12:00:00.000Z (Yellow)
|
93
|
+
```
|
94
|
+
|
95
|
+
---
|
96
|
+
|
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` |
|
103
|
+
|
104
|
+
### **Example: Custom Config**
|
105
|
+
```js
|
106
|
+
versionInjectorPlugin.webpack({
|
107
|
+
version: '2.0.0',
|
108
|
+
log: false, // Disable console logs
|
109
|
+
});
|
110
|
+
```
|
111
|
+
|
112
|
+
---
|
113
|
+
|
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
|
119
|
+
|
120
|
+
---
|
121
|
+
|
122
|
+
## **๐ License**
|
123
|
+
MIT License ยฉ 2024 [Nian YI](https://github.com/nianyi778)
|
124
|
+
|
125
|
+
---
|
126
|
+
|
127
|
+
## **๐ก Contributing**
|
128
|
+
Pull requests are welcome! If you encounter any issues, feel free to open an issue on GitHub.
|
129
|
+
|
130
|
+
**GitHub Repository:** [๐ unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
|
131
|
+
|
132
|
+
---
|
133
|
+
|
134
|
+
๐ฅ **`unplugin-version-injector` โ The simplest way to keep track of your app's version & build time!** ๐
|
package/README.zh-CN.md
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
# **๐ `unplugin-version-injector` - ่ชๅจๆณจๅ
ฅ็ๆฌๅท & ๆๅปบๆถ้ด**
|
2
|
+
|
3
|
+
[๐ฌ๐ง English README](./README.md) | [๐จ๐ณ ไธญๆ README](./README.zh-CN.md)
|
4
|
+
|
5
|
+
---
|
6
|
+
|
7
|
+
## **๐ ็ฎไป**
|
8
|
+
`unplugin-version-injector` ๆฏไธไธช **่ฝป้็บง** ๆไปถ๏ผๅฏ่ชๅจๅฐ **็ๆฌๅท** ๅ **ๆๅปบๆถ้ด** ๆณจๅ
ฅๅฐๆๆ HTML ๆไปถไธญใ
|
9
|
+
ๆฏๆ **Webpack 4/5ใVite ๅ Rollup**๏ผ้็จไบ **ๅ้กตๅบ็จ (SPA) ๅ ๅค้กตๅบ็จ (MPA)**ใ
|
10
|
+
|
11
|
+
### **โจ ๅ่ฝ็น็น**
|
12
|
+
โ
**่ชๅจๆณจๅ
ฅ** `<meta name="version">` ๅฐๆๆ HTML `<head>` ้จๅ
|
13
|
+
โ
**่ชๅจๆณจๅ
ฅ `<script>`**๏ผๅจๆต่งๅจๆงๅถๅฐๆๅฐ `็ๆฌๅท` & `ๆๅปบๆถ้ด`
|
14
|
+
โ
**ๅ
ผๅฎน Webpack 4 & 5ใVite ๅ Rollup**
|
15
|
+
โ
**ๆฏๆๅค้กตๅบ็จ (MPA)**๏ผไธไผ้ๆผไปปไฝ HTML
|
16
|
+
โ
**ๆฏๆๆๅจๆๅฎ็ๆฌๅท**๏ผ้ป่ฎค่ฏปๅ `package.json`
|
17
|
+
|
18
|
+
---
|
19
|
+
|
20
|
+
## **๐ฆ ๅฎ่ฃ
**
|
21
|
+
```sh
|
22
|
+
# ไฝฟ็จ Yarn
|
23
|
+
yarn add -D unplugin-version-injector
|
24
|
+
|
25
|
+
# ไฝฟ็จ npm
|
26
|
+
npm install -D unplugin-version-injector
|
27
|
+
```
|
28
|
+
|
29
|
+
---
|
30
|
+
|
31
|
+
## **๐ ไฝฟ็จๆนๆณ**
|
32
|
+
|
33
|
+
### **๐ Webpack 4/5**
|
34
|
+
ไฟฎๆน `webpack.config.js`๏ผ
|
35
|
+
```js
|
36
|
+
const versionInjectorPlugin = require('unplugin-version-injector');
|
37
|
+
|
38
|
+
module.exports = {
|
39
|
+
plugins: [
|
40
|
+
versionInjectorPlugin.webpack({
|
41
|
+
version: '1.2.3', // ๏ผๅฏ้๏ผๆๅจๆๅฎ็ๆฌๅท
|
42
|
+
})
|
43
|
+
],
|
44
|
+
};
|
45
|
+
```
|
46
|
+
|
47
|
+
---
|
48
|
+
|
49
|
+
### **๐ Vite**
|
50
|
+
ไฟฎๆน `vite.config.js`๏ผ
|
51
|
+
```js
|
52
|
+
import versionInjectorPlugin from 'unplugin-version-injector';
|
53
|
+
|
54
|
+
export default {
|
55
|
+
plugins: [versionInjectorPlugin.vite()]
|
56
|
+
};
|
57
|
+
```
|
58
|
+
|
59
|
+
---
|
60
|
+
|
61
|
+
### **๐ Rollup**
|
62
|
+
ไฟฎๆน `rollup.config.js`๏ผ
|
63
|
+
```js
|
64
|
+
import versionInjectorPlugin from 'unplugin-version-injector';
|
65
|
+
|
66
|
+
export default {
|
67
|
+
plugins: [versionInjectorPlugin.rollup()]
|
68
|
+
};
|
69
|
+
```
|
70
|
+
|
71
|
+
---
|
72
|
+
|
73
|
+
## **๐ ็ๆ็ HTML ็คบไพ**
|
74
|
+
ๆๅปบๅฎๆๅ๏ผๆๆ HTML ๆไปถๅฐๅ
ๅซไปฅไธๅ
ๅฎน๏ผ
|
75
|
+
```html
|
76
|
+
<head>
|
77
|
+
<meta name="version" content="1.2.3">
|
78
|
+
<meta charset="UTF-8">
|
79
|
+
<title>ๆ็ๅบ็จ</title>
|
80
|
+
</head>
|
81
|
+
<body>
|
82
|
+
<h1>Hello World</h1>
|
83
|
+
<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;");
|
86
|
+
</script>
|
87
|
+
</body>
|
88
|
+
```
|
89
|
+
|
90
|
+
โ
**ๆต่งๅจๆงๅถๅฐ่พๅบ (ๅธฆ้ข่ฒๆฅๅฟ)**
|
91
|
+
```
|
92
|
+
๐ข ็ๆฌๅท: 1.2.3 (็ปฟ่ฒ)
|
93
|
+
๐ก ๆๅปบๆถ้ด: 2024-03-01T12:00:00.000Z (้ป่ฒ)
|
94
|
+
```
|
95
|
+
|
96
|
+
---
|
97
|
+
|
98
|
+
## **๐ง ้
็ฝฎ้้กน**
|
99
|
+
| **้้กน** | **็ฑปๅ** | **ๆ่ฟฐ** | **้ป่ฎคๅผ** |
|
100
|
+
|---------|--------|---------|---------|
|
101
|
+
| `version` | `string` | ๆๅจๆๅฎ็ๆฌๅท (ๅฆ `1.2.3`) | ่ชๅจ่ฏปๅ `package.json` |
|
102
|
+
| `log` | `boolean` | ๆฏๅฆๅจๆงๅถๅฐๆๅฐ็ๆฌไฟกๆฏ | `true` |
|
103
|
+
| `dateFormat` | `string` | ่ชๅฎไนๆๅปบๆถ้ดๆ ผๅผ | `ISO 8601` |
|
104
|
+
|
105
|
+
### **๐ ่ชๅฎไน้
็ฝฎ็คบไพ**
|
106
|
+
```js
|
107
|
+
versionInjectorPlugin.webpack({
|
108
|
+
version: '2.0.0',
|
109
|
+
log: false, // ๅ
ณ้ญๆงๅถๅฐๆฅๅฟ
|
110
|
+
});
|
111
|
+
```
|
112
|
+
|
113
|
+
---
|
114
|
+
|
115
|
+
## **๐ ไธบไปไน้ๆฉ `unplugin-version-injector`๏ผ**
|
116
|
+
- ๐ **ๅผ็ฎฑๅณ็จ**๏ผๅฎ่ฃ
ๅ็ซๅณ็ๆ๏ผๆ ้้ขๅค้
็ฝฎ
|
117
|
+
- ๐ **ๆๅ่ฐ่ฏๆ็**๏ผ่ฝปๆพๆฅ็ๅฝๅ็ๆฌไฟกๆฏ
|
118
|
+
- ๐
**่ฟฝ่ธชๆๅปบๆถ้ด**๏ผๆนไพฟ็ๆงไธๅ็ๆฌ็ๅๅธๆถ้ด
|
119
|
+
- ๐ฏ **่ฝป้้ซๆ**๏ผๅ ไนไธไผๅฝฑๅๆๅปบ้ๅบฆ
|
120
|
+
|
121
|
+
---
|
122
|
+
|
123
|
+
## **๐ ่ฎธๅฏ่ฏ**
|
124
|
+
MIT License ยฉ 2024 [Nian YI](https://github.com/nianyi778)
|
125
|
+
|
126
|
+
---
|
127
|
+
|
128
|
+
## **๐ก ่ดก็ฎ**
|
129
|
+
ๆฌข่ฟ PR๏ผๅฆๆ้ฎ้ข๏ผๆฌข่ฟๅจ GitHub ๆไบค issueใ
|
130
|
+
|
131
|
+
**GitHub ไปๅบ**๏ผ[๐ unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
|
132
|
+
|
133
|
+
---
|
134
|
+
|
135
|
+
๐ฅ **`unplugin-version-injector` - ่ฎฉไฝ ็ๅบ็จ็ๆฌ็ฎก็ๆด็ฎๅ๏ผ** ๐๐๐
|
package/package.json
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
{
|
2
|
+
"name": "unplugin-version-injector",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"author": "Nian Yi <nianyi778@gmail.com>",
|
5
|
+
"license": "MIT",
|
6
|
+
"description": "A universal plugin to inject version and build time into HTML (supports Webpack, Vite, Rollup)",
|
7
|
+
"repository": {
|
8
|
+
"type": "git",
|
9
|
+
"url": "https://github.com/nianyi778/unplugin-version-injector.git"
|
10
|
+
},
|
11
|
+
"keywords": ["unplugin", "version", "injector", "webpack", "vite", "rollup"],
|
12
|
+
"main": "./dist/cjs/index.js",
|
13
|
+
"module": "./dist/esm/index.js",
|
14
|
+
"exports": {
|
15
|
+
"require": "./dist/cjs/index.js",
|
16
|
+
"import": "./dist/esm/index.js",
|
17
|
+
"default": "./dist/esm/index.js"
|
18
|
+
},
|
19
|
+
"types": "./dist/index.d.ts",
|
20
|
+
"scripts": {
|
21
|
+
"clean": "rm -rf dist",
|
22
|
+
"build:esm": "tsc --module ESNext --outDir dist/esm",
|
23
|
+
"build:cjs": "tsc --module CommonJS --outDir dist/cjs",
|
24
|
+
"build": "npm run clean && npm run build:esm && npm run build:cjs",
|
25
|
+
"publish": "npm run build && npm publish"
|
26
|
+
},
|
27
|
+
"dependencies": {
|
28
|
+
"unplugin": "^1.0.0"
|
29
|
+
},
|
30
|
+
"devDependencies": {
|
31
|
+
"typescript": "^4.9.5",
|
32
|
+
"webpack": "^5",
|
33
|
+
"vite": "^4",
|
34
|
+
"rollup": "^3"
|
35
|
+
},
|
36
|
+
"publishConfig": {
|
37
|
+
"access": "public"
|
38
|
+
},
|
39
|
+
"packageManager": "pnpm@10.5.2+sha512.da9dc28cd3ff40d0592188235ab25d3202add8a207afbedc682220e4a0029ffbff4562102b9e6e46b4e3f9e8bd53e6d05de48544b0c57d4b0179e22c76d1199b"
|
40
|
+
}
|
package/src/index.ts
ADDED
@@ -0,0 +1,77 @@
|
|
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
ADDED
package/src/utils.ts
ADDED
@@ -0,0 +1,20 @@
|
|
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
ADDED
@@ -0,0 +1,15 @@
|
|
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
|
+
|