-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
77 lines (73 loc) · 2.31 KB
/
vite.config.ts
File metadata and controls
77 lines (73 loc) · 2.31 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
77
import { defineConfig, Plugin } from "vite";
import react from "@vitejs/plugin-react-swc";
import path from "path";
import { copyFileSync } from "fs";
import type { IncomingMessage, ServerResponse } from "http";
import { createServer } from "./server";
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
// Use root path for custom domain (aof.sh)
// If deploying to GitHub Pages subdomain, change to "/aofdotsh/"
base: process.env.VITE_BASE_PATH || "/",
server: {
host: "::",
port: 3040,
fs: {
allow: ["./client", "./shared"],
deny: [".env", ".env.*", "*.{crt,pem}", "**/.git/**", "server/**"],
},
},
build: {
outDir: "dist/spa",
rollupOptions: {
input: {
main: path.resolve(__dirname, "index.html"),
},
},
},
publicDir: "public",
plugins: [react(), expressPlugin(), copy404Plugin()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./client"),
"@shared": path.resolve(__dirname, "./shared"),
},
},
}));
function expressPlugin(): Plugin {
return {
name: "express-plugin",
apply: "serve", // Only apply during development (serve mode)
configureServer(server) {
const app = createServer();
// Add Express app as middleware to Vite dev server
// Only handle /api routes, let Vite handle everything else
server.middlewares.use((req: IncomingMessage, res: ServerResponse, next: () => void) => {
if (req.url?.startsWith("/api")) {
// Express is compatible with Connect-style middleware (IncomingMessage/ServerResponse)
// Type assertion needed because Express types are more specific
app(req as any, res as any, next);
} else {
next();
}
});
},
};
}
function copy404Plugin(): Plugin {
return {
name: "copy-404",
apply: "build",
closeBundle() {
// Copy index.html to 404.html for GitHub Pages SPA routing
const indexPath = path.resolve(__dirname, "dist/spa/index.html");
const notFoundPath = path.resolve(__dirname, "dist/spa/404.html");
try {
copyFileSync(indexPath, notFoundPath);
console.log("✓ Copied index.html to 404.html for GitHub Pages");
} catch (error) {
console.warn("Failed to copy index.html to 404.html:", error);
}
},
};
}