-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
97 lines (85 loc) · 3 KB
/
Copy pathvite.config.ts
File metadata and controls
97 lines (85 loc) · 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright (c) Mike Lischke. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import { readFileSync } from "node:fs";
import { join } from "node:path";
import preact from "@preact/preset-vite";
import tailwindcss from "@tailwindcss/vite";
import { defineConfig, Plugin } from "vite";
/**
* Helper plugin to serve index.html for non-asset requests, enabling SPA-like routing in development mode.
*
* @returns A Vite plugin object.
*/
const spaDocumentFallback = (): Plugin => {
return {
name: "spa-document-fallback",
apply: "serve",
configureServer: (server) => {
server.middlewares.use((req, _res, next) => {
const method = req.method ?? "GET";
if (method !== "GET" && method !== "HEAD") {
next();
return;
}
const url = req.url ?? "/";
const pathname = url.split("?")[0];
const accept = req.headers.accept ?? "";
const isHtmlRequest =
accept.includes("text/html") || accept.includes("*/*");
const isAssetLike =
pathname.startsWith("/api") ||
pathname.startsWith("/sounds") ||
pathname.startsWith("/assets") ||
pathname.startsWith("/src") ||
pathname.startsWith("/@vite") ||
pathname.includes("/__vite_") ||
/\.[a-zA-Z0-9]+$/.test(pathname);
if (!isHtmlRequest || isAssetLike) {
next();
return;
}
req.url = "/index.html";
next();
});
},
};
};
// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
// eslint-disable-next-line no-restricted-syntax
const home = process.env.HOME ?? process.env.USERPROFILE ?? "";
return {
server: command === "serve" ? {
https: {
key: readFileSync(join(home, ".ssh", "localhost-key.pem")),
cert: readFileSync(join(home, ".ssh", "localhost-cert.pem")),
},
host: "127.0.0.1",
port: 5173,
proxy: {
"/api": {
target: "http://127.0.0.1:3100",
changeOrigin: true,
},
"/soundLib": {
target: "http://127.0.0.1:3100",
changeOrigin: true,
},
},
} : undefined,
plugins: [
preact({
prefreshEnabled: false, // Disable Preact's fast refresh to avoid issues with the NoteViewer component.
}),
tailwindcss(),
spaDocumentFallback(),
],
build: {
target: "esnext",
assetsInlineLimit: 0, // Don't inline any assets.
},
appType: "mpa",
};
});