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