🌱 构建脚本

This commit is contained in:
BTMuli
2025-12-30 02:53:00 +08:00
parent 0c8eda3f74
commit d50dcc34c6
14 changed files with 114 additions and 87 deletions

View File

@@ -1,43 +0,0 @@
/**
* 用于获取 vite 打包时间
* @see https://gitee.com/lihanspace/vite-plugin-build-time
* @since Alpha v0.1.4
*/
import type { Plugin } from "vite";
const buildTimeKey = "buildTime";
const buildTimePlugin = (modes: Array<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 = <typeof window & { [buildTimeKey]?: string }>window;
if (!windowEnv[buildTimeKey]) {
console.info("当前环境为开发环境");
return `dev.${Math.floor(Date.now() / 1000)}`;
}
return windowEnv[buildTimeKey];
};
export default buildTimePlugin;