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 CHANGED
@@ -1,134 +1,158 @@
1
- ### **๐Ÿš€ `unplugin-version-injector` - Auto Inject Version & Build Time**
1
+ # ๐Ÿš€ `unplugin-version-injector` โ€“ Auto Inject Version & Build Time
2
2
 
3
- [๐Ÿ‡จ๐Ÿ‡ณ ไธญๆ–‡ README](./README.zh-CN.md) | [๐Ÿ‡ฌ๐Ÿ‡ง English README](./README.md)
3
+ [๐Ÿ‡จ๐Ÿ‡ณ ไธญๆ–‡ๆ–‡ๆกฃ](./README.zh-CN.md) | [๐Ÿ‡บ๐Ÿ‡ธ English README](./README.md)
4
4
 
5
5
  ---
6
6
 
7
- ## **๐Ÿ“Œ Introduction**
8
- `unplugin-version-injector` is a powerful and lightweight plugin that automatically injects the **version number** and **build timestamp** into all HTML files. It supports **Webpack 4/5, Vite, and Rollup**, making it ideal for both **Single Page Applications (SPA)** and **Multi-Page Applications (MPA)**.
7
+ ## ๐Ÿ“Œ Introduction
9
8
 
10
- ### **โœจ Features**
11
- โœ… **Auto-injects** `<meta name="version">` into all HTML `<head>` sections
12
- โœ… **Auto-injects a `<script>`** that logs `version` & `build time` in the browser console
13
- โœ… **Supports Webpack 4 & 5, Vite, and Rollup**
14
- โœ… **Works in Multi-Page Applications (MPA)**
15
- โœ… **Highly configurable**: Supports manually specifying the version or using `package.json`
9
+ **`unplugin-version-injector`** is a lightweight 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
- ## **๐Ÿ“ฆ Installation**
20
- ```sh
21
- # Using Yarn
22
- yarn add -D unplugin-version-injector
21
+ ## ๐Ÿ“ฆ Installation
23
22
 
24
- # Using npm
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
- ## **๐Ÿš€ Usage**
36
+ ## ๐Ÿš€ Usage
31
37
 
32
- ### **๐Ÿ“Œ Webpack 4/5**
33
- Modify your `webpack.config.js`:
34
- ```js
35
- const versionInjectorPlugin = require('unplugin-version-injector');
38
+ ### โ–ถ Vite
36
39
 
37
- module.exports = {
38
- plugins: [
39
- versionInjectorPlugin.webpack({
40
- version: '1.2.3', // (Optional) Manually specify version
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
- ### **๐Ÿ“Œ Vite**
49
- Modify your `vite.config.js`:
51
+ ### โ–ถ Webpack (4 or 5)
52
+
50
53
  ```js
51
- import versionInjectorPlugin from 'unplugin-version-injector';
54
+ // webpack.config.js
55
+ const versionInjector = require('unplugin-version-injector/webpack')
52
56
 
53
- export default {
54
- plugins: [versionInjectorPlugin.vite()]
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
- ### **๐Ÿ“Œ Rollup**
61
- Modify your `rollup.config.js`:
68
+ ### โ–ถ Rollup
69
+
62
70
  ```js
63
- import versionInjectorPlugin from 'unplugin-version-injector';
71
+ // rollup.config.js
72
+ import versionInjector from 'unplugin-version-injector/rollup'
64
73
 
65
74
  export default {
66
- plugins: [versionInjectorPlugin.rollup()]
67
- };
75
+ plugins: [versionInjector()],
76
+ }
68
77
  ```
69
78
 
70
79
  ---
71
80
 
72
- ## **๐Ÿ“œ Example Output**
73
- After building, all HTML files will include the following:
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
- <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;");
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
- โœ… **Console Output (Colored Logs)**
90
- ```
91
- ๐ŸŸข Version: 1.2.3 (Green)
92
- ๐ŸŸก Build Time: 2024-03-01T12:00:00.000Z (Yellow)
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
- ## **๐Ÿ”ง 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` |
119
+ ## ๐Ÿ’ก Why Use This?
103
120
 
104
- ### **Example: Custom Config**
105
- ```js
106
- versionInjectorPlugin.webpack({
107
- version: '2.0.0',
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
- ## **๐ŸŒ 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
128
+ ## ๐Ÿ“œ License
129
+
130
+ MIT ยฉ [@nianyi778](https://github.com/nianyi778)
119
131
 
120
132
  ---
121
133
 
122
- ## **๐Ÿ“œ License**
123
- MIT License ยฉ 2024 [Nian YI](https://github.com/nianyi778)
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
- ## **๐Ÿ’ก Contributing**
128
- Pull requests are welcome! If you encounter any issues, feel free to open an issue on GitHub.
150
+ ## ๐Ÿค Contributing
151
+
152
+ Feel free to submit issues or PRs ๐Ÿ™Œ
129
153
 
130
- **GitHub Repository:** [๐Ÿ”— unplugin-version-injector](https://github.com/nianyi778/unplugin-version-injector)
154
+ GitHub โ†’ https://github.com/nianyi778/unplugin-version-injector
131
155
 
132
156
  ---
133
157
 
134
- ๐Ÿ”ฅ **`unplugin-version-injector` โ€“ The simplest way to keep track of your app's version & build time!** ๐Ÿš€
158
+ ๐Ÿ”ฅ Simple, reliable version & build timestamp injection โ€“ that just works!
package/README.zh-CN.md CHANGED
@@ -1,25 +1,26 @@
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.zh-CN.md)
4
4
 
5
5
  ---
6
6
 
7
- ## **๐Ÿ“Œ ็ฎ€ไป‹**
8
- `unplugin-version-injector` ๆ˜ฏไธ€ไธช **่ฝป้‡็บง** ๆ’ไปถ๏ผŒๅฏ่‡ชๅŠจๅฐ† **็‰ˆๆœฌๅท** ๅ’Œ **ๆž„ๅปบๆ—ถ้—ด** ๆณจๅ…ฅๅˆฐๆ‰€ๆœ‰ HTML ๆ–‡ไปถไธญใ€‚
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
- โœ… **่‡ชๅŠจๆณจๅ…ฅ** `<meta name="version">` ๅˆฐๆ‰€ๆœ‰ HTML `<head>` ้ƒจๅˆ†
13
- โœ… **่‡ชๅŠจๆณจๅ…ฅ `<script>`**๏ผŒๅœจๆต่งˆๅ™จๆŽงๅˆถๅฐๆ‰“ๅฐ `็‰ˆๆœฌๅท` & `ๆž„ๅปบๆ—ถ้—ด`
14
- โœ… **ๅ…ผๅฎน Webpack 4 & 5ใ€Vite ๅ’Œ Rollup**
15
- โœ… **ๆ”ฏๆŒๅคš้กตๅบ”็”จ (MPA)**๏ผŒไธไผš้—ๆผไปปไฝ• HTML
16
- โœ… **ๆ”ฏๆŒๆ‰‹ๅŠจๆŒ‡ๅฎš็‰ˆๆœฌๅท**๏ผŒ้ป˜่ฎค่ฏปๅ– `package.json`
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
- ```sh
22
- # ไฝฟ็”จ Yarn
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
- ### **๐Ÿ“Œ Webpack 4/5**
34
- ไฟฎๆ”น `webpack.config.js`๏ผš
35
- ```js
36
- const versionInjectorPlugin = require('unplugin-version-injector');
34
+ ### **๐Ÿ“Œ Vite**
35
+ `vite.config.ts` ไธญ้…็ฝฎ๏ผš
36
+ ```ts
37
+ import versionInjector from 'unplugin-version-injector/vite';
37
38
 
38
- module.exports = {
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
- ### **๐Ÿ“Œ Vite**
50
- ไฟฎๆ”น `vite.config.js`๏ผš
46
+ ### **๐Ÿ“Œ Webpack 4/5**
47
+ `webpack.config.js` ไธญ้…็ฝฎ๏ผš
51
48
  ```js
52
- import versionInjectorPlugin from 'unplugin-version-injector';
49
+ const versionInjector = require('unplugin-version-injector/webpack');
53
50
 
54
- export default {
55
- plugins: [versionInjectorPlugin.vite()]
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
- ไฟฎๆ”น `rollup.config.js`๏ผš
63
+ `rollup.config.js` ไธญ้…็ฝฎ๏ผš
63
64
  ```js
64
- import versionInjectorPlugin from 'unplugin-version-injector';
65
+ import versionInjector from 'unplugin-version-injector/rollup';
65
66
 
66
67
  export default {
67
- plugins: [versionInjectorPlugin.rollup()]
68
+ plugins: [versionInjector()],
68
69
  };
69
70
  ```
70
71
 
71
72
  ---
72
73
 
73
- ## **๐Ÿ“œ ็”Ÿๆˆ็š„ HTML ็คบไพ‹**
74
- ๆž„ๅปบๅฎŒๆˆๅŽ๏ผŒๆ‰€ๆœ‰ HTML ๆ–‡ไปถๅฐ†ๅŒ…ๅซไปฅไธ‹ๅ†…ๅฎน๏ผš
74
+ ## **๐Ÿงช ็คบไพ‹่พ“ๅ‡บ**
75
+
76
+ ๆž„ๅปบๅŽ็š„ HTML ๆ–‡ไปถไธญๅฐ†่‡ชๅŠจๆณจๅ…ฅ๏ผš
77
+
75
78
  ```html
76
79
  <head>
77
80
  <meta name="version" content="1.2.3">
78
- <meta charset="UTF-8">
79
- <title>ๆˆ‘็š„ๅบ”็”จ</title>
81
+ ...
80
82
  </head>
81
83
  <body>
82
- <h1>Hello World</h1>
84
+ ...
83
85
  <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
+ 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
- ๐ŸŸข ็‰ˆๆœฌๅท: 1.2.3 (็ปฟ่‰ฒ)
93
- ๐ŸŸก ๆž„ๅปบๆ—ถ้—ด: 2024-03-01T12:00:00.000Z (้ป„่‰ฒ)
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
- ```js
107
- versionInjectorPlugin.webpack({
108
- version: '2.0.0',
109
- log: false, // ๅ…ณ้—ญๆŽงๅˆถๅฐๆ—ฅๅฟ—
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
- ## **๐ŸŒ ไธบไป€ไนˆ้€‰ๆ‹ฉ `unplugin-version-injector`๏ผŸ**
116
- - ๐Ÿ›  **ๅผ€็ฎฑๅณ็”จ**๏ผšๅฎ‰่ฃ…ๅŽ็ซ‹ๅณ็”Ÿๆ•ˆ๏ผŒๆ— ้œ€้ขๅค–้…็ฝฎ
117
- - ๐Ÿš€ **ๆๅ‡่ฐƒ่ฏ•ๆ•ˆ็އ**๏ผš่ฝปๆพๆŸฅ็œ‹ๅฝ“ๅ‰็‰ˆๆœฌไฟกๆฏ
118
- - ๐Ÿ“… **่ฟฝ่ธชๆž„ๅปบๆ—ถ้—ด**๏ผšๆ–นไพฟ็›‘ๆŽงไธๅŒ็‰ˆๆœฌ็š„ๅ‘ๅธƒๆ—ถ้—ด
119
- - ๐ŸŽฏ **่ฝป้‡้ซ˜ๆ•ˆ**๏ผšๅ‡ ไนŽไธไผšๅฝฑๅ“ๆž„ๅปบ้€Ÿๅบฆ
111
+ ## **๐ŸŒ ไธบไป€ไนˆ้€‰ๆ‹ฉ่ฟ™ไธชๆ’ไปถ๏ผŸ**
120
112
 
121
- ---
122
-
123
- ## **๐Ÿ“œ ่ฎธๅฏ่ฏ**
124
- MIT License ยฉ 2024 [Nian YI](https://github.com/nianyi778)
113
+ - ๐Ÿ›  ้›ถ้…็ฝฎ๏ผŒๅผ€็ฎฑๅณ็”จ
114
+ - ๐Ÿš€ ๅฟซ้€Ÿๅฎšไฝ็‰ˆๆœฌ้—ฎ้ข˜
115
+ - ๐Ÿ“… ่ทŸ่ธชๆž„ๅปบๆ—ถ้—ด
116
+ - ๐ŸŽฏ ๆ”ฏๆŒๅคšๆž„ๅปบๅทฅๅ…ท
117
+ - ๐Ÿงฉ ้ซ˜ๅบฆๅฏ้…็ฝฎ๏ผŒ็ตๆดปๆ’ๆ‹”
125
118
 
126
119
  ---
127
120
 
128
- ## **๐Ÿ’ก ่ดก็Œฎ**
129
- ๆฌข่ฟŽ PR๏ผๅฆ‚ๆœ‰้—ฎ้ข˜๏ผŒๆฌข่ฟŽๅœจ GitHub ๆไบค issueใ€‚
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
- ๐Ÿ”ฅ **`unplugin-version-injector` - ่ฎฉไฝ ็š„ๅบ”็”จ็‰ˆๆœฌ็ฎก็†ๆ›ด็ฎ€ๅ•๏ผ** ๐Ÿš€๐Ÿš€๐Ÿš€
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;
@@ -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 };
@@ -0,0 +1,5 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ declare function versionInjectorPlugin(options?: {}): Plugin;
4
+
5
+ export { versionInjectorPlugin as default };
package/dist/vite.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ declare function versionInjectorPlugin(options?: {}): Plugin;
4
+
5
+ export { versionInjectorPlugin as default };
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 };
@@ -0,0 +1,7 @@
1
+ import { Compiler } from 'webpack';
2
+
3
+ declare function versionInjectorPlugin(options?: {}): {
4
+ apply(compiler: Compiler): void;
5
+ };
6
+
7
+ export { versionInjectorPlugin as default };
@@ -0,0 +1,7 @@
1
+ import { Compiler } from 'webpack';
2
+
3
+ declare function versionInjectorPlugin(options?: {}): {
4
+ apply(compiler: Compiler): void;
5
+ };
6
+
7
+ export { versionInjectorPlugin as default };
@@ -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;
@@ -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": "1.1.2",
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
- "type": "git",
9
- "url": "https://github.com/nianyi778/unplugin-version-injector.git"
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
- "require": "./dist/cjs/index.js",
16
- "import": "./dist/esm/index.js",
17
- "default": "./dist/esm/index.js"
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
- "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
- "prepublishOnly": "npm run build"
46
+ "clean": "rm -rf dist",
47
+ "build": "tsup",
48
+ "prepublishOnly": "npm run build"
26
49
  },
27
50
  "dependencies": {
28
- "unplugin": "^1.0.0"
51
+ "tsup": "^8.4.0"
29
52
  },
30
53
  "devDependencies": {
31
- "typescript": "^4.9.5",
32
- "webpack": "^5",
33
- "vite": "^4",
34
- "rollup": "^3"
54
+ "typescript": "^4.9.5",
55
+ "webpack": "^5",
56
+ "vite": "^4",
57
+ "rollup": "^3"
35
58
  },
36
59
  "publishConfig": {
37
- "access": "public"
60
+ "access": "public"
38
61
  },
39
- "files": ["dist","README.md","README.zh-CN.md"],
62
+ "files": [
63
+ "dist",
64
+ "README.md",
65
+ "README.zh-CN.md"
66
+ ],
40
67
  "packageManager": "pnpm@10.5.2+sha512.da9dc28cd3ff40d0592188235ab25d3202add8a207afbedc682220e4a0029ffbff4562102b9e6e46b4e3f9e8bd53e6d05de48544b0c57d4b0179e22c76d1199b"
41
- }
68
+ }
@@ -1,3 +0,0 @@
1
- import type { VersionInjectorOptions } from './types';
2
- declare const VersionInjectorPlugin: import("unplugin").UnpluginInstance<VersionInjectorOptions | undefined, boolean>;
3
- export default VersionInjectorPlugin;
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;
@@ -1,5 +0,0 @@
1
- export interface VersionInjectorOptions {
2
- version?: string;
3
- log?: boolean;
4
- formatDate?: (date: Date) => string;
5
- }
package/dist/cjs/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,4 +0,0 @@
1
- /** ่Žทๅ– package.json ็‰ˆๆœฌ */
2
- export declare function getPackageVersion(): string;
3
- /** ้ป˜่ฎคๆ ผๅผๅŒ– build time */
4
- export declare function defaultFormatDate(date: Date): string;
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;
@@ -1,3 +0,0 @@
1
- import type { VersionInjectorOptions } from './types';
2
- declare const VersionInjectorPlugin: import("unplugin").UnpluginInstance<VersionInjectorOptions | undefined, boolean>;
3
- export default VersionInjectorPlugin;
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;
@@ -1,5 +0,0 @@
1
- export interface VersionInjectorOptions {
2
- version?: string;
3
- log?: boolean;
4
- formatDate?: (date: Date) => string;
5
- }
package/dist/esm/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,4 +0,0 @@
1
- /** ่Žทๅ– package.json ็‰ˆๆœฌ */
2
- export declare function getPackageVersion(): string;
3
- /** ้ป˜่ฎคๆ ผๅผๅŒ– build time */
4
- export declare function defaultFormatDate(date: Date): string;
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
- }