Skip to content
File

Blob: ui/vite.config.ts

typescript68 lines
1import { fileURLToPath } from 'node:url';
2import { svelte } from '@sveltejs/vite-plugin-svelte';
3import tailwindcss from '@tailwindcss/vite';
4import { defineConfig, type Connect, type Plugin } from 'vite';
5 
6function routePages(preview = false): Connect.NextHandleFunction {
7 return (request, _response, next) => {
8 const url = new URL(request.url ?? '/', 'http://localhost');
9 const wantsJSON = url.searchParams.get('format') === 'json'
10 || request.headers.accept?.includes('application/json');
11 if (request.method === 'GET' || request.method === 'HEAD') {
12 if (preview && /^\/(?:_internal\/)?ui\//.test(url.pathname)) {
13 request.url = request.url?.replace(/^\/(?:_internal\/)?ui\//, '/');
14 } else if (!wantsJSON) {
15 if (url.pathname === '/') {
16 request.url = `/client/index.html${url.search}`;
17 } else if (/^\/_internal\/(?:overview\/?|tun(?:\/.*)?)?$/.test(url.pathname)) {
18 request.url = `/operator/index.html${url.search}`;
19 }
20 }
21 }
22 next();
23 };
24}
25 
26function pages(): Plugin {
27 return {
28 name: 'specter-pages',
29 configureServer(server) {
30 server.middlewares.use(routePages());
31 },
32 configurePreviewServer(server) {
33 server.middlewares.use(routePages(true));
34 },
35 };
36}
37 
38export default defineConfig({
39 appType: 'mpa',
40 base: './',
41 plugins: [pages(), svelte(), tailwindcss()],
42 experimental: {
43 renderBuiltUrl(filename, { hostId, hostType }) {
44 if (hostType === 'html') {
45 return `${hostId === 'operator/index.html' ? '/_internal/ui/' : '/ui/'}${filename}`;
46 }
47 return { relative: true };
48 },
49 },
50 build: {
51 outDir: 'dist',
52 emptyOutDir: true,
53 modulePreload: { polyfill: false },
54 rolldownOptions: {
55 input: {
56 client: fileURLToPath(new URL('./client/index.html', import.meta.url)),
57 operator: fileURLToPath(new URL('./operator/index.html', import.meta.url)),
58 },
59 },
60 },
61 server: {
62 proxy: {
63 '/api': process.env.SPECTER_CLIENT_ORIGIN ?? 'http://127.0.0.1:1180',
64 '/_internal': process.env.SPECTER_OPERATOR_ORIGIN ?? 'http://127.0.0.1:11180',
65 },
66 },
67});