vite.config.ts
This file
Path
apps/web/vite.config.ts
100 lines · 3.4 KiB · TypeScript
Last changed by
Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101import { fileURLToPath } from "node:url";
import { cloudflare } from "@cloudflare/vite-plugin";import tailwindcss from "@tailwindcss/vite";import { tanstackStart } from "@tanstack/react-start/plugin/vite";import viteReact from "@vitejs/plugin-react";import { defineConfig, loadEnv } from "vite";import type { Plugin, ServerOptions } from "vite";
import { MANIFEST_PATH, webManifestText,} from "./src/features/agents/manifest.ts";
// Made from `Brand.name` rather than kept as a file that would spell the name.const webAppManifest = (): Plugin => ({ configureServer: (server) => { server.middlewares.use(MANIFEST_PATH, (_request, response) => { response.setHeader("content-type", "application/manifest+json"); response.end(webManifestText()); }); }, generateBundle() { if (this.environment.name === "client") { this.emitFile({ fileName: MANIFEST_PATH.slice(1), source: webManifestText(), type: "asset", }); } }, name: "web-app-manifest",});
const NO_WASM = "\0worker-no-shiki-wasm";
const WORKER_IN_PLACE = new Map([ ["@pierre/theming/themes", "src/server/pierre-themes.ts"], ["shiki", "src/server/shiki.ts"],]);
// The Worker's Shiki: core and the JavaScript engine alone, with no theme collection, so the server bundles only// the grammars it names, never Shiki's 10 MB map, Pierre's themes or WebAssembly. The browser keeps the whole packages.const workerShiki = (): Plugin => ({ applyToEnvironment: (environment) => environment.name === "ssr", enforce: "pre", load: (id) => (id === NO_WASM ? "export default undefined;" : undefined), name: "worker-shiki", resolveId: (id) => { const inPlace = WORKER_IN_PLACE.get(id);
if (inPlace !== undefined) { return fileURLToPath(new URL(inPlace, import.meta.url)); }
return id === "shiki/wasm" ? NO_WASM : undefined; },});
// The Cloudflare framework guide's order: the Worker runs TanStack Start's// SSR environment, and Start comes before React. `vite build` picks the// stage from `CLOUDFLARE_ENV` (infra/deploy.ts sets it).export default defineConfig(({ mode }) => { // This machine's Caddy name for the dev server, from the gitignored // `apps/web/.env.development.local`, which Vite reads in dev and Wrangler // never loads as a Worker secret; without one, the dev server answers on // 127.0.0.1 alone. const caddyDevHost = loadEnv(mode, process.cwd(), "").CADDY_DEV_HOST;
const server: ServerOptions = { host: "127.0.0.1", port: 3032, strictPort: true, };
if (caddyDevHost !== undefined && caddyDevHost !== "") { server.allowedHosts = ["localhost", "127.0.0.1", caddyDevHost]; server.hmr = { clientPort: 443, host: caddyDevHost, protocol: "wss" }; }
return { plugins: [ cloudflare({ viteEnvironment: { name: "ssr" } }), tailwindcss(), tanstackStart(), viteReact(), webAppManifest(), workerShiki(), ], // shadcn's components import through `@/` (components.json), which // tsconfig.json's `paths` maps to `src/`. resolve: { tsconfigPaths: true }, server, // The Worker's own code is minified like the browser's: about a third // of its bytes. Its source maps go up with each deploy // (`upload_source_maps` in wrangler.jsonc), so a stack trace in the // Worker's logs still names the source. environments: { ssr: { build: { minify: true, sourcemap: true } } }, };});