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

vite.config.ts

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