fear(build): 添加打包时间显示

This commit is contained in:
BTMuli
2023-04-08 02:37:06 +08:00
parent 7b68554168
commit 80212f2592
6 changed files with 50 additions and 14 deletions

43
src/utils/TGBuild.ts Normal file
View File

@@ -0,0 +1,43 @@
/**
* @file utils TGBuild.ts
* @description 用于获取 vite 打包时间
* @see https://gitee.com/lihanspace/vite-plugin-build-time
* @author BTMuli<bt-muli@outlook.com>
* @since Alpha v0.1.2
*/
import { type Plugin } from "vite";
const buildTimeKey = "buildTime";
const buildTimePlugin = (modes: string[] = []): Plugin => {
let _mode = "";
return {
name: "build-time",
config (uc, { mode }) {
_mode = mode;
},
transformIndexHtml () {
if (_mode !== "production" && !modes.includes(_mode)) return;
return [{
tag: "script",
children: `window.${buildTimeKey} = ${Math.floor(Date.now() / 1000)}`,
}];
},
};
};
export const getBuildTime = (): string => {
if (typeof window === "undefined") {
console.warn("getBuildTime() should only be called in the browser");
return "dev";
}
const windowEnv = window as typeof window & { [buildTimeKey]?: string };
if (!windowEnv[buildTimeKey]) {
console.info("当前环境为开发环境");
return `dev.${Math.floor(Date.now() / 1000)}`;
}
return windowEnv[buildTimeKey];
};
export default buildTimePlugin;