Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
65 lines (62 loc) · 1.79 KB
/
Copy pathvite.config.ts
File metadata and controls
65 lines (62 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { readdirSync } from "node:fs";
import { join } from "node:path";
import type { Plugin } from "vite";
import { defineConfig } from "vite";
import { tanstackStart } from "@tanstack/react-start/plugin/vite";
import viteReact from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import { nitro } from "nitro/vite";
import { isMigrationFile } from "./scripts/migration-plan.mjs";
/** The files `src/lib/db.ts` globs — same directory, same non-recursive scope. */
function hasGlobbedMigrations(root: string): boolean {
try {
return readdirSync(join(root, "migrations")).some(isMigrationFile);
} catch {
return false;
}
}
/**
* Finish PGLite bootstrap during dev-server setup (before traffic). Vite awaits
* async `configureServer` hooks. Production: `src/lib/db` kicks `ensureDbReady`
* on import.
*/
function pgliteBootstrapPlugin(): Plugin {
return {
name: "pglite-bootstrap",
apply: "serve",
async configureServer(server) {
if (!hasGlobbedMigrations(server.config.root)) return;
try {
const mod = (await server.ssrLoadModule("/src/lib/db.ts")) as {
ensureDbReady?: () => Promise<void>;
};
if (typeof mod.ensureDbReady === "function") {
await mod.ensureDbReady();
}
} catch (err) {
console.error("[greenkeep] DB bootstrap failed:", err);
throw err;
}
},
};
}
export default defineConfig(({ command, isPreview }) => ({
server: {
host: "0.0.0.0",
port: 8080,
strictPort: true,
},
preview: {
host: "127.0.0.1",
port: 8081,
strictPort: true,
},
resolve: { tsconfigPaths: true },
plugins: [
pgliteBootstrapPlugin(),
tailwindcss(),
tanstackStart(),
...(command === "build" || isPreview ? [nitro()] : []),
viteReact(),
],
}));