/ concept-collection / fastandaccurate
Sign in
concept-collection / fastandaccurate
fastandaccurate / vite.config.ts
55 lines · 1.8 KBBlameHistoryRaw
1import { defineConfig, type Plugin } from "vite";
2import react from "@vitejs/plugin-react";
3import { existsSync, readFileSync } from "fs";
4import { fileURLToPath } from "url";
5import { dirname, join, normalize } from "path";
7const root = dirname(fileURLToPath(import.meta.url));
8const numblVersion = (
9 JSON.parse(
10 readFileSync(join(root, "node_modules", "numbl", "package.json"), "utf-8")
11 ) as { version: string }
12).version;
14/**
15 * In dev, serve the sibling fastandaccurate-results checkout under
16 * /local-results/ so the page shows the results repo as it stands on
17 * disk, uncommitted and unpushed included, and never touches the network
18 * for them. Production builds fetch the deployed results site instead.
19 */
20function localResultsPlugin(): Plugin {
21 const resultsRoot = join(root, "..", "fastandaccurate-results");
22 return {
23 name: "fastandaccurate-local-results",
24 apply: "serve",
25 configureServer(server) {
26 server.middlewares.use((req, res, next) => {
27 const url = (req.url ?? "").split("?")[0];
28 if (!url.startsWith("/local-results/")) return next();
29 const rel = decodeURIComponent(url.slice("/local-results/".length));
30 const file = normalize(join(resultsRoot, rel));
31 if (!file.startsWith(normalize(resultsRoot)) || !existsSync(file)) {
32 res.statusCode = 404;
33 res.end("not found");
34 return;
35 }
36 res.setHeader("content-type", "application/json");
37 res.end(readFileSync(file));
38 });
39 },
40 };
43export default defineConfig({
44 base: "./",
45 plugins: [react(), localResultsPlugin()],
46 define: {
47 __NUMBL_VERSION__: JSON.stringify(numblVersion),
48 __BUILD_ID__: JSON.stringify(
49 process.env.GITHUB_SHA?.slice(0, 7) ?? "dev"
50 ),
51 },
52 worker: {
53 format: "es",
54 },
55});
moveopenescclose