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

vite.config.ts

146 lines6.0 KB

1import "vite-plus/test/config";
2import { defineConfig, mergeConfig } from "vite-plus";
3​
4import baseConfig from "../../vite.config.ts";
5import { loadRepoEnv } from "../../scripts/lib/public-config.ts";
6import rootPackageJson from "../../package.json" with { type: "json" };
7import packageJson from "./package.json" with { type: "json" };
8import { WeightedShardSequencer } from "./src/testUtils/weightedShardSequencer.ts";
9​
10// NSIS install time tracks file count, not bytes.
11import {
12 isExternalCliDependency,
13 shouldBundleCliDependency,
14} from "../../scripts/lib/cli-external-packages.ts";
15​
16export { shouldBundleCliDependency };
17​
18const repoEnv = loadRepoEnv();
19const cliBuildChannel = /^[^-+]+-(?:nightly|preview)\./.test(packageJson.version)
20 ? "nightly"
21 : "latest";
22// Sent with every analytics event. Release workflows set it for pingdotgg/t3code
23// builds only, so local builds and forks report "dev". A typo fails the build
24// instead of mislabeling events.
25const TELEMETRY_CHANNELS = ["stable", "nightly", "preview", "dev"];
26const telemetryChannel = process.env.T3CODE_TELEMETRY_CHANNEL?.trim() || "dev";
27if (!TELEMETRY_CHANNELS.includes(telemetryChannel)) {
28 throw new Error(
29 `T3CODE_TELEMETRY_CHANNEL must be one of ${TELEMETRY_CHANNELS.join(", ")}, got "${telemetryChannel}".`,
30 );
32​
33// `build:exe` wraps the same bundle in a Node single-executable. tsdown's exe
34// step refuses multi-chunk output and counts the sourcemap as a chunk, and the
35// executable needs a host Node that supports `--build-sea` (25.7+), so this is
36// a separate mode rather than a second entry in the default build.
37const packExecutable = process.env.T3CODE_PACK_EXE === "1";
38const SEA_TARGETS = {
39 "darwin-arm64": { platform: "darwin", arch: "arm64" },
40 "darwin-x64": { platform: "darwin", arch: "x64" },
41 "linux-arm64": { platform: "linux", arch: "arm64" },
42 "linux-x64": { platform: "linux", arch: "x64" },
43 "win-arm64": { platform: "win", arch: "arm64" },
44 "win-x64": { platform: "win", arch: "x64" },
45} as const;
46const packExecutableTarget = process.env.T3CODE_PACK_EXE_TARGET?.trim();
47const cliNodeVersion = rootPackageJson.engines.node;
48if (packExecutable && !packExecutableTarget && process.versions.node !== cliNodeVersion) {
49 throw new Error(
50 `Native CLI executables must be built with Node ${cliNodeVersion}; running ${process.versions.node}.`,
51 );
53if (packExecutableTarget && !Object.hasOwn(SEA_TARGETS, packExecutableTarget)) {
54 throw new Error(
55 `T3CODE_PACK_EXE_TARGET must be one of ${Object.keys(SEA_TARGETS).join(", ")}, got "${packExecutableTarget}".`,
56 );
58const packExecutableTargets = packExecutableTarget
59 ? [
60 {
61 ...SEA_TARGETS[packExecutableTarget as keyof typeof SEA_TARGETS],
62 nodeVersion: cliNodeVersion,
63 },
64 ]
65 : undefined;
66​
67// The root config's `projects` route repo-root runs to package configs,
68// including this one. Inheriting them would make this config a container.
69const { projects: _rootTestProjects, ...baseTestConfig } = baseConfig.test ?? {};
70​
71export default mergeConfig(
72 { ...baseConfig, test: baseTestConfig },
73 defineConfig({
74 run: {
75 tasks: {
76 build: {
77 command: "node scripts/cli.ts build",
78 dependsOn: ["@t3tools/web#build"],
79 cache: false,
80 },
81 },
82 },
83 pack: {
84 // The executable embeds one entry; the history worker becomes a hidden
85 // subcommand there instead of a sibling script.
86 entry: packExecutable ? ["src/bin.ts"] : ["src/bin.ts", "src/claude-history-worker.ts"],
87 outDir: packExecutable ? "dist-exe" : "dist",
88 sourcemap: !packExecutable,
89 clean: true,
90 ...(packExecutable
91 ? {
92 exe: {
93 fileName: "t3",
94 outDir: "dist-exe",
95 ...(packExecutableTargets ? { targets: packExecutableTargets } : {}),
96 // Node's SEA docs: `import()` does not work when useCodeCache is
97 // true, and the server reaches several modules that way.
98 seaConfig: { useCodeCache: false },
99 },
100 }
101 : {}),
102 deps: {
103 // `neverBundle` forces the native packages out: returning false from
104 // `alwaysBundle` only means "no opinion", so a transitive dependency
105 // would still be bundled — which silently inlined native loaders such
106 // as node-gyp-build, losing native acceleration.
107 alwaysBundle: shouldBundleCliDependency,
108 neverBundle: (id: string) => isExternalCliDependency(id),
109 onlyBundle: false,
110 },
111 banner: {
112 js: "#!/usr/bin/env node\n",
113 },
114 define: {
115 __T3CODE_BUILD_CHANNEL__: JSON.stringify(cliBuildChannel),
116 __T3CODE_TELEMETRY_CHANNEL__: JSON.stringify(telemetryChannel),
117 __T3CODE_BUILD_RELAY_URL__: JSON.stringify(repoEnv.T3CODE_RELAY_URL?.trim() ?? ""),
118 __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: JSON.stringify(
119 repoEnv.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() ?? "",
120 ),
121 __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__: JSON.stringify(
122 repoEnv.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() ?? "",
123 ),
124 __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__: JSON.stringify(
125 repoEnv.T3CODE_RELAY_CLIENT_OTLP_TRACES_URL?.trim() ?? "",
126 ),
127 __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__: JSON.stringify(
128 repoEnv.T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET?.trim() ?? "",
129 ),
130 __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__: JSON.stringify(
131 repoEnv.T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN?.trim() ?? "",
132 ),
133 },
134 },
135 test: {
136 // The server suite exercises sqlite, git, temp worktrees, and orchestration
137 // runtimes heavily. Running files in parallel introduces load-sensitive flakes.
138 fileParallelism: false,
139 sequence: { sequencer: WeightedShardSequencer },
140 // Appended to the root setup, which mergeConfig concatenates.
141 setupFiles: ["./src/testUtils/gitConfig.setup.ts"],
142 hookTimeout: 120_000,
143 testTimeout: 120_000,
144 },
145 }),
146);