Files
US-Entry-Coach/usentrycoach.client/vite.config.ts
Simon Lübeß f4737f4dcf
All checks were successful
Build Backend and Frontend / Build & Test .NET Backend (push) Successful in 32s
Build Backend and Frontend / Build Frontend (push) Successful in 12s
Don't create self signed cert in production build pipeline
2025-05-23 16:33:23 +02:00

81 lines
2.5 KiB
TypeScript

import { fileURLToPath, URL } from 'node:url';
import { defineConfig, type ProxyOptions } from 'vite';
import plugin from '@vitejs/plugin-react';
import fs from 'fs';
import path from 'path';
import child_process from 'child_process';
import { env } from 'process';
const isDevelopment = env.NODE_ENV !== 'production';
const baseFolder =
env.APPDATA !== undefined && env.APPDATA !== ''
? `${env.APPDATA}/ASP.NET/https`
: `${env.HOME}/.aspnet/https`;
const certificateName = "usentrycoach.client";
const certFilePath = path.join(baseFolder, `${certificateName}.pem`);
const keyFilePath = path.join(baseFolder, `${certificateName}.key`);
if (!fs.existsSync(baseFolder))
{
fs.mkdirSync(baseFolder, { recursive: true });
}
// Generate dev certificate, if we are in development mode, and it doesn't exist yet.
if (isDevelopment && (!fs.existsSync(certFilePath) || !fs.existsSync(keyFilePath)))
{
if (0 !== child_process.spawnSync('dotnet', [
'dev-certs',
'https',
'--export-path',
certFilePath,
'--format',
'Pem',
'--no-password',
], { stdio: 'inherit', }).status)
{
throw new Error("Could not create certificate.");
}
}
const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'https://localhost:7085';
// Define a list of all existing backend routes.
const backendRoutes = ['/login', '/ephemeral_token'];
// For development, we have a node.js server running that delivers our frontend.
// We have to configure proxies for the backend calls, so that node.js will forward them to the backend.
const proxyConfig = backendRoutes.reduce((acc, path) =>
{
acc[path] = {
target: target,
// disable certificate verification because the development certificate is self-signed
secure: false,
changeOrigin: true
}
return acc;
}, {} as Record<string, ProxyOptions>);
// https://vitejs.dev/config/
export default defineConfig({
plugins: [plugin()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
server: {
proxy: proxyConfig,
port: parseInt(env.DEV_SERVER_PORT ?? '54044'),
// This is only relevant for development anyway.
https: isDevelopment ? {
key: fs.readFileSync(keyFilePath),
cert: fs.readFileSync(certFilePath),
} : undefined
}
})