Files
clean-architecture-frontend…/vite.config.ts
T
DongHyeonkaandClaude Opus 5.5 9c8a5f417a fix(bootstrap): show the boot error shell when the app graph fails to evaluate (09-03)
A module that threw while evaluating stopped main.tsx before boot() ran, so the
static loading shell stayed up. The entry now imports the app (boot.tsx)
dynamically and renders the shell on rejection; the shell reads boot messages
without the feature catalog. The preload helper gets its own chunk so lazy
chunks do not import the per-build entry, and the bundle budget counts the
entry's dynamic imports as initial JS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 22:54:40 +09:00

84 lines
2.9 KiB
TypeScript

import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import { viteModuleInventoryPlugin } from "./scripts/lib/vite-module-inventory.ts";
import { ENV_REGISTRY } from "./src/contracts/env.ts";
/**
* 13-06. Every VITE_* value Vite loads (shell or `.env*`) is public once
* something bundles it, so a value the build does not declare is refused
* rather than carried along.
*/
function assertDeclaredBuildVariables(mode: string): void {
const undeclared = Object.keys(loadEnv(mode, process.cwd(), "VITE_"))
.filter((name) => !Object.hasOwn(ENV_REGISTRY, name))
.sort();
if (undeclared.length > 0) {
throw new Error(
`Undeclared VITE_* build variables: ${undeclared.join(", ")}. ` +
"Declare them in ENV_REGISTRY (src/contracts/env.ts) or unset them; " +
"VITE_ values are published to the browser.",
);
}
}
/**
* §6.1. One sub-path, declared once.
*
* `VITE_ROUTER_BASE_PATH` already drives the router and the Service Worker
* scope. Vite's asset `base` was left at its default, so a build served from
* `/app/` emitted root-absolute asset URLs and loaded nothing: the three
* consumers of the same setting disagreed. They are read from one value here so
* a sub-path deployment is coherent or fails at build time.
*/
function routerBasePath(environment: NodeJS.ProcessEnv): string {
const declared = environment["VITE_ROUTER_BASE_PATH"];
if (declared === undefined || declared === "") return "/";
if (!declared.startsWith("/") || !declared.endsWith("/")) {
throw new Error(
`VITE_ROUTER_BASE_PATH must start and end with "/"; received ${declared}`,
);
}
return declared;
}
/**
* 13-07. The runtime document ships inside dist, which is served under the
* base path. An explicit root-relative URL outside it would 404 at boot.
*/
function assertRuntimeConfigUnderBase(
environment: NodeJS.ProcessEnv,
base: string,
): void {
const declared = environment["VITE_RUNTIME_CONFIG_URL"];
if (declared?.startsWith("/") && !declared.startsWith(base)) {
throw new Error(
`VITE_RUNTIME_CONFIG_URL ${declared} is outside VITE_ROUTER_BASE_PATH ${base}`,
);
}
}
export default defineConfig(({ mode }) => {
assertDeclaredBuildVariables(mode);
const base = routerBasePath(process.env);
assertRuntimeConfigUnderBase(process.env, base);
return {
base,
plugins: [react(), tailwindcss(), viteModuleInventoryPlugin()],
build: {
manifest: true,
sourcemap: false,
rollupOptions: {
output: {
// 09-03. The entry now imports the app dynamically, so Vite's preload
// helper would land in the entry chunk and every lazy chunk would
// import the entry, whose hash changes with each build id.
manualChunks: (id: string) =>
id.includes("vite/preload-helper") ? "preload-helper" : undefined,
},
},
},
};
});