-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathversionPlugin.ts
More file actions
34 lines (29 loc) · 987 Bytes
/
Copy pathversionPlugin.ts
File metadata and controls
34 lines (29 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// vite.config.ts
import fs from "fs";
import crypto from "crypto";
import path from "path";
import { fileURLToPath } from "url";
// 获取当前 vite.config.ts 所在目录
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export const versionPlugin = {
name: "post-build-hash",
closeBundle() {
console.log("Vite build finished!");
// 相对于 vite.config.ts 的路径
const manifestPath = path.resolve(__dirname, "dist/.vite/manifest.json");
if (!fs.existsSync(manifestPath)) {
console.warn("manifest.json not found!");
return;
}
const content = fs.readFileSync(manifestPath);
const hash = crypto.createHash("sha256").update(content).digest("hex");
const version = {
hash,
timeStamp: Date.now(),
};
// 写入 dist/manifest.hash
const hashFile = path.resolve(__dirname, "dist/.vite/version.json");
fs.writeFileSync(hashFile, JSON.stringify(version));
},
};