mirror of
https://github.com/hanzoai/dev-desktop.git
synced 2026-11-20 01:07:23 +00:00
456 lines
17 KiB
TypeScript
456 lines
17 KiB
TypeScript
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Vite config runs before any Effect runtime.
|
|
import * as NodeFSP from "node:fs/promises";
|
|
import * as NodeZlib from "node:zlib";
|
|
|
|
import react from "@vitejs/plugin-react";
|
|
import { tanstackRouter } from "@tanstack/router-plugin/vite";
|
|
import compression from "compression";
|
|
import { playwright } from "vite-plus/test/browser-playwright";
|
|
import { defineProject, type TestProjectInlineConfiguration } from "vite-plus/test/config";
|
|
import "vite-plus/test/config";
|
|
import { defineConfig, type Connect, type Plugin } from "vite-plus";
|
|
import * as Hash from "effect/Hash";
|
|
import pkg from "./package.json" with { type: "json" };
|
|
import pdfjsPkg from "pdfjs-dist/package.json" with { type: "json" };
|
|
|
|
import {
|
|
DEV_PROXIED_ORIGIN_PRESERVING_PREFIXES,
|
|
DEV_PROXIED_PATH_PREFIXES,
|
|
} from "@hanzodev/shared/devProxy";
|
|
|
|
import { loadRepoEnv } from "../../scripts/lib/public-config";
|
|
import { resolveTaggedSourceVersion } from "../../scripts/lib/tagged-source-version";
|
|
import { thirdPartyLicensesPlugin } from "../../scripts/lib/third-party-licenses";
|
|
import { tailwindPlugins } from "./vite/tailwind";
|
|
import { warmBundledDevPlugin } from "./vite/warmBundledDev";
|
|
import { formatAppDisplayName, resolveWebAppStageLabel } from "./src/branding.logic";
|
|
|
|
const repoEnv = loadRepoEnv();
|
|
Object.assign(process.env, repoEnv);
|
|
|
|
// Signal single-origin dev explicitly; loadRepoEnv can restore deleted URL keys from .env.
|
|
const isSingleOriginDev = process.env.HANZO_DEV_SINGLE_ORIGIN_DEV === "1";
|
|
|
|
const port = Number(process.env.PORT ?? 5733);
|
|
const explicitHost = process.env.HOST?.trim();
|
|
const host = explicitHost || "127.0.0.1";
|
|
const configuredWsUrl = isSingleOriginDev ? undefined : process.env.VITE_WS_URL?.trim();
|
|
const configuredHttpUrl = isSingleOriginDev ? undefined : process.env.VITE_HTTP_URL?.trim();
|
|
const configuredRelayUrl = repoEnv.VITE_HANZO_DEV_RELAY_URL?.trim() || "";
|
|
const configuredClerkPublishableKey = repoEnv.VITE_CLERK_PUBLISHABLE_KEY?.trim() || "";
|
|
const configuredClerkJwtTemplate = repoEnv.VITE_CLERK_JWT_TEMPLATE?.trim() || "";
|
|
const configuredClerkCliOAuthClientId = repoEnv.VITE_CLERK_CLI_OAUTH_CLIENT_ID?.trim() || "";
|
|
const configuredRelayTracingUrl = repoEnv.VITE_RELAY_OTLP_TRACES_URL?.trim() || "";
|
|
const configuredRelayTracingDataset = repoEnv.VITE_RELAY_OTLP_TRACES_DATASET?.trim() || "";
|
|
const configuredRelayTracingToken = repoEnv.VITE_RELAY_OTLP_TRACES_TOKEN?.trim() || "";
|
|
const configuredHostedAppChannel = process.env.VITE_HOSTED_APP_CHANNEL?.trim() || "";
|
|
const configuredReleaseChannel =
|
|
process.env.HANZO_DEV_BUILT_DEV === "1" ? "dev" : process.env.VITE_RELEASE_CHANNEL?.trim() || "";
|
|
const configuredDesktopUpdateRepository =
|
|
process.env.HANZO_DEV_DESKTOP_UPDATE_REPOSITORY?.trim() ||
|
|
process.env.GITHUB_REPOSITORY?.trim() ||
|
|
"";
|
|
const configuredFeatureSecrets = process.env.HANZO_DEV_FEATURE_SECRETS?.trim() || "";
|
|
const configuredHostedAppUrl = (() => {
|
|
const explicitHostedAppUrl = process.env.VITE_HOSTED_APP_URL?.trim();
|
|
if (explicitHostedAppUrl) {
|
|
return explicitHostedAppUrl;
|
|
}
|
|
if (process.env.VERCEL_ENV === "production" && process.env.VERCEL_PROJECT_PRODUCTION_URL) {
|
|
return `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}`;
|
|
}
|
|
if (process.env.VERCEL_URL) {
|
|
return `https://${process.env.VERCEL_URL}`;
|
|
}
|
|
return undefined;
|
|
})();
|
|
const sourcemapEnv = process.env.HANZO_DEV_WEB_SOURCEMAP?.trim().toLowerCase();
|
|
|
|
// `vp run dev --share --built` rebuilds on every change. The license manifest
|
|
// is about 2 s of each build, and nothing in that mode reads it.
|
|
const builtDev = process.env.HANZO_DEV_BUILT_DEV === "1";
|
|
|
|
const bundledDevEnv = process.env.HANZO_DEV_BUNDLED_DEV?.trim().toLowerCase();
|
|
const bundledDev = bundledDevEnv === "1" || bundledDevEnv === "true";
|
|
|
|
// Bundled dev and `build --watch` pass Rolldown `exclude` but not Vite's default `ignored`, so
|
|
// they watch node_modules. Those files are pnpm-store hardlinks shared by every worktree, so each
|
|
// `vp i` or worktree removal elsewhere rebuilds every long-running server and leaks native memory.
|
|
const rolldownWatchExclude = [/[\\/]node_modules[\\/]/];
|
|
|
|
const buildSourcemap: boolean | "hidden" =
|
|
sourcemapEnv === "0" || sourcemapEnv === "false"
|
|
? false
|
|
: sourcemapEnv === "hidden"
|
|
? "hidden"
|
|
: true;
|
|
|
|
const unitTestProject = {
|
|
extends: true,
|
|
test: {
|
|
name: "unit",
|
|
include: ["src/**/*.test.{ts,tsx}"],
|
|
exclude: ["src/**/*.browser.test.{ts,tsx}"],
|
|
hookTimeout: 15_000,
|
|
testTimeout: 15_000,
|
|
setupFiles: [
|
|
"../../packages/shared/src/testing/longTempDir.ts",
|
|
"../../packages/shared/src/testing/providerEnv.ts",
|
|
"../../packages/shared/src/testing/gitEnv.ts",
|
|
],
|
|
},
|
|
} satisfies TestProjectInlineConfiguration;
|
|
|
|
// Hash checkout paths to ports 20000-23999; Vitest's fixed port collides under inherited strictPort.
|
|
// Stay below ephemeral ranges: Windows reserves Hyper-V/WSL blocks there and EACCES is not skipped.
|
|
const browserTestApiPort = 20_000 + ((Hash.string(import.meta.dirname) >>> 0) % 4_000);
|
|
|
|
const browserTestProject = {
|
|
extends: true,
|
|
// Pre-bundle lazy browser and worker dependencies to avoid mid-run reloads.
|
|
optimizeDeps: {
|
|
include: [
|
|
"vite-plus/test/browser",
|
|
"@base-ui/react/autocomplete",
|
|
"saxes",
|
|
"@betteroffice/docx/editor",
|
|
"@betteroffice/docx/layout",
|
|
"@betteroffice/docx/layout/render",
|
|
"@betteroffice/docx/yrs",
|
|
"@betteroffice/fonts",
|
|
"@betteroffice/pptx",
|
|
"@betteroffice/xlsx",
|
|
"@betteroffice/xlsx/headless",
|
|
"fast-xml-parser",
|
|
"entities",
|
|
"dompurify",
|
|
"heic-to/csp",
|
|
"jszip",
|
|
"pdfjs-dist",
|
|
"sql.js",
|
|
"tiff",
|
|
],
|
|
},
|
|
test: {
|
|
name: "browser",
|
|
include: ["src/**/*.browser.test.{ts,tsx}"],
|
|
hookTimeout: 15_000,
|
|
testTimeout: 15_000,
|
|
setupFiles: ["./test/browserSetup.ts"],
|
|
api: { port: browserTestApiPort, strictPort: false },
|
|
browser: {
|
|
enabled: true,
|
|
headless: true,
|
|
provider: playwright(),
|
|
instances: [{ browser: "chromium" }],
|
|
screenshotFailures: false,
|
|
commands: {
|
|
// Playwright's mouse outlives each test, so whatever mounts under its last
|
|
// position starts hovered. test/browserSetup.ts parks it off the page before each test.
|
|
resetPointer: async ({ page }) => {
|
|
await page.mouse.move(-1, -1);
|
|
},
|
|
},
|
|
},
|
|
},
|
|
} satisfies TestProjectInlineConfiguration;
|
|
|
|
// Render scenarios count React commits; they run only through scripts/render-metrics.ts,
|
|
// which reads the per-scenario reports this command writes.
|
|
const renderScenariosProject = {
|
|
...browserTestProject,
|
|
test: {
|
|
...browserTestProject.test,
|
|
name: "renders",
|
|
include: ["test/*.renders.tsx"],
|
|
// App scenarios mount the whole app per test, which outlasts 15s on a loaded machine.
|
|
hookTimeout: 60_000,
|
|
testTimeout: 60_000,
|
|
setupFiles: ["./test/renderCounter.ts", ...browserTestProject.test.setupFiles],
|
|
browser: {
|
|
...browserTestProject.test.browser,
|
|
// Vitest names each instance in place, so a shared array would clash with the browser project.
|
|
instances: [{ browser: "chromium" }],
|
|
commands: {
|
|
...browserTestProject.test.browser.commands,
|
|
writeRenderReport: async (_context, scenario: string, json: string) => {
|
|
const directory = process.env.HANZO_DEV_RENDER_REPORT_DIR;
|
|
if (!directory) throw new Error("HANZO_DEV_RENDER_REPORT_DIR is not set");
|
|
await NodeFSP.writeFile(`${directory}/${scenario}.json`, json);
|
|
},
|
|
},
|
|
},
|
|
},
|
|
} satisfies TestProjectInlineConfiguration;
|
|
|
|
function resolveDevProxyTarget(
|
|
backendPort: string | undefined,
|
|
wsUrl: string | undefined,
|
|
): string | undefined {
|
|
const port = Number(backendPort?.trim());
|
|
if (Number.isInteger(port) && port > 0) {
|
|
return `http://localhost:${port}/`;
|
|
}
|
|
|
|
// dev:desktop connects directly to the backend; derive its target from the explicit WebSocket URL.
|
|
if (!wsUrl) {
|
|
return undefined;
|
|
}
|
|
|
|
try {
|
|
const url = new URL(wsUrl);
|
|
if (url.protocol === "ws:") {
|
|
url.protocol = "http:";
|
|
} else if (url.protocol === "wss:") {
|
|
url.protocol = "https:";
|
|
}
|
|
url.pathname = "";
|
|
url.search = "";
|
|
url.hash = "";
|
|
return url.toString();
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
const devProxyTarget = process.env.VITEST
|
|
? undefined
|
|
: resolveDevProxyTarget(
|
|
process.env.HANZO_DEV_PORT ?? process.env.T3CODE_PORT ?? "13773",
|
|
configuredWsUrl,
|
|
);
|
|
|
|
// Brotli quality 5 encodes in hundreds of ms; default quality 11 would replace transfer stalls with encode stalls.
|
|
function devCompressionPlugin(): Plugin {
|
|
return {
|
|
name: "hanzodev:dev-compression",
|
|
apply: "serve",
|
|
configureServer(server) {
|
|
server.middlewares.use(
|
|
compression({
|
|
brotli: { params: { [NodeZlib.constants.BROTLI_PARAM_QUALITY]: 5 } },
|
|
}) as unknown as Connect.NextHandleFunction,
|
|
);
|
|
},
|
|
};
|
|
}
|
|
|
|
// A static import cycle between chunks can run a chunk before the values it imports exist,
|
|
// which blanks the app at boot. Fail the build instead of shipping it.
|
|
function chunkCycleGuardPlugin(): Plugin {
|
|
return {
|
|
name: "hanzodev:chunk-cycle-guard",
|
|
apply: "build",
|
|
generateBundle(_options, bundle) {
|
|
const done = new Set<string>();
|
|
const visit = (fileName: string, path: string[]) => {
|
|
if (done.has(fileName)) return;
|
|
const start = path.indexOf(fileName);
|
|
if (start !== -1) {
|
|
this.error(`Chunk import cycle: ${[...path.slice(start), fileName].join(" -> ")}`);
|
|
}
|
|
const chunk = bundle[fileName];
|
|
if (chunk?.type !== "chunk") return;
|
|
for (const imported of chunk.imports) visit(imported, [...path, fileName]);
|
|
done.add(fileName);
|
|
};
|
|
for (const fileName of Object.keys(bundle)) visit(fileName, []);
|
|
},
|
|
};
|
|
}
|
|
|
|
// Bundled dev never evicts emitted files from memory (vitejs/vite#23781), so hashed names keep
|
|
// a copy of every changed chunk from every rebuild. Fixed names overwrite the previous copy.
|
|
function bundledDevStableNamesPlugin(): Plugin {
|
|
return {
|
|
name: "hanzodev:bundled-dev-stable-names",
|
|
apply: "serve",
|
|
outputOptions: (options) => ({
|
|
...options,
|
|
chunkFileNames: "assets/[name].js",
|
|
assetFileNames: "assets/[name][extname]",
|
|
}),
|
|
};
|
|
}
|
|
|
|
// Stamps index.html's title with the name branding.ts resolves once the app
|
|
// loads, so a tab opens on the right channel instead of flashing another's.
|
|
// Desktop swaps in its injected branding from an inline script in index.html.
|
|
function bootstrapTitlePlugin(): Plugin {
|
|
let development = false;
|
|
return {
|
|
name: "hanzodev:bootstrap-title",
|
|
configResolved(config) {
|
|
// The same flag Vite exposes to the app as import.meta.env.DEV.
|
|
development = !config.isProduction;
|
|
},
|
|
transformIndexHtml(html) {
|
|
const stageLabel = resolveWebAppStageLabel({
|
|
hostedAppChannel: configuredHostedAppChannel,
|
|
development,
|
|
});
|
|
const title = formatAppDisplayName({ baseName: "Hanzo Dev", stageLabel });
|
|
return html.replace(/<title>[^<]*<\/title>/, `<title>${title}</title>`);
|
|
},
|
|
};
|
|
}
|
|
|
|
// Allow Tailnet hosts for sharing; Tailscale-controlled DNS prevents attacker rebinding.
|
|
const configuredAllowedHosts = (process.env.HANZO_DEV_DEV_ALLOWED_HOSTS ?? "")
|
|
.split(",")
|
|
.map((entry) => entry.trim())
|
|
.filter((entry) => entry.length > 0);
|
|
const allowedHosts = [".ts.net", ...configuredAllowedHosts];
|
|
|
|
export default defineConfig(({ command }) => {
|
|
// `vp dev` and `--built` dev pair this client with a server run from source, which reports the
|
|
// manifest version. Only a production build is paired with a packed server.
|
|
const configuredAppVersion =
|
|
process.env.APP_VERSION?.trim() ||
|
|
(command === "build" && !builtDev
|
|
? resolveTaggedSourceVersion(`${import.meta.dirname}/../..`, pkg.version)
|
|
: pkg.version);
|
|
return {
|
|
run: {
|
|
tasks: {
|
|
// A local tsbuildinfo is leftover build state, not a typecheck input.
|
|
typecheck: {
|
|
command: "effect-tsgo tsrs --noEmit",
|
|
cache: { input: [{ auto: true }, "!*.tsbuildinfo"] },
|
|
},
|
|
},
|
|
},
|
|
assetsInclude: ["**/*.wasm"],
|
|
plugins: [
|
|
bootstrapTitlePlugin(),
|
|
devCompressionPlugin(),
|
|
chunkCycleGuardPlugin(),
|
|
builtDev
|
|
? undefined
|
|
: thirdPartyLicensesPlugin({
|
|
bundleName: "web",
|
|
configFile: new URL("../../third-party-licenses.config.json", import.meta.url),
|
|
packageManifests: [
|
|
{ bundle: "web", path: new URL("./package.json", import.meta.url) },
|
|
{ bundle: "server", path: new URL("../server/package.json", import.meta.url) },
|
|
{ bundle: "desktop", path: new URL("../desktop/package.json", import.meta.url) },
|
|
],
|
|
}),
|
|
tanstackRouter({ autoCodeSplitting: true }),
|
|
react({ compiler: true }),
|
|
tailwindPlugins(bundledDev),
|
|
warmBundledDevPlugin(),
|
|
bundledDev ? bundledDevStableNamesPlugin() : undefined,
|
|
],
|
|
optimizeDeps: {
|
|
include: [
|
|
"@clerk/clerk-js",
|
|
"@clerk/react/internal",
|
|
"@pierre/diffs",
|
|
"@pierre/diffs/edit",
|
|
"@pierre/diffs/react",
|
|
"@pierre/diffs/worker/worker.js",
|
|
"effect/Array",
|
|
"effect/Order",
|
|
"react-dom/client",
|
|
],
|
|
},
|
|
define: {
|
|
"import.meta.env.VITE_WS_URL": JSON.stringify(configuredWsUrl ?? ""),
|
|
// Override .env URLs with empty values in single-origin dev so clients use window.location.origin.
|
|
"import.meta.env.VITE_HTTP_URL": JSON.stringify(configuredHttpUrl ?? ""),
|
|
"import.meta.env.VITE_HANZO_DEV_RELAY_URL": JSON.stringify(configuredRelayUrl),
|
|
"import.meta.env.VITE_CLERK_PUBLISHABLE_KEY": JSON.stringify(configuredClerkPublishableKey),
|
|
"import.meta.env.VITE_CLERK_JWT_TEMPLATE": JSON.stringify(configuredClerkJwtTemplate),
|
|
"import.meta.env.VITE_CLERK_CLI_OAUTH_CLIENT_ID": JSON.stringify(
|
|
configuredClerkCliOAuthClientId,
|
|
),
|
|
"import.meta.env.VITE_RELAY_OTLP_TRACES_URL": JSON.stringify(configuredRelayTracingUrl),
|
|
"import.meta.env.VITE_RELAY_OTLP_TRACES_DATASET": JSON.stringify(
|
|
configuredRelayTracingDataset,
|
|
),
|
|
"import.meta.env.VITE_RELAY_OTLP_TRACES_TOKEN": JSON.stringify(configuredRelayTracingToken),
|
|
"import.meta.env.VITE_HOSTED_APP_URL": JSON.stringify(configuredHostedAppUrl ?? ""),
|
|
"import.meta.env.VITE_HOSTED_APP_CHANNEL": JSON.stringify(configuredHostedAppChannel),
|
|
"import.meta.env.HANZO_DEV_DESKTOP_UPDATE_REPOSITORY": JSON.stringify(
|
|
configuredDesktopUpdateRepository,
|
|
),
|
|
"import.meta.env.HANZO_DEV_FEATURE_SECRETS": JSON.stringify(configuredFeatureSecrets),
|
|
"import.meta.env.VITE_RELEASE_CHANNEL": JSON.stringify(configuredReleaseChannel),
|
|
"import.meta.env.APP_VERSION": JSON.stringify(configuredAppVersion),
|
|
},
|
|
resolve: {
|
|
tsconfigPaths: true,
|
|
dedupe: ["react", "react-dom"],
|
|
},
|
|
experimental: {
|
|
bundledDev,
|
|
},
|
|
server: {
|
|
host,
|
|
port,
|
|
strictPort: true,
|
|
allowedHosts,
|
|
// Warm the module graph before first request to avoid serial import transforms over tailnet.
|
|
// Bundled dev ignores this; warmBundledDevPlugin handles it there.
|
|
warmup: {
|
|
clientFiles: ["./src/main.tsx"],
|
|
},
|
|
watch: { exclude: rolldownWatchExclude },
|
|
...(devProxyTarget
|
|
? {
|
|
// Share prefixes with the backend catch-all so the proxy and 404 lists cannot drift.
|
|
proxy: Object.fromEntries(
|
|
DEV_PROXIED_PATH_PREFIXES.map((prefix) => [
|
|
`^${prefix.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:/|\\?|$)`,
|
|
{
|
|
target: devProxyTarget,
|
|
changeOrigin: !DEV_PROXIED_ORIGIN_PRESERVING_PREFIXES.has(prefix),
|
|
...(prefix === "/ws" || prefix === "/v1" ? { ws: true } : {}),
|
|
},
|
|
]),
|
|
),
|
|
}
|
|
: {}),
|
|
// Electron needs an explicit HMR host; elsewhere derive it from page origin for Tailscale/LAN.
|
|
...(explicitHost
|
|
? {
|
|
hmr: {
|
|
protocol: "ws",
|
|
host: explicitHost,
|
|
clientPort: port,
|
|
},
|
|
}
|
|
: {}),
|
|
},
|
|
// Tailwind needs devSourcemap to avoid Rolldown SOURCEMAP_BROKEN on sourcemapped builds.
|
|
css: {
|
|
devSourcemap: buildSourcemap !== false,
|
|
},
|
|
build: {
|
|
outDir: "dist",
|
|
emptyOutDir: true,
|
|
manifest: true,
|
|
sourcemap: buildSourcemap,
|
|
...(builtDev ? { watch: { exclude: rolldownWatchExclude } } : {}),
|
|
rolldownOptions: {
|
|
output: {
|
|
// PDF.js resolves decoder modules and ICC data relative to wasmUrl.
|
|
assetFileNames: (asset) =>
|
|
asset.originalFileNames.some((path) => /[/\\]pdfjs-dist[/\\]wasm[/\\]/.test(path))
|
|
? `assets/pdfjs-${pdfjsPkg.version}/[name][extname]`
|
|
: "assets/[name]-[hash][extname]",
|
|
},
|
|
},
|
|
},
|
|
worker: { format: "es" },
|
|
test: {
|
|
projects: [
|
|
defineProject(unitTestProject),
|
|
defineProject(browserTestProject),
|
|
defineProject(renderScenariosProject),
|
|
],
|
|
},
|
|
};
|
|
});
|