50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import fs from 'node:fs';
|
|
import type { ServerOptions as HttpsServerOptions } from 'node:https';
|
|
import path from 'node:path';
|
|
import { defineConfig, loadEnv } from 'vite';
|
|
import react from '@vitejs/plugin-react';
|
|
|
|
export default defineConfig(({ mode }) => {
|
|
const env = loadEnv(mode, process.cwd(), '');
|
|
const apiPort = Number(env.PORT ?? 5000);
|
|
const httpsOptions = getHttpsOptions(env);
|
|
const apiProtocol = httpsOptions ? 'https' : 'http';
|
|
|
|
return {
|
|
plugins: [react()],
|
|
server: {
|
|
https: httpsOptions,
|
|
port: 5173,
|
|
strictPort: true,
|
|
proxy: {
|
|
'/api': {
|
|
target: `${apiProtocol}://127.0.0.1:${apiPort}`,
|
|
changeOrigin: true,
|
|
secure: false
|
|
}
|
|
}
|
|
}
|
|
};
|
|
});
|
|
|
|
const getConfiguredPath = (value: string | undefined) =>
|
|
value ? path.resolve(process.cwd(), value) : undefined;
|
|
|
|
const getHttpsOptions = (
|
|
env: Record<string, string>
|
|
): HttpsServerOptions | undefined => {
|
|
const keyPath = getConfiguredPath(env.HTTPS_KEY_PATH);
|
|
const certPath = getConfiguredPath(env.HTTPS_CERT_PATH);
|
|
|
|
if (!keyPath && !certPath) return undefined;
|
|
|
|
if (!keyPath || !certPath) {
|
|
throw new Error('HTTPS_KEY_PATH and HTTPS_CERT_PATH must both be set.');
|
|
}
|
|
|
|
return {
|
|
key: fs.readFileSync(keyPath),
|
|
cert: fs.readFileSync(certPath)
|
|
};
|
|
};
|