Open History to see the last commit
1import "vite-plus/test/config";
2// @effect-diagnostics-next-line nodeBuiltinImport:off -- tsdown needs the asset path when it reads this config.
3import * as NodePath from "node:path";
4import { defineConfig, mergeConfig } from "vite-plus";
5
6import baseConfig from "../../vite.config.ts";
7import { loadRepoEnv } from "../../scripts/lib/public-config.ts";
8import { resolveTaggedSourceVersion } from "../../scripts/lib/tagged-source-version.ts";
9import rootPackageJson from "../../package.json" with { type: "json" };
10import packageJson from "./package.json" with { type: "json" };
11import { WeightedShardSequencer } from "./src/testUtils/weightedShardSequencer.ts";
12
13// NSIS install time tracks file count, not bytes.
14import { cliAssets } from "../../scripts/lib/cli-assets.ts";
15import {
16 isExternalCliDependency,
17 shouldBundleCliDependency,
18} from "../../scripts/lib/cli-external-packages.ts";
19
20export { shouldBundleCliDependency };
21
22const repoEnv = loadRepoEnv();
23const serverManifestPath = NodePath.join(import.meta.dirname, "package.json");
24// The desktop packager passes APP_VERSION; it can equal the manifest at a commit tagged otherwise.
25const buildVersion =
26 process.env.APP_VERSION?.trim() ||
27 resolveTaggedSourceVersion(NodePath.join(import.meta.dirname, "../.."), packageJson.version);
28const cliBuildChannel = /^[^-+]+-(?:nightly|preview)\./.test(buildVersion) ? "nightly" : "latest";
29// Official release workflows set the telemetry channel; local builds and forks use "dev".
30const TELEMETRY_CHANNELS = ["stable", "nightly", "preview", "dev"];
31const telemetryChannel = process.env.T3CODE_TELEMETRY_CHANNEL?.trim() || "dev";
32if (!TELEMETRY_CHANNELS.includes(telemetryChannel)) {
33 throw new Error(
34 `T3CODE_TELEMETRY_CHANNEL must be one of ${TELEMETRY_CHANNELS.join(", ")}, got "${telemetryChannel}".`,
35 );
36}
37
38// tsdown SEA needs one chunk (sourcemaps count) and Node >=25.7, so package separately.
39// "bundle" emits the embedded CLI. "1" builds dist-exe/t3 from the bootstrap.
40// TEMPORARY one-time Node fix. Remove when fixed: https://github.com/nodejs/node/issues/67048
41// "bootstrap" stops at bin.mjs for scripts/cli.ts to inject (linux-arm64).
42const packMode = process.env.T3CODE_PACK_EXE;
43const packBootstrap = packMode === "1" || packMode === "bootstrap";
44const packExecutable = packBootstrap || packMode === "bundle";
45const cliAssetDir = process.env.T3CODE_CLI_ASSET_DIR ?? "";
46if (packExecutable && !cliAssetDir) throw new Error("Build the executable with build:exe.");
47// TEMPORARY one-time Node fix. Remove when fixed: https://github.com/nodejs/node/issues/67048
48// No linux-arm64: --build-sea output cannot load on 16/64 KiB page kernels, so it builds natively.
49const SEA_TARGETS = {
50 "darwin-arm64": { platform: "darwin", arch: "arm64" },
51 "darwin-x64": { platform: "darwin", arch: "x64" },
52 "linux-x64": { platform: "linux", arch: "x64" },
53 "win-arm64": { platform: "win", arch: "arm64" },
54 "win-x64": { platform: "win", arch: "x64" },
55} as const;
56const packExecutableTarget = process.env.T3CODE_PACK_EXE_TARGET?.trim();
57const cliNodeVersion = rootPackageJson.engines.node;
58if (packExecutable && !packExecutableTarget && process.versions.node !== cliNodeVersion) {
59 throw new Error(
60 `Native CLI executables must be built with Node ${cliNodeVersion}; running ${process.versions.node}.`,
61 );
62}
63if (packExecutableTarget && !Object.hasOwn(SEA_TARGETS, packExecutableTarget)) {
64 throw new Error(
65 `T3CODE_PACK_EXE_TARGET must be one of ${Object.keys(SEA_TARGETS).join(", ")}, got "${packExecutableTarget}".`,
66 );
67}
68const packExecutableTargets = packExecutableTarget
69 ? [
70 {
71 ...SEA_TARGETS[packExecutableTarget as keyof typeof SEA_TARGETS],
72 nodeVersion: cliNodeVersion,
73 },
74 ]
75 : undefined;
76
77// The root config's `projects` route repo-root runs to package configs,
78// including this one. Inheriting them would make this config a container.
79const { projects: _rootTestProjects, ...baseTestConfig } = baseConfig.test ?? {};
80// Workspace cache configuration is invalid on a package config.
81const { run: _rootRunConfig, ...basePackageConfig } = baseConfig;
82
83export default mergeConfig(
84 { ...basePackageConfig, test: baseTestConfig },
85 defineConfig({
86 run: {
87 tasks: {
88 build: {
89 command: "node scripts/cli.ts build",
90 dependsOn: ["@t3tools/web#build"],
91 cache: false,
92 },
93 // A local tsbuildinfo is leftover build state, not a typecheck input.
94 typecheck: {
95 command: "effect-tsgo tsrs --noEmit",
96 cache: { input: [{ auto: true }, "!*.tsbuildinfo"] },
97 },
98 },
99 },
100 pack: {
101 plugins: [
102 {
103 // Every server module reads its version from this manifest import.
104 name: "t3code:build-version",
105 load(id) {
106 if (NodePath.resolve(id) !== serverManifestPath) return;
107 return {
108 code: JSON.stringify({ ...packageJson, version: buildVersion }),
109 moduleType: "json",
110 };
111 },
112 },
113 ],
114 // SEA embeds one entry; expose the workers as hidden subcommands.
115 entry: packBootstrap
116 ? ["src/bin.ts"]
117 : packExecutable
118 ? ["src/binMain.ts"]
119 : ["src/bin.ts", "src/claude-history-worker.ts"],
120 outDir: packBootstrap ? "dist-exe" : packExecutable ? cliAssetDir : "dist",
121 sourcemap: !packExecutable,
122 clean: true,
123 // The SEA main can only resolve built-ins; bin.ts serves the CLI's chunks from SEA assets.
124 ...(packBootstrap ? { outputOptions: { codeSplitting: false } } : {}),
125 ...(packMode === "1"
126 ? {
127 exe: {
128 fileName: "t3",
129 outDir: "dist-exe",
130 ...(packExecutableTargets ? { targets: packExecutableTargets } : {}),
131 // Node SEA useCodeCache breaks import(), which the server needs.
132 seaConfig: {
133 useCodeCache: false,
134 // The bootstrap loads the embedded CLI with require(esm).
135 execArgv: ["--require-module"],
136 assets: cliAssets(cliAssetDir),
137 },
138 },
139 }
140 : {}),
141 // jsonc-parser's UMD main requires its own files at runtime, which the bundle cannot follow.
142 alias: { "jsonc-parser": "jsonc-parser/lib/esm/main.js" },
143 deps: {
144 // alwaysBundle=false has no opinion; neverBundle must retain transitive native acceleration.
145 alwaysBundle: shouldBundleCliDependency,
146 neverBundle: (id: string) =>
147 (packBootstrap && id === "./binMain.ts") || isExternalCliDependency(id),
148 onlyBundle: false,
149 },
150 banner: {
151 js: "#!/usr/bin/env node\n",
152 },
153 define: {
154 __T3CODE_BUILD_CHANNEL__: JSON.stringify(cliBuildChannel),
155 __T3CODE_TELEMETRY_CHANNEL__: JSON.stringify(telemetryChannel),
156 __T3CODE_BUILD_RELAY_URL__: JSON.stringify(repoEnv.T3CODE_RELAY_URL?.trim() ?? ""),
157 __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: JSON.stringify(
158 repoEnv.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() ?? "",
159 ),
160 __T3CODE_BUILD_CLERK_CLI_OAUTH_CLIENT_ID__: JSON.stringify(
161 repoEnv.T3CODE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() ?? "",
162 ),
163 __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__: JSON.stringify(
164 repoEnv.T3CODE_RELAY_CLIENT_OTLP_TRACES_URL?.trim() ?? "",
165 ),
166 __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__: JSON.stringify(
167 repoEnv.T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET?.trim() ?? "",
168 ),
169 __T3CODE_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__: JSON.stringify(
170 repoEnv.T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN?.trim() ?? "",
171 ),
172 },
173 },
174 test: {
175 // Parallel sqlite/git/worktree/orchestration tests introduce load-sensitive flakes.
176 fileParallelism: false,
177 sequence: { sequencer: WeightedShardSequencer },
178 setupFiles: [
179 "./src/testUtils/gitConfig.setup.ts",
180 "./src/testUtils/managedServiceEnv.setup.ts",
181 ],
182 hookTimeout: 120_000,
183 testTimeout: 120_000,
184 },
185 }),
186);