Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

vite.config.ts

171 lines6.0 KB

1import tailwindcss from "@tailwindcss/vite";
2import react, { reactCompilerPreset } from "@vitejs/plugin-react";
3import babel from "@rolldown/plugin-babel";
4import { tanstackRouter } from "@tanstack/router-plugin/vite";
5import { defineProject, type TestProjectInlineConfiguration } from "vite-plus/test/config";
6import "vite-plus/test/config";
7import { defineConfig } from "vite-plus";
8import pkg from "./package.json" with { type: "json" };
9​
10import { loadRepoEnv } from "../../scripts/lib/public-config";
11​
12const repoEnv = loadRepoEnv();
13Object.assign(process.env, repoEnv);
14​
15const port = Number(process.env.PORT ?? 5733);
16const host = process.env.HOST?.trim() || "localhost";
17const configuredWsUrl = process.env.VITE_WS_URL?.trim();
18const configuredRelayUrl = repoEnv.VITE_T3CODE_RELAY_URL?.trim() || "";
19const configuredClerkPublishableKey = repoEnv.VITE_CLERK_PUBLISHABLE_KEY?.trim() || "";
20const configuredClerkJwtTemplate = repoEnv.VITE_CLERK_JWT_TEMPLATE?.trim() || "";
21const configuredRelayTracingUrl = repoEnv.VITE_RELAY_OTLP_TRACES_URL?.trim() || "";
22const configuredRelayTracingDataset = repoEnv.VITE_RELAY_OTLP_TRACES_DATASET?.trim() || "";
23const configuredRelayTracingToken = repoEnv.VITE_RELAY_OTLP_TRACES_TOKEN?.trim() || "";
24const configuredHostedAppChannel = process.env.VITE_HOSTED_APP_CHANNEL?.trim() || "";
25const configuredAppVersion = process.env.APP_VERSION?.trim() || pkg.version;
26const configuredHostedAppUrl = (() => {
27 const explicitHostedAppUrl = process.env.VITE_HOSTED_APP_URL?.trim();
28 if (explicitHostedAppUrl) {
29 return explicitHostedAppUrl;
30 }
31 if (process.env.VERCEL_ENV === "production" && process.env.VERCEL_PROJECT_PRODUCTION_URL) {
32 return `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}`;
33 }
34 if (process.env.VERCEL_URL) {
35 return `https://${process.env.VERCEL_URL}`;
36 }
37 return undefined;
38})();
39const sourcemapEnv = process.env.T3CODE_WEB_SOURCEMAP?.trim().toLowerCase();
40​
41const buildSourcemap: boolean | "hidden" =
42 sourcemapEnv === "0" || sourcemapEnv === "false"
43 ? false
44 : sourcemapEnv === "hidden"
45 ? "hidden"
46 : true;
47​
48const unitTestProject = {
49 extends: true,
50 test: {
51 name: "unit",
52 include: ["src/**/*.test.{ts,tsx}"],
53 // The web runtime suite exercises auth bootstrap, saved environments,
54 // and websocket subscription lifecycles. Under the full monorepo test
55 // run, those async tests can exceed Vitest's default 5s budget.
56 hookTimeout: 15_000,
57 testTimeout: 15_000,
58 },
59} satisfies TestProjectInlineConfiguration;
60​
61function resolveDevProxyTarget(wsUrl: string | undefined): string | undefined {
62 if (!wsUrl) {
63 return undefined;
64 }
65​
66 try {
67 const url = new URL(wsUrl);
68 if (url.protocol === "ws:") {
69 url.protocol = "http:";
70 } else if (url.protocol === "wss:") {
71 url.protocol = "https:";
72 }
73 url.pathname = "";
74 url.search = "";
75 url.hash = "";
76 return url.toString();
77 } catch {
78 return undefined;
79 }
81​
82const devProxyTarget = resolveDevProxyTarget(configuredWsUrl);
83​
84export default defineConfig(() => {
85 return {
86 plugins: [
87 tanstackRouter(),
88 react(),
89 babel({
90 // We need to be explicit about the parser options after moving to @vitejs/plugin-react v6.0.0
91 // This is because the babel plugin only automatically parses typescript and jsx based on relative paths (e.g. "**/*.ts")
92 // whereas the previous version of the plugin parsed all files with a .ts extension.
93 // This is causing our packages/ directory to fail to parse, as they are not relative to the CWD.
94 parserOpts: { plugins: ["typescript", "jsx"] },
95 presets: [reactCompilerPreset()],
96 }),
97 tailwindcss(),
98 ],
99 optimizeDeps: {
100 include: [
101 "@clerk/clerk-js",
102 "@clerk/react/internal",
103 "@pierre/diffs",
104 "@pierre/diffs/editor",
105 "@pierre/diffs/react",
106 "@pierre/diffs/worker/worker.js",
107 "effect/Array",
108 "effect/Order",
109 "react-dom/client",
110 ],
111 },
112 define: {
113 // In dev mode, tell the web app where the WebSocket server lives
114 "import.meta.env.VITE_WS_URL": JSON.stringify(configuredWsUrl ?? ""),
115 "import.meta.env.VITE_T3CODE_RELAY_URL": JSON.stringify(configuredRelayUrl),
116 "import.meta.env.VITE_CLERK_PUBLISHABLE_KEY": JSON.stringify(configuredClerkPublishableKey),
117 "import.meta.env.VITE_CLERK_JWT_TEMPLATE": JSON.stringify(configuredClerkJwtTemplate),
118 "import.meta.env.VITE_RELAY_OTLP_TRACES_URL": JSON.stringify(configuredRelayTracingUrl),
119 "import.meta.env.VITE_RELAY_OTLP_TRACES_DATASET": JSON.stringify(
120 configuredRelayTracingDataset,
121 ),
122 "import.meta.env.VITE_RELAY_OTLP_TRACES_TOKEN": JSON.stringify(configuredRelayTracingToken),
123 "import.meta.env.VITE_HOSTED_APP_URL": JSON.stringify(configuredHostedAppUrl ?? ""),
124 "import.meta.env.VITE_HOSTED_APP_CHANNEL": JSON.stringify(configuredHostedAppChannel),
125 "import.meta.env.APP_VERSION": JSON.stringify(configuredAppVersion),
126 },
127 resolve: {
128 tsconfigPaths: true,
129 dedupe: ["react", "react-dom"],
130 },
131 server: {
132 host,
133 port,
134 strictPort: true,
135 ...(devProxyTarget
136 ? {
137 proxy: {
138 "/.well-known": {
139 target: devProxyTarget,
140 changeOrigin: true,
141 },
142 "/api": {
143 target: devProxyTarget,
144 changeOrigin: true,
145 },
146 "/attachments": {
147 target: devProxyTarget,
148 changeOrigin: true,
149 },
150 },
151 }
152 : {}),
153 hmr: {
154 // Explicit config so Vite's HMR WebSocket connects reliably
155 // inside Electron's BrowserWindow. Vite 8 uses console.debug for
156 // connection logs — enable "Verbose" in DevTools to see them.
157 protocol: "ws",
158 host,
159 clientPort: port,
160 },
161 },
162 build: {
163 outDir: "dist",
164 emptyOutDir: true,
165 sourcemap: buildSourcemap,
166 },
167 test: {
168 projects: [defineProject(unitTestProject)],
169 },
170 };
171});