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

vite.config.ts

221 lines7.7 KB

1import "vite-plus/test/config";
2import * as NodeOS from "node:os";
3​
4import { defaultInclude, defineConfig } from "vite-plus";
5import { Rolldown } from "vite-plus/pack";
6​
7import { isDesktopRuntimeExternalDependency } from "../../scripts/lib/desktop-external-packages.ts";
8import { loadRepoEnv } from "../../scripts/lib/public-config.ts";
9​
10const repoEnv = loadRepoEnv();
11const domTests = ["src/preview/PickPreload.test.ts"];
12​
13// Install only bundle externals; the server bundle already carries its dependency tree.
14const isMainProcessExternal = (id: string) =>
15 id === "electron" || id.startsWith("electron/") || isDesktopRuntimeExternalDependency(id);
16const shouldLaunchElectronAfterPack = process.env.T3CODE_DESKTOP_DEV === "1";
17// tsdown runs onSuccess through a shell. dash forks instead of exec'ing the last command, which
18// would leave an `sh` between `vp pack` and the supervisor and hide `vp pack` dying from it.
19const devElectronCommand =
20 // oxlint-disable-next-line t3code/no-global-process-runtime -- Build config has no Effect runtime.
21 NodeOS.platform() === "win32"
22 ? "node scripts/dev-electron.mjs"
23 : "exec node scripts/dev-electron.mjs";
24const publicConfigDefine = {
25 __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: JSON.stringify(
26 repoEnv.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() ?? "",
27 ),
28};
29​
30const PAGE_WORLD_MODULE = /[\\/]src[\\/]preview[\\/]PageWorld\.ts$/;
31​
32/**
33 * The preview preload passes `installPageWorld` to `contextBridge.executeInMainWorld`, which runs
34 * the function's source text in the page. Inline the module's imports into the function body so
35 * that text is complete on its own.
36 */
37const pageWorldFunction = (): Rolldown.Plugin => ({
38 name: "t3code:preview-page-world",
39 async load(id) {
40 if (!PAGE_WORLD_MODULE.test(id)) return null;
41 const bundle = await Rolldown.rolldown({ input: id, platform: "browser" });
42 try {
43 const { output } = await bundle.generate({ format: "iife", name: "pageWorld" });
44 for (const file of await bundle.watchFiles) this.addWatchFile(file);
45 return `export function installPageWorld(host) {\n${output[0].code}\nreturn pageWorld.installPageWorld(host);\n}\n`;
46 } finally {
47 await bundle.close();
48 }
49 },
50});
51​
52export default defineConfig({
53 run: {
54 tasks: {
55 build: {
56 command:
57 "node scripts/build-browser-secret.mjs && node scripts/build-apple-speech.mjs && node scripts/build-preview-annotation-css.mjs && vp pack",
58 dependsOn: ["t3#build"],
59 cache: false,
60 },
61 // CI bundle jobs skip the Linux browser secret helper; packaging stages one per arch.
62 "build:bundle": {
63 command: "node scripts/build-preview-annotation-css.mjs && vp pack",
64 dependsOn: ["t3#build"],
65 cache: false,
66 },
67 dev: {
68 command:
69 "node scripts/build-browser-secret.mjs && node scripts/build-apple-speech.mjs && node scripts/build-preview-annotation-css.mjs && cross-env T3CODE_DESKTOP_DEV=1 vp pack --watch",
70 dependsOn: ["t3#build"],
71 cache: false,
72 },
73 "dev:bundle": {
74 command:
75 "node scripts/build-browser-secret.mjs && node scripts/build-apple-speech.mjs && node scripts/build-preview-annotation-css.mjs && vp pack --watch",
76 cache: false,
77 },
78 "dev:electron": {
79 command: "node scripts/dev-electron.mjs",
80 dependsOn: ["t3#build"],
81 cache: false,
82 },
83 // A local tsbuildinfo is leftover build state, not a typecheck input.
84 typecheck: {
85 command: "effect-tsgo tsrs --noEmit",
86 cache: { input: [{ auto: true }, "!*.tsbuildinfo"] },
87 },
88 },
89 },
90 pack: [
91 {
92 format: "cjs",
93 outDir: "dist-electron",
94 dts: false,
95 sourcemap: true,
96 outExtensions: () => ({ js: ".cjs" }),
97 define: publicConfigDefine,
98 outputOptions: { codeSplitting: false },
99 entry: ["src/main.ts"],
100 clean: true,
101 deps: {
102 alwaysBundle: (id) => !id.startsWith("node:") && !isMainProcessExternal(id),
103 neverBundle: isMainProcessExternal,
104 onlyBundle: false,
105 },
106 ...(shouldLaunchElectronAfterPack ? { onSuccess: devElectronCommand } : {}),
107 },
108 {
109 format: "cjs",
110 outDir: "dist-electron",
111 dts: false,
112 sourcemap: true,
113 outExtensions: () => ({ js: ".cjs" }),
114 define: publicConfigDefine,
115 entry: [
116 "src/backend/DesktopBackendRecoveryWorker.ts",
117 "src/electron/WindowsForegroundFocusWorker.ts",
118 "src/snapShot/GlobalShiftShortcutWorker.ts",
119 "src/snapShot/RegionSnapShotWorker.ts",
120 "src/snapShot/SnapShotAccessibilityWorker.ts",
121 ],
122 clean: false,
123 deps: {
124 alwaysBundle: (id) => !id.startsWith("node:") && !isMainProcessExternal(id),
125 neverBundle: isMainProcessExternal,
126 onlyBundle: false,
127 },
128 },
129 {
130 // boot.cjs requires the other two at runtime, so all three stay separate files.
131 format: "cjs",
132 outDir: "dist-electron",
133 dts: false,
134 sourcemap: true,
135 outExtensions: () => ({ js: ".cjs" }),
136 entry: ["src/boot.ts", "src/compileCache.ts", "src/serverCompileCache.ts"],
137 clean: false,
138 deps: {
139 neverBundle: (id) => id === "./main.cjs" || id === "./compileCache.cjs",
140 },
141 },
142 {
143 format: "cjs",
144 outDir: "dist-electron",
145 dts: false,
146 sourcemap: true,
147 outExtensions: () => ({ js: ".cjs" }),
148 define: publicConfigDefine,
149 entry: ["src/preload.ts"],
150 deps: {
151 // Sandboxed preloads cannot resolve imports inside ASAR; bundle Clerk's bridge.
152 alwaysBundle: (id) => id === "@clerk/electron" || id.startsWith("@clerk/electron/"),
153 },
154 },
155 {
156 format: "cjs",
157 outDir: "dist-electron",
158 dts: false,
159 sourcemap: true,
160 outExtensions: () => ({ js: ".cjs" }),
161 entry: ["src/preview-pick-preload.ts"],
162 plugins: [pageWorldFunction()],
163 deps: {
164 alwaysBundle: (id) => id === "@t3tools/shared/keybindingMatching",
165 },
166 },
167 {
168 format: "cjs",
169 outDir: "dist-electron",
170 dts: false,
171 sourcemap: true,
172 outExtensions: () => ({ js: ".cjs" }),
173 entry: ["src/preview-pip-preload.ts"],
174 },
175 {
176 // Sandboxed preloads must be self-contained, without shared runtime chunks.
177 format: "cjs",
178 outDir: "dist-electron",
179 dts: false,
180 sourcemap: true,
181 outExtensions: () => ({ js: ".cjs" }),
182 entry: ["src/mac-permission-preload.ts"],
183 },
184 ],
185 test: {
186 // The Windows lane runs workspace suites concurrently; filesystem-heavy
187 // desktop integration tests can exceed Vitest's 5 second default there.
188 testTimeout: 15_000,
189 // `require("electron")` downloads a missing binary, so concurrent workers race to
190 // extract it into the same dist/. Install it once before any worker starts.
191 globalSetup: ["./scripts/ensure-electron-runtime.mjs"],
192 projects: [
193 {
194 extends: true,
195 test: {
196 name: "unit",
197 include: [...defaultInclude, "../../native/kde-snap-shot/src/*.test.ts"],
198 exclude: ["**/node_modules/**", ...domTests],
199 setupFiles: [
200 "../../packages/shared/src/testing/longTempDir.ts",
201 "../../packages/shared/src/testing/providerEnv.ts",
202 "../../packages/shared/src/testing/gitEnv.ts",
203 ],
204 },
205 },
206 {
207 extends: true,
208 test: {
209 name: "dom",
210 include: domTests,
211 // Guest preloads run in the page; these tests drive them against a DOM.
212 environment: "jsdom",
213 setupFiles: [
214 "../../packages/shared/src/testing/providerEnv.ts",
215 "../../packages/shared/src/testing/gitEnv.ts",
216 ],
217 },
218 },
219 ],
220 },
221});