Skip to content

Commit 47e2c50

Browse files
committed
feat(version): Auto-inject version from package.json at build time
- Read version from root package.json in vite.config.js - Define __APP_VERSION__ at build time for consistent versioning - VersionBadge now uses injected version with fallback
1 parent ccf18b0 commit 47e2c50

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

frontend/src/components/VersionBadge.jsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import React from "react";
22

3-
// Version should match root package.json
4-
export const APP_VERSION = "0.7.4";
3+
// Version is now injected at build time via Vite's define
4+
// Falls back to package.json import if define is not available
5+
const APP_VERSION = typeof __APP_VERSION__ !== 'undefined' ? __APP_VERSION__ : (() => {
6+
try {
7+
// Dynamic import doesn't work in all contexts, so we use a static fallback
8+
return "0.8.2";
9+
} catch {
10+
return "unknown";
11+
}
12+
})();
513

614
const VersionBadge = () => {
715
return (
@@ -17,4 +25,5 @@ const VersionBadge = () => {
1725
);
1826
};
1927

28+
export { APP_VERSION };
2029
export default VersionBadge;

frontend/vite.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { defineConfig, loadEnv } from "vite";
22
import react from "@vitejs/plugin-react";
33
import { resolve } from "path";
4+
import { readFileSync } from "fs";
5+
6+
// Read version from root package.json
7+
const rootPackageJson = JSON.parse(
8+
readFileSync(resolve(__dirname, "../package.json"), "utf-8")
9+
);
410

511
export default defineConfig(({ mode }) => {
612
const env = loadEnv(mode, resolve(__dirname, "../"), "");
@@ -11,6 +17,7 @@ export default defineConfig(({ mode }) => {
1117
"import.meta.env.VITE_DISABLE_REGISTRATION": JSON.stringify(
1218
env.DISABLE_REGISTRATION || "true",
1319
),
20+
"__APP_VERSION__": JSON.stringify(rootPackageJson.version),
1421
},
1522
plugins: [
1623
react(),

0 commit comments

Comments
 (0)