diff --git a/vite.config.js b/vite.config.js index a4d4d38..ecc892b 100644 --- a/vite.config.js +++ b/vite.config.js @@ -14,8 +14,31 @@ import { fileURLToPath, URL } from "node:url"; import { defineConfig, loadEnv } from "vite"; import vue from "@vitejs/plugin-vue"; +/** + * Builds Vite proxy configuration when backend target is available. + * @param {Record} env - Loaded environment variables. + * @returns {Record|undefined} Proxy config, or undefined when no target is configured. + */ +function createApiProxy(env) { + if (!env.VUE_APP_API_URL) { + console.warn( + "[vite] VUE_APP_API_URL is not set; /api proxy is disabled for this run.", + ); + return undefined; + } + + return { + "/api": { + target: env.VUE_APP_API_URL, + changeOrigin: true, + rewrite: (path) => path.replace(/^\/api/, ""), + }, + }; +} + export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd()); + const proxy = createApiProxy(env); return { cacheDir: env.VITE_CACHE_DIR || undefined, @@ -31,13 +54,12 @@ export default defineConfig(({ mode }) => { server: { host: "localhost", port: 58249, - proxy: { - "/api": { - target: env.VUE_APP_API_URL, - changeOrigin: true, - rewrite: (path) => path.replace(/^\/api/, ""), - }, - }, + proxy, + }, + preview: { + host: "localhost", + port: 4173, + proxy, }, optimizeDeps: { include: ["vue", "vue-router", "pinia", "axios"],