import { fileURLToPath, URL } from 'node:url'; import { defineConfig, loadEnv } from 'vite'; import plugin from '@vitejs/plugin-react'; import fs from 'fs'; import path from 'path'; import child_process from 'child_process'; import process, { env } from 'process'; const baseFolder = process.env.APPDATA !== undefined && process.env.APPDATA !== '' ? `${process.env.APPDATA}/ASP.NET/https` : `${process.env.HOME}/.aspnet/https`; const certificateName = "bts.datastreamwebapp.client"; const certFilePath = path.join(baseFolder, `${certificateName}.pem`); const keyFilePath = path.join(baseFolder, `${certificateName}.key`); if (!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."); } } export default defineConfig(({ $$command, mode, $$isSsrBuild, $$isPreview }) => { const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` : env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'https://localhost:8443'; return { plugins: [plugin()], resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } }, build: { minify: false, sourcemap: true, outDir: `../../../build/Bts.DataStreamWebApp.Client/${mode}/dist` }, server: { proxy: { '^/api': { target, secure: false }, '^/ws': { target, secure: false } }, port: 5173, https: { key: fs.readFileSync(keyFilePath), cert: fs.readFileSync(certFilePath), } } }; });