-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathvite.config.ts
More file actions
89 lines (85 loc) · 2.68 KB
/
vite.config.ts
File metadata and controls
89 lines (85 loc) · 2.68 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
import fs from "node:fs";
import path from "node:path";
import devServer from "@hono/vite-dev-server";
import tailwindcss from "@tailwindcss/vite";
import { TanStackRouterVite } from "@tanstack/router-plugin/vite";
import react from "@vitejs/plugin-react-swc";
import TOML from "smol-toml";
import { defineConfig, loadEnv } from "vite";
import { analyzer } from "vite-bundle-analyzer";
// Read wrangler.toml configuration
function readWranglerConfig() {
try {
const wranglerPath = path.resolve(__dirname, "wrangler.toml");
if (fs.existsSync(wranglerPath)) {
const content = fs.readFileSync(wranglerPath, "utf-8");
const config = TOML.parse(content) as any;
return config.vars || {};
}
} catch (error) {
console.warn("Failed to read wrangler.toml:", error);
}
return {};
}
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), "");
const wranglerVars = readWranglerConfig();
// Adapted to work in both Cloudflare Workers and Node.js environments
function getEnv(key: string, defaultValue?: string): string | undefined {
const envValue = process.env[key] || env[key] || wranglerVars[key];
if (envValue !== undefined) {
return JSON.stringify(envValue);
}
if (defaultValue !== undefined) {
return JSON.stringify(defaultValue);
}
return undefined;
}
return {
plugins: [
TanStackRouterVite({
target: "react",
autoCodeSplitting: true,
routesDirectory: "src/app/routes",
generatedRouteTree: "src/app/routeTree.gen.ts",
}),
react(),
tailwindcss(),
devServer({
entry: "./src/server/index.ts",
exclude: [
/.*\.tsx?($|\?)/,
/.*\.(s?css|less)($|\?)/,
/.*\.(svg|png)($|\?)/,
/.*\.json($|\?)/,
/.*\.sql($|\?)/,
/^\/@.+$/,
/^\/favicon\.ico$/,
/^\/(public|assets|static)\/.+/,
/^\/node_modules\/.*/,
],
injectClientScript: false,
}),
mode === "analyze" ? analyzer() : undefined,
],
optimizeDeps: {
exclude: ["@vlcn.io/crsqlite-wasm"],
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
// Only define environment variables that are needed on the client side
define: {
"import.meta.env.RUNTIME": getEnv("RUNTIME"),
"import.meta.env.MODE": getEnv("MODE"),
"import.meta.env.AUTH_EMAIL_VERIFICATION_ENABLED": getEnv("AUTH_EMAIL_VERIFICATION_ENABLED"),
"import.meta.env.AUTH_SOCIAL_GOOGLE_ENABLED": getEnv("AUTH_SOCIAL_GOOGLE_ENABLED"),
"import.meta.env.AUTH_SOCIAL_GITHUB_ENABLED": getEnv("AUTH_SOCIAL_GITHUB_ENABLED"),
"import.meta.env.GOOGLE_ANALYTICS_ID": getEnv("GOOGLE_ANALYTICS_ID"),
"import.meta.env.PROVIDER_CLOUDFLARE_BUILTIN": getEnv("PROVIDER_CLOUDFLARE_BUILTIN"),
},
};
});