Add .env and package.json version support

- Updated vite config to export the package.json version as env variable
- Added src/helpers/envHelper.js
- Updated some constants to support being set by the .env file
- Renamed Constants.sceneryFiles.fetchDisable to enableBuiltIn
This commit is contained in:
izawartka 2026-08-12 17:28:44 +02:00
parent 87b592e78b
commit f9494feefa
5 changed files with 28 additions and 5 deletions

View File

@ -1,2 +1,5 @@
# please create .env.dev and .env.prod based on this file # please create .env.dev and .env.prod based on this file
VITE_BASE=/ VITE_BASE=/
VITE_DO_LOG_SCENERY=true
VITE_DO_ENABLE_BUILTIN_SCENERIES=false
VITE_DO_SHOW_BACK_MASEUKO=false

View File

@ -166,7 +166,7 @@ export default function SceneryManager(props) {
}, [setScenery]); }, [setScenery]);
useEffect(() => { useEffect(() => {
if(Constants.sceneryFiles.fetchDisable) return; if(!Constants.sceneryFiles.enableBuiltIn) return;
updateSceneryList(); updateSceneryList();
}, [updateSceneryList]); }, [updateSceneryList]);

View File

@ -1,6 +1,8 @@
import { readEnvBool, readEnvString} from './envHelper';
const Constants = { const Constants = {
buildVersion: '1.7.2', buildVersion: readEnvString('VITE_APP_VERSION') ?? 'unknown',
showBackMaseuko: false, showBackMaseuko: readEnvBool('VITE_DO_SHOW_BACK_MASEUKO') ?? false,
map: { map: {
zoomSensitivity: 0.002, zoomSensitivity: 0.002,
zoomMin: 0.03, zoomMin: 0.03,
@ -15,7 +17,7 @@ const Constants = {
mapZoomObjectDetailsThreshold: 0.5 mapZoomObjectDetailsThreshold: 0.5
}, },
parser: { parser: {
logSceneryAfterFinished: true, logSceneryAfterFinished: readEnvBool('VITE_DO_LOG_SCENERY') ?? false,
sceneryInfoVersionMin: 29, sceneryInfoVersionMin: 29,
sceneryInfoVersionMax: 30, sceneryInfoVersionMax: 30,
alwaysShowLogDialog: false, alwaysShowLogDialog: false,
@ -73,7 +75,7 @@ const Constants = {
invalidSceneryInfo: true invalidSceneryInfo: true
}, },
sceneryFiles: { sceneryFiles: {
fetchDisable: true, enableBuiltIn: readEnvBool('VITE_DO_ENABLE_BUILTIN_SCENERIES') ?? false,
fetchBaseUrl: `${import.meta.env.BASE_URL}sceneries/`, fetchBaseUrl: `${import.meta.env.BASE_URL}sceneries/`,
fetchListUrl: `${import.meta.env.BASE_URL}sceneries/sceneries.json`, fetchListUrl: `${import.meta.env.BASE_URL}sceneries/sceneries.json`,
}, },

12
src/helpers/envHelper.js Normal file
View File

@ -0,0 +1,12 @@
export const readEnvString = (key) => {
return import.meta.env[key];
};
export const readEnvBool = (key) => {
const value = import.meta.env[key];
switch(value) {
case "true": return true;
case "false": return false;
default: return null;
}
};

View File

@ -1,5 +1,8 @@
import { defineConfig, loadEnv } from 'vite'; import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react'; import react from '@vitejs/plugin-react';
import fs from 'fs';
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
export default defineConfig(({ mode }) => { export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), ''); const env = loadEnv(mode, process.cwd(), '');
@ -9,6 +12,9 @@ export default defineConfig(({ mode }) => {
plugins: [ plugins: [
react(), react(),
], ],
define: {
'import.meta.env.VITE_APP_VERSION': JSON.stringify(packageJson.version),
},
build: { build: {
target: 'es2015', target: 'es2015',
}, },