vite-plugin-multiversion 1.0.1 → 1.0.3
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 +2 -1
- package/dist/index.js +3 -5
- package/dist/index.mjs +3 -5
- package/package.json +1 -1
- package/src/cleanOldVersions.ts +0 -75
- package/src/index.ts +0 -2
- package/src/versionLink.ts +0 -38
package/README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
通过打包进行多版本控制的 Vite 插件
|
|
4
4
|
|
|
5
|
+
|
|
5
6
|
## 安装
|
|
6
7
|
|
|
7
8
|
```bash
|
|
@@ -77,7 +78,7 @@ VITE_APP_ENV=prod
|
|
|
77
78
|
|
|
78
79
|
### cleanOldVersions
|
|
79
80
|
|
|
80
|
-
|
|
81
|
+
删除超过指定天数的旧版本目录。超过指定天数的删除版本中,会保留至少一个版本,防止出现指定删除x天,x+1天再打包把前面的版本全删掉了(保证打包完除了当前版本,至少还有最后的一个线上版本(超过x天))
|
|
81
82
|
|
|
82
83
|
| 参数 | 类型 | 默认值 | 说明 |
|
|
83
84
|
|------|------|--------|------|
|
package/dist/index.js
CHANGED
|
@@ -100,13 +100,11 @@ function cleanOldVersions(options) {
|
|
|
100
100
|
});
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
+
const maxOldTimestamp = Math.max(...oldVersions.map((i) => i.timestamp));
|
|
104
|
+
console.log(`[clean-old-versions] \u8D85\u8FC7${days}\u5929\u6700\u5927\u4FDD\u7559\u5386\u53F2\u4E3A:${maxOldTimestamp}`);
|
|
103
105
|
let deletedCount = 0;
|
|
104
|
-
oldVersions.forEach((item) => {
|
|
106
|
+
oldVersions.filter((i) => i.timestamp !== maxOldTimestamp).forEach((item) => {
|
|
105
107
|
const { dirPath, entryName } = item;
|
|
106
|
-
if (oldVersions.length - deletedCount <= 1) {
|
|
107
|
-
console.log(`[clean-old-versions] \u89E6\u53D1\u6700\u5C11\u4FDD\u7559\u8D85\u8FC71\u5929\u7684\u4E00\u4E2A\u65E7\u5386\u53F2: ${entryName}`);
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
108
|
import_node_fs2.default.rmSync(dirPath, { recursive: true, force: true });
|
|
111
109
|
console.log(`[clean-old-versions] \u5DF2\u5220\u9664: ${entryName}`);
|
|
112
110
|
deletedCount++;
|
package/dist/index.mjs
CHANGED
|
@@ -63,13 +63,11 @@ function cleanOldVersions(options) {
|
|
|
63
63
|
});
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
+
const maxOldTimestamp = Math.max(...oldVersions.map((i) => i.timestamp));
|
|
67
|
+
console.log(`[clean-old-versions] \u8D85\u8FC7${days}\u5929\u6700\u5927\u4FDD\u7559\u5386\u53F2\u4E3A:${maxOldTimestamp}`);
|
|
66
68
|
let deletedCount = 0;
|
|
67
|
-
oldVersions.forEach((item) => {
|
|
69
|
+
oldVersions.filter((i) => i.timestamp !== maxOldTimestamp).forEach((item) => {
|
|
68
70
|
const { dirPath, entryName } = item;
|
|
69
|
-
if (oldVersions.length - deletedCount <= 1) {
|
|
70
|
-
console.log(`[clean-old-versions] \u89E6\u53D1\u6700\u5C11\u4FDD\u7559\u8D85\u8FC71\u5929\u7684\u4E00\u4E2A\u65E7\u5386\u53F2: ${entryName}`);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
71
|
fs2.rmSync(dirPath, { recursive: true, force: true });
|
|
74
72
|
console.log(`[clean-old-versions] \u5DF2\u5220\u9664: ${entryName}`);
|
|
75
73
|
deletedCount++;
|
package/package.json
CHANGED
package/src/cleanOldVersions.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from 'vite'
|
|
2
|
-
import fs from 'node:fs'
|
|
3
|
-
import path from 'node:path'
|
|
4
|
-
|
|
5
|
-
export interface CleanOldVersionsOptions {
|
|
6
|
-
outDir?: string
|
|
7
|
-
days?: number
|
|
8
|
-
now?: number
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function cleanOldVersions(options: CleanOldVersionsOptions): Plugin {
|
|
12
|
-
const { outDir = 'dist-prod', days = 2, now } = options
|
|
13
|
-
|
|
14
|
-
return {
|
|
15
|
-
name: 'clean-old-versions',
|
|
16
|
-
apply: 'build',
|
|
17
|
-
closeBundle() {
|
|
18
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
19
|
-
return
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
const outDirPath = path.resolve(process.cwd(), outDir)
|
|
24
|
-
|
|
25
|
-
if (!fs.existsSync(outDirPath)) {
|
|
26
|
-
console.error(`[clean-old-versions] 目录不存在: ${outDirPath}`)
|
|
27
|
-
return
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const currentTime = now || Date.now()
|
|
31
|
-
const msPerDay = 24 * 60 * 60 * 1000
|
|
32
|
-
const threshold = currentTime - days * msPerDay
|
|
33
|
-
|
|
34
|
-
const entries = fs.readdirSync(outDirPath, { withFileTypes: true })
|
|
35
|
-
|
|
36
|
-
const oldVersions: { dirPath: string; timestamp: number; entryName: string }[] = []
|
|
37
|
-
|
|
38
|
-
for (const entry of entries) {
|
|
39
|
-
if (!entry.isDirectory()) continue
|
|
40
|
-
if (entry.name === 'index.html') continue
|
|
41
|
-
|
|
42
|
-
const timestamp = Number(entry.name)
|
|
43
|
-
if (isNaN(timestamp)) continue
|
|
44
|
-
|
|
45
|
-
if (timestamp < threshold) {
|
|
46
|
-
oldVersions.push({
|
|
47
|
-
dirPath: path.join(outDirPath, entry.name),
|
|
48
|
-
timestamp,
|
|
49
|
-
entryName: entry.name,
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let deletedCount = 0
|
|
55
|
-
|
|
56
|
-
oldVersions.forEach(item => {
|
|
57
|
-
const { dirPath, entryName } = item
|
|
58
|
-
|
|
59
|
-
if (oldVersions.length - deletedCount <= 1) {
|
|
60
|
-
console.log(`[clean-old-versions] 触发最少保留超过1天的一个旧历史: ${entryName}`)
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
fs.rmSync(dirPath, { recursive: true, force: true })
|
|
65
|
-
console.log(`[clean-old-versions] 已删除: ${entryName}`)
|
|
66
|
-
deletedCount++
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
console.log(`[clean-old-versions] ✅ 清理完成,删除 ${deletedCount} 个目录`)
|
|
70
|
-
} catch (err: any) {
|
|
71
|
-
console.error('[clean-old-versions] 操作失败:', err.message)
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
}
|
|
75
|
-
}
|
package/src/index.ts
DELETED
package/src/versionLink.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from 'vite'
|
|
2
|
-
import fs from 'node:fs'
|
|
3
|
-
import path from 'node:path'
|
|
4
|
-
|
|
5
|
-
export interface VersionLinkOptions {
|
|
6
|
-
versionDir: string
|
|
7
|
-
outDir?: string
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function versionLink(options: VersionLinkOptions): Plugin {
|
|
11
|
-
const { versionDir, outDir = 'dist-prod' } = options
|
|
12
|
-
|
|
13
|
-
if (!versionDir) {
|
|
14
|
-
throw new Error('[version-link] versionDir is required')
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return {
|
|
18
|
-
name: 'version-link',
|
|
19
|
-
apply: 'build',
|
|
20
|
-
closeBundle() {
|
|
21
|
-
try {
|
|
22
|
-
const outDirPath = path.resolve(process.cwd(), outDir)
|
|
23
|
-
const versionPath = path.join(outDirPath, versionDir, 'index.html')
|
|
24
|
-
const copyPath = path.join(outDirPath, 'index.html')
|
|
25
|
-
|
|
26
|
-
if (!fs.existsSync(versionPath)) {
|
|
27
|
-
console.error(`[version-link] 文件不存在: ${versionPath}`)
|
|
28
|
-
return
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
fs.copyFileSync(versionPath, copyPath)
|
|
32
|
-
console.log(`[version-link] ✅ 复制成功: ${versionDir}/index.html -> index.html`)
|
|
33
|
-
} catch (err: any) {
|
|
34
|
-
console.error('[version-link] 操作失败:', err.message)
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
}
|
|
38
|
-
}
|