From 8d1383cdaa11a1f13d4dd7becefd79ae5c9f90ae Mon Sep 17 00:00:00 2001 From: Marc Date: Sun, 23 Feb 2025 22:25:54 +0100 Subject: [PATCH] better dev setup --- frontend/.env.local.example | 4 +++- frontend/index.html | 2 +- frontend/vite.config.ts | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/frontend/.env.local.example b/frontend/.env.local.example index 6fd3aaff2..15dc19401 100644 --- a/frontend/.env.local.example +++ b/frontend/.env.local.example @@ -9,4 +9,6 @@ # SENTRY_AUTH_TOKEN=YOUR_TOKEN # SENTRY_ORG=vikunja # SENTRY_PROJECT=frontend-oss -# VIKUNJA_FRONTEND_BASE=/custom-subpath \ No newline at end of file +# VIKUNJA_FRONTEND_BASE=/custom-subpath + +# DEV_PROXY=http://vikunja-backend.domain.tld \ No newline at end of file diff --git a/frontend/index.html b/frontend/index.html index 5619b2f99..9ad084d61 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -22,7 +22,7 @@ // This variable points the frontend to the api. // It has to be the full url, including the last /api/v1 part and port. // You can change this if your api is not reachable on the same port as the frontend. - window.API_URL = 'http://localhost:3456/api/v1' + window.API_URL = '/api/v1' diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 66e97c7ed..df08e5f1b 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -66,12 +66,24 @@ function createFontMatcher(fontNames: string[]) { } // https://vitejs.dev/config/ -export default defineConfig(({mode}) => { +export default defineConfig(({command, mode}) => { // Load env file based on `mode` in the current working directory. // Set the third parameter to '' to load all env regardless of the `VITE_` prefix. // https://vitejs.dev/config/#environment-variables const env = loadEnv(mode, process.cwd(), '') + switch (command) { + case 'serve': + // this is DEV mode + return getServeConfig(env) + // return getBuildConfig(env) + case 'build': + // build for prodution + return getBuildConfig(env) + } +}) + +function getBuildConfig(env: Record) { return { base: env.VIKUNJA_FRONTEND_BASE, // https://vitest.dev/config/ @@ -220,4 +232,23 @@ export default defineConfig(({mode}) => { }, }, } -}) +} + +function getServeConfig(env: Record) { + // get some default settings from prod mod + const buildConfig = getBuildConfig(env) + // override prod settings with dev settings + return { + ...buildConfig, + server: { + ...buildConfig.server, + ...(env.DEV_PROXY && { proxy: { + '/api': { + target: env.DEV_PROXY, + changeOrigin: true, + secure: false, + }, + }}), + }, + } +}