Files
zeekayandHanzo Dev f44b2255c3 Ship dev.wasm inside the server bundle
The pack build inlines the module in place of devWasmBytes.ts, so the
bundle, the single executable, the desktop app and hosted web carry it and
resolve no repo-relative path. A test builds DevWasm.ts with the real plugin,
moves the bundle where no repo is beside it, and decides.

Co-authored-by: Hanzo Dev <[email protected]>
2026-11-21 02:08:11 -08:00

177 lines
7.6 KiB
TypeScript

import "vite-plus/test/config";
// @effect-diagnostics-next-line nodeBuiltinImport:off -- tsdown needs the asset path when it reads this config.
import * as NodePath from "node:path";
import { defineConfig, mergeConfig } from "vite-plus";
import baseConfig from "../../vite.config.ts";
import { devWasmPlugin } from "../../scripts/lib/dev-wasm-plugin.ts";
import { loadRepoEnv } from "../../scripts/lib/public-config.ts";
import { resolveTaggedSourceVersion } from "../../scripts/lib/tagged-source-version.ts";
import rootPackageJson from "../../package.json" with { type: "json" };
import packageJson from "./package.json" with { type: "json" };
import { WeightedShardSequencer } from "./src/testUtils/weightedShardSequencer.ts";
// NSIS install time tracks file count, not bytes.
import {
isExternalCliDependency,
shouldBundleCliDependency,
} from "../../scripts/lib/cli-external-packages.ts";
export { shouldBundleCliDependency };
const repoEnv = loadRepoEnv();
const serverManifestPath = NodePath.join(import.meta.dirname, "package.json");
// The desktop packager passes APP_VERSION; it can equal the manifest at a commit tagged otherwise.
const buildVersion =
process.env.APP_VERSION?.trim() ||
resolveTaggedSourceVersion(NodePath.join(import.meta.dirname, "../.."), packageJson.version);
const cliBuildChannel = /^[^-+]+-(?:nightly|preview)\./.test(buildVersion) ? "nightly" : "latest";
// Official release workflows set the telemetry channel; local builds and forks use "dev".
const TELEMETRY_CHANNELS = ["stable", "nightly", "preview", "dev"];
const telemetryChannel = process.env.HANZO_DEV_TELEMETRY_CHANNEL?.trim() || "dev";
if (!TELEMETRY_CHANNELS.includes(telemetryChannel)) {
throw new Error(
`HANZO_DEV_TELEMETRY_CHANNEL must be one of ${TELEMETRY_CHANNELS.join(", ")}, got "${telemetryChannel}".`,
);
}
// tsdown SEA needs one chunk (sourcemaps count) and Node >=25.7, so package separately.
// "bundle" emits the embedded CLI. "1" builds dist-exe/hanzo from the bootstrap.
// TEMPORARY one-time Node fix. Remove when fixed: https://github.com/nodejs/node/issues/67048
// "bootstrap" stops at bin.mjs for scripts/cli.ts to inject (linux-arm64).
const packMode = process.env.HANZO_DEV_PACK_EXE;
const packBootstrap = packMode === "1" || packMode === "bootstrap";
const packExecutable = packBootstrap || packMode === "bundle";
const cliAssetDir = process.env.HANZO_DEV_CLI_ASSET_DIR ?? "";
if (packExecutable && !cliAssetDir) throw new Error("Build the executable with build:exe.");
// TEMPORARY one-time Node fix. Remove when fixed: https://github.com/nodejs/node/issues/67048
// No linux-arm64: --build-sea output cannot load on 16/64 KiB page kernels, so it builds natively.
const SEA_TARGETS = {
"darwin-arm64": { platform: "darwin", arch: "arm64" },
"darwin-x64": { platform: "darwin", arch: "x64" },
"linux-x64": { platform: "linux", arch: "x64" },
"win-arm64": { platform: "win", arch: "arm64" },
"win-x64": { platform: "win", arch: "x64" },
} as const;
const packExecutableTarget = process.env.HANZO_DEV_PACK_EXE_TARGET?.trim();
const cliNodeVersion = rootPackageJson.engines.node;
if (packExecutable && !packExecutableTarget && process.versions.node !== cliNodeVersion) {
throw new Error(
`Native CLI executables must be built with Node ${cliNodeVersion}; running ${process.versions.node}.`,
);
}
if (packExecutableTarget && !Object.hasOwn(SEA_TARGETS, packExecutableTarget)) {
throw new Error(
`HANZO_DEV_PACK_EXE_TARGET must be one of ${Object.keys(SEA_TARGETS).join(", ")}, got "${packExecutableTarget}".`,
);
}
const packExecutableTargets = packExecutableTarget
? [
{
...SEA_TARGETS[packExecutableTarget as keyof typeof SEA_TARGETS],
nodeVersion: cliNodeVersion,
},
]
: undefined;
// The root config's `projects` route repo-root runs to package configs,
// including this one. Inheriting them would make this config a container.
const { projects: _rootTestProjects, ...baseTestConfig } = baseConfig.test ?? {};
// Workspace cache configuration is invalid on a package config.
const { run: _rootRunConfig, ...basePackageConfig } = baseConfig;
export default mergeConfig(
{ ...basePackageConfig, test: baseTestConfig },
defineConfig({
run: {
tasks: {
build: {
command: "node scripts/cli.ts build",
dependsOn: ["@hanzodev/web#build"],
cache: false,
},
},
},
pack: {
plugins: [
devWasmPlugin,
{
// Every server module reads its version from this manifest import.
name: "hanzodev:build-version",
load(id) {
if (NodePath.resolve(id) !== serverManifestPath) return;
return {
code: JSON.stringify({ ...packageJson, version: buildVersion }),
moduleType: "json",
};
},
},
],
// SEA embeds one entry; expose the history worker as a hidden subcommand.
entry: packBootstrap
? ["src/bin.ts"]
: packExecutable
? ["src/binMain.ts"]
: ["src/bin.ts", "src/claude-history-worker.ts"],
outDir: packBootstrap ? "dist-exe" : packExecutable ? cliAssetDir : "dist",
sourcemap: !packExecutable,
clean: true,
// SEA can only resolve built-ins; keep dynamic imports inside each emitted module.
...(packExecutable ? { outputOptions: { codeSplitting: false } } : {}),
...(packMode === "1"
? {
exe: {
fileName: "hanzo",
outDir: "dist-exe",
...(packExecutableTargets ? { targets: packExecutableTargets } : {}),
// Node SEA useCodeCache breaks import(), which the server needs.
seaConfig: {
useCodeCache: false,
// The bootstrap loads the embedded CLI with require(esm).
execArgv: ["--require-module"],
assets: { "binMain.mjs": NodePath.join(cliAssetDir, "binMain.mjs") },
},
},
}
: {}),
// jsonc-parser's UMD main requires its own files at runtime, which the bundle cannot follow.
alias: { "jsonc-parser": "jsonc-parser/lib/esm/main.js" },
deps: {
// alwaysBundle=false has no opinion; neverBundle must retain transitive native acceleration.
alwaysBundle: shouldBundleCliDependency,
neverBundle: (id: string) =>
(packBootstrap && id === "./binMain.ts") || isExternalCliDependency(id),
onlyBundle: false,
},
banner: {
js: "#!/usr/bin/env node\n",
},
define: {
__HANZO_DEV_BUILD_CHANNEL__: JSON.stringify(cliBuildChannel),
__HANZO_DEV_TELEMETRY_CHANNEL__: JSON.stringify(telemetryChannel),
__HANZO_DEV_BUILD_RELAY_URL__: JSON.stringify(repoEnv.HANZO_DEV_RELAY_URL?.trim() ?? ""),
__HANZO_DEV_BUILD_RELAY_CLIENT_OTLP_TRACES_URL__: JSON.stringify(
repoEnv.HANZO_DEV_RELAY_CLIENT_OTLP_TRACES_URL?.trim() ?? "",
),
__HANZO_DEV_BUILD_RELAY_CLIENT_OTLP_TRACES_DATASET__: JSON.stringify(
repoEnv.HANZO_DEV_RELAY_CLIENT_OTLP_TRACES_DATASET?.trim() ?? "",
),
__HANZO_DEV_BUILD_RELAY_CLIENT_OTLP_TRACES_TOKEN__: JSON.stringify(
repoEnv.HANZO_DEV_RELAY_CLIENT_OTLP_TRACES_TOKEN?.trim() ?? "",
),
},
},
test: {
// Parallel sqlite/git/worktree/orchestration tests introduce load-sensitive flakes.
fileParallelism: false,
sequence: { sequencer: WeightedShardSequencer },
setupFiles: [
"./src/testUtils/gitConfig.setup.ts",
"./src/testUtils/managedServiceEnv.setup.ts",
],
hookTimeout: 120_000,
testTimeout: 120_000,
},
}),
);