import { createHash } from "node:crypto"; import { readdirSync, readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { defineConfig, type Plugin } from "vite"; import react from "@vitejs/plugin-react"; // sourceStamp writes a hash of everything the build is made from next to // the build. The Go test in this directory computes the same hash, so a // change under src that was committed without a rebuild fails the tests. const stamped = ["index.html", "package.json", "package-lock.json", "tsconfig.json", "vite.config.ts"]; function walk(dir: string): string[] { return readdirSync(dir, { withFileTypes: true }).flatMap((e) => e.isDirectory() ? walk(join(dir, e.name)) : [join(dir, e.name)], ); } function sourceStamp(): Plugin { return { name: "source-stamp", apply: "build", closeBundle() { const hash = createHash("sha256"); const files = [...stamped, ...walk("src")].map((f) => f.split("\\").join("/")).sort(); for (const f of files) { hash.update(f + "\0"); hash.update(readFileSync(f)); hash.update("\0"); } writeFileSync("dist/source.sha256", hash.digest("hex") + "\n"); }, }; } // The app is served by the core under /app/ and embedded in its binary. // File names are stable so a rebuild changes as few files as possible; // the core sends an ETag per file, so caches still notice a new build. export default defineConfig({ base: "/app/", plugins: [react(), sourceStamp()], build: { outDir: "dist", emptyOutDir: true, target: "es2020", rollupOptions: { output: { entryFileNames: "assets/app.js", chunkFileNames: "assets/[name].js", assetFileNames: "assets/[name][extname]", }, }, }, server: { // npm run dev: API calls go to a core running locally. proxy: { "/app/api": "http://localhost:8080", "/pay": "http://localhost:8080" }, }, });