-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
76 lines (68 loc) · 2.3 KB
/
Copy pathvite.config.ts
File metadata and controls
76 lines (68 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import react from "@vitejs/plugin-react";
import { defineConfig, type Plugin } from "vite";
const appRoutes = new Set(["checkout", "dashboard", "portal"]);
const projectRoot = fileURLToPath(new URL(".", import.meta.url));
const publicBasePath = normalizePublicBasePath(firstNonEmpty(process.env.BILLTAP_PUBLIC_BASE_PATH, process.env.PUBLIC_BASE_PATH));
function appPathDevFallback(): Plugin {
return {
name: "billtap-app-path-dev-fallback",
apply: "serve",
configureServer(server) {
server.middlewares.use((req, _res, next) => {
const [pathname, query] = req.url?.split("?") ?? ["", ""];
const appPath = stripBasePath(pathname, publicBasePath);
const match = appPath.match(/^\/app\/(checkout|dashboard|portal)\/?$/);
if (match && appRoutes.has(match[1])) {
req.url = `/${match[1]}/index.html${query ? `?${query}` : ""}`;
} else if (appPath === "/app/" || appPath === "/app") {
req.url = `/dashboard/index.html${query ? `?${query}` : ""}`;
}
next();
});
},
};
}
function firstNonEmpty(...values: Array<string | undefined>): string {
for (const value of values) {
if (value?.trim()) return value;
}
return "";
}
function normalizePublicBasePath(value: string): string {
const trimmed = value.trim();
if (!trimmed || trimmed === "/") return "";
const withLeading = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
return withLeading.replace(/\/+$/, "");
}
function stripBasePath(pathname: string, basePath: string): string {
if (!basePath) return pathname;
if (pathname === basePath) return "/";
if (pathname.startsWith(`${basePath}/`)) return pathname.slice(basePath.length);
return pathname;
}
export default defineConfig({
root: "web",
base: "./",
plugins: [react(), appPathDevFallback()],
server: {
host: "127.0.0.1",
port: 5173,
},
preview: {
host: "127.0.0.1",
port: 4173,
},
build: {
outDir: "../dist/app",
emptyOutDir: true,
rollupOptions: {
input: {
checkout: resolve(projectRoot, "web/checkout/index.html"),
dashboard: resolve(projectRoot, "web/dashboard/index.html"),
portal: resolve(projectRoot, "web/portal/index.html"),
},
},
},
});