Skip to content

Commit 2c1077a

Browse files
committed
use env var for app version in frontend
1 parent b8050f9 commit 2c1077a

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

frontend/src/constants/version.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Import version from the root package.json
2-
// This will be bundled at build time
3-
import { version } from '../../../package.json';
4-
5-
// Export the version for use in the app
6-
export const APP_VERSION = version;
1+
// Get version from Vite environment variable that's injected at build time
2+
// This is a more reliable approach than direct importing package.json
3+
export const APP_VERSION = import.meta.env.APP_VERSION || '1.0.2';

frontend/src/env.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// / <reference types="vite/client" />
2+
3+
interface ImportMetaEnv {
4+
readonly APP_VERSION: string;
5+
}
6+
7+
interface ImportMeta {
8+
readonly env: ImportMetaEnv;
9+
}

frontend/tsconfig.app.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@
2828
},
2929
"include": [
3030
"./",
31-
"../package.json"
3231
]
3332
}

frontend/vite.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import react from '@vitejs/plugin-react-swc';
2+
import fs from 'fs';
3+
import { resolve } from 'path';
24
import { defineConfig } from 'vite';
35

6+
// Read the version from the root package.json
7+
const packageJson = JSON.parse(
8+
fs.readFileSync(resolve(__dirname, '../package.json'), 'utf-8')
9+
);
10+
411
// https://vite.dev/config/
512
export default defineConfig({
613
plugins: [react()],
714
server: {
815
port: 2022
916
},
17+
define: {
18+
// Inject the version as a global variable
19+
'import.meta.env.APP_VERSION': JSON.stringify(packageJson.version),
20+
},
1021
// build: {
1122
// rollupOptions: {
1223
// output: {

0 commit comments

Comments
 (0)