From 4782c0f6fc8cc143b0d7ebe9f4c87a2eee78ee66 Mon Sep 17 00:00:00 2001 From: YISH Date: Fri, 26 Apr 2024 01:48:02 +0800 Subject: [PATCH] feat(ui): auto proxy backend (#2238) --- packages/ui/src/store/constant.js | 5 +--- packages/ui/vite.config.js | 45 ++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/packages/ui/src/store/constant.js b/packages/ui/src/store/constant.js index 8081815c8..02556d778 100644 --- a/packages/ui/src/store/constant.js +++ b/packages/ui/src/store/constant.js @@ -4,10 +4,7 @@ export const drawerWidth = 260 export const appDrawerWidth = 320 export const headerHeight = 80 export const maxScroll = 100000 -export const baseURL = - import.meta.env.PROD === true - ? window.location.origin - : window.location.origin.replace(`:${import.meta.env.VITE_PORT ?? '8080'}`, ':3000') +export const baseURL = window.location.origin export const uiBaseURL = window.location.origin export const FLOWISE_CREDENTIAL_ID = 'FLOWISE_CREDENTIAL_ID' export const REDACTED_CREDENTIAL_VALUE = '_FLOWISE_BLANK_07167752-1a71-43b1-bf8f-4f32252165db' diff --git a/packages/ui/vite.config.js b/packages/ui/vite.config.js index 4ccf09824..da529009c 100644 --- a/packages/ui/vite.config.js +++ b/packages/ui/vite.config.js @@ -3,21 +3,36 @@ import react from '@vitejs/plugin-react' import { resolve } from 'path' import dotenv from 'dotenv' -dotenv.config() - -export default defineConfig({ - plugins: [react()], - resolve: { - alias: { - '@': resolve(__dirname, 'src') +export default defineConfig(async ({ mode }) => { + let proxy = undefined + if (mode === 'development') { + const serverPort = parseInt(dotenv.config({ processEnv: {}, path: '../server/.env' }).parsed?.['PORT']) + if (!Number.isNaN(serverPort) && serverPort > 0 && serverPort < 65535) { + proxy = { + '/api': { + target: `http://localhost:${serverPort}`, + changeOrigin: true + } + } + } + } + dotenv.config() + return { + plugins: [react()], + resolve: { + alias: { + '@': resolve(__dirname, 'src') + } + }, + root: resolve(__dirname), + build: { + outDir: './build' + }, + server: { + open: true, + proxy, + port: process.env.VITE_PORT ?? 8080, + host: process.env.VITE_HOST } - }, - root: resolve(__dirname), - build: { - outDir: './build' - }, - server: { - open: true, - port: process.env.VITE_PORT ?? 8080 } })