vite-plugin-multiversion 1.0.2 → 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/package.json +1 -1
- package/src/cleanOldVersions.ts +0 -70
- package/src/index.ts +0 -2
- package/src/versionLink.ts +0 -38
package/package.json
CHANGED
package/src/cleanOldVersions.ts
DELETED
|
@@ -1,70 +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
|
-
const maxOldTimestamp = Math.max(...oldVersions.map(i => i.timestamp))
|
|
54
|
-
console.log(`[clean-old-versions] 超过${days}天最大保留历史为:${maxOldTimestamp}`)
|
|
55
|
-
let deletedCount = 0
|
|
56
|
-
|
|
57
|
-
oldVersions.filter(i => i.timestamp !== maxOldTimestamp).forEach(item => {
|
|
58
|
-
const { dirPath, entryName } = item
|
|
59
|
-
fs.rmSync(dirPath, { recursive: true, force: true })
|
|
60
|
-
console.log(`[clean-old-versions] 已删除: ${entryName}`)
|
|
61
|
-
deletedCount++
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
console.log(`[clean-old-versions] ✅ 清理完成,删除 ${deletedCount} 个目录`)
|
|
65
|
-
} catch (err: any) {
|
|
66
|
-
console.error('[clean-old-versions] 操作失败:', err.message)
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
}
|
|
70
|
-
}
|
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
|
-
}
|