Files
streamline/control-web/vite.config.ts
T
2026-11-16 17:26:44 -08:00

67 lines
3.1 KiB
TypeScript

import { execSync } from "node:child_process";
import { defineConfig, type Plugin } from "vite";
import react from "@vitejs/plugin-react";
/**
* Stamp the build (commit + build time) into index.html as
* `<meta name="streamline-build">`; the account menu reads it, so each tab can
* say which deploy it booted from. Deliberately NOT a `define` in JS: the entry
* chunk's hash would then change on every deploy — backend-only ones too — and
* with it every lazy chunk's (they import the entry by hashed name), so every
* open tab would hit a missing chunk and reload on its next lazy navigation.
* index.html carries no content hash, so changing it costs nothing.
*/
function buildStamp(): Plugin {
let sha = "unknown";
try {
sha = execSync("git rev-parse --short HEAD", { stdio: ["ignore", "pipe", "ignore"] }).toString().trim() || sha;
} catch { /* not a git checkout — keep "unknown" */ }
const content = `${sha} ${new Date().toISOString()}`;
return {
name: "streamline-build-stamp",
apply: "build",
transformIndexHtml: () => [{ tag: "meta", attrs: { name: "streamline-build", content }, injectTo: "head" }],
};
}
// Collaborator frontend-only mode: set API_TARGET to a remote control-server
// (e.g. https://streamline-internal-api.bytedance.net) to proxy /api + /ws there
// instead of the local control-server. Unset → host default (local server behind Caddy).
const API_TARGET = process.env.API_TARGET?.replace(/\/$/, "");
const remote = !!API_TARGET;
const httpTarget = API_TARGET ?? "http://127.0.0.1:5173";
const wsTarget = remote ? API_TARGET!.replace(/^http/, "ws") : "ws://127.0.0.1:5173";
export default defineConfig({
plugins: [react(), buildStamp()],
build: {
rollupOptions: {
output: {
// React + ReactDOM change far less often than our code. In their own
// chunk the hash survives a deploy, so returning users (assets are
// served immutable) re-download only the app chunk, not ~140 KB of
// framework. It's modulepreloaded from index.html alongside the entry,
// so it adds no round trip. Keep this list to the stable core — adding
// churny deps here just makes the vendor hash change every deploy.
manualChunks(id) {
if (/[\\/]node_modules[\\/](react|react-dom|scheduler)[\\/]/.test(id)) return "react-vendor";
},
},
},
},
server: {
// Bound to loopback; on the host, TLS is terminated by Caddy (443) which reverse-proxies here.
host: "127.0.0.1",
port: 8080,
// Host serves fixed domains behind Caddy; remote-mode collaborators dev on plain localhost.
allowedHosts: remote ? true : ["streamline.bytedance.net", "streamline.shiro0.icu", "fei.shiro0.icu"],
// Page is served over https://streamline.bytedance.net (443); HMR ws must target 443, not 8080.
// Remote mode runs on plain http://localhost:8080, so use Vite's default HMR.
...(remote ? {} : { hmr: { clientPort: 443 } }),
proxy: {
"/api": { target: httpTarget, changeOrigin: remote },
"/ws": { target: wsTarget, ws: true, changeOrigin: remote },
},
},
});