mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-03 18:22:57 +00:00
update
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
import path from 'node:path';
|
||||
import { URL } from 'node:url';
|
||||
import { defineConfig, loadEnv } from 'vite';
|
||||
import react from '@vitejs/plugin-react';
|
||||
|
||||
function resolveBackendOrigin(env: Record<string, string>) {
|
||||
const raw = env.REACT_APP_API_BASE_URL || env.REACT_APP_API_URL || '';
|
||||
const fallback = 'http://127.0.0.1:8080';
|
||||
|
||||
try {
|
||||
if (!raw || raw.startsWith('/')) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const parsed = new URL(raw);
|
||||
parsed.pathname = '/';
|
||||
parsed.search = '';
|
||||
parsed.hash = '';
|
||||
return parsed.toString();
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
function buildProcessEnv(mode: string, env: Record<string, string>, base: string) {
|
||||
const processEnv: Record<string, string> = {
|
||||
NODE_ENV: mode === 'production' ? 'production' : mode,
|
||||
PUBLIC_URL: base === '/' ? '' : base.replace(/\/$/, ''),
|
||||
};
|
||||
|
||||
for (const [key, value] of Object.entries(env)) {
|
||||
if (key.startsWith('REACT_APP_')) {
|
||||
processEnv[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return processEnv;
|
||||
}
|
||||
|
||||
export default defineConfig(({ mode }) => {
|
||||
const env = loadEnv(mode, process.cwd(), '');
|
||||
const base = env.PUBLIC_URL ? (env.PUBLIC_URL.endsWith('/') ? env.PUBLIC_URL : `${env.PUBLIC_URL}/`) : '/';
|
||||
const backendOrigin = resolveBackendOrigin(env);
|
||||
const processEnv = buildProcessEnv(mode, env, base);
|
||||
const commonProxy = {
|
||||
target: backendOrigin,
|
||||
changeOrigin: true,
|
||||
secure: false,
|
||||
};
|
||||
|
||||
return {
|
||||
base,
|
||||
publicDir: 'public',
|
||||
plugins: [react()],
|
||||
resolve: {
|
||||
alias: {
|
||||
'@': path.resolve(__dirname, 'src'),
|
||||
},
|
||||
},
|
||||
define: {
|
||||
'process.env': JSON.stringify(processEnv),
|
||||
global: 'globalThis',
|
||||
},
|
||||
build: {
|
||||
outDir: 'build',
|
||||
assetsDir: 'static',
|
||||
emptyOutDir: true,
|
||||
sourcemap: false,
|
||||
},
|
||||
server: {
|
||||
host: '0.0.0.0',
|
||||
port: 3000,
|
||||
proxy: {
|
||||
'/api': commonProxy,
|
||||
'/uploads': commonProxy,
|
||||
'/s': commonProxy,
|
||||
'/r': commonProxy,
|
||||
'/static': commonProxy,
|
||||
'/dist': commonProxy,
|
||||
'/cache': commonProxy,
|
||||
'/images': commonProxy,
|
||||
'/img': commonProxy,
|
||||
'/media': commonProxy,
|
||||
'/files': commonProxy,
|
||||
'/logos': commonProxy,
|
||||
'/avatars': commonProxy,
|
||||
'/downloads': commonProxy,
|
||||
'/public': commonProxy,
|
||||
'/favicon.ico': commonProxy,
|
||||
},
|
||||
},
|
||||
preview: {
|
||||
host: '0.0.0.0',
|
||||
port: 4173,
|
||||
},
|
||||
test: {
|
||||
environment: 'jsdom',
|
||||
globals: true,
|
||||
setupFiles: 'src/setupTests.ts',
|
||||
},
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user