build(ui): define version constant

This commit is contained in:
PBK Bin 2025-06-22 20:50:01 +08:00 committed by GitHub
parent 2aac79af54
commit 14a3f055ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 18 deletions

View File

@ -1 +1,3 @@
export const version = "v0.3.18";
// .env > git tag > v0.0.0-beta
// https://vite.dev/guide/env-and-mode.html#env-variables
export const version = import.meta.env.VITE_APP_VERSION || __APP_VERSION__ || "v0.0.0-dev";

View File

@ -1 +1,2 @@
/// <reference types="vite/client" />
declare const __APP_VERSION__: string;

View File

@ -1,9 +1,10 @@
import { type SpawnSyncReturns, execFileSync } from "node:child_process";
import path from "node:path";
import legacyPlugin from "@vitejs/plugin-legacy";
import reactPlugin from "@vitejs/plugin-react";
import fs from "fs-extra";
import { type Plugin, defineConfig } from "vite";
import { type Plugin, defineConfig, loadEnv } from "vite";
const preserveFilesPlugin = (filesToPreserve: string[]): Plugin => {
return {
@ -32,22 +33,40 @@ const preserveFilesPlugin = (filesToPreserve: string[]): Plugin => {
};
};
export default defineConfig({
plugins: [
reactPlugin({}),
legacyPlugin({
targets: ["defaults", "not IE 11"],
}),
preserveFilesPlugin(["dist/.gitkeep"]),
],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
export default defineConfig(({ mode }) => {
const envs = loadEnv(mode, process.cwd());
let appVersion = undefined;
if (!envs?.VITE_APP_VERSION) {
try {
appVersion = execFileSync("git", ["describe", "--match", "v[0-9]*", "--tags", "--abbrev=8"], {
stdio: [],
})?.toString();
} catch (error) {
const err = error as SpawnSyncReturns<Buffer>;
console.warn("[Warn] failed to get version number through git", err?.stderr?.toString());
}
}
return {
define: {
__APP_VERSION__: JSON.stringify(appVersion),
},
},
server: {
proxy: {
"/api": "http://127.0.0.1:8090",
plugins: [
reactPlugin({}),
legacyPlugin({
targets: ["defaults", "not IE 11"],
}),
preserveFilesPlugin(["dist/.gitkeep"]),
],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
},
server: {
proxy: {
"/api": "http://127.0.0.1:8090",
},
},
};
});