forked from tscircuit/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
79 lines (73 loc) · 1.84 KB
/
vite.config.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { createDatabase } from "./fake-snippets-api/lib/db/db-client"
import { defineConfig, Plugin } from "vite"
import path from "path"
import react from "@vitejs/plugin-react"
import { getNodeHandler } from "winterspec/adapters/node"
// @ts-ignore
import winterspecBundle from "./dist/bundle.js"
const db = createDatabase({ seed: true })
const fakeHandler = getNodeHandler(winterspecBundle as any, {
middleware: [
(req, ctx, next) => {
;(ctx as any).db = db
return next(req, ctx)
},
],
})
function apiFakePlugin(): Plugin {
return {
name: "api-fake",
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url?.startsWith("/api/")) {
// simulate slow responses
await new Promise((resolve) => setTimeout(resolve, 500))
fakeHandler(req, res)
} else {
next()
}
})
},
}
}
let plugins: any[] = [react()]
let proxy: any = undefined
if (!process.env.SNIPPETS_API_URL && !process.env.VERCEL) {
process.env.VITE_USE_FAKE_API = "true"
console.log("Using fake snippets API (see ./fake-snippets-api)")
plugins.push(apiFakePlugin())
} else {
console.log(`Using snippets API at "${process.env.SNIPPETS_API_URL}"`)
process.env.VITE_SNIPPETS_API_URL =
process.env.VITE_SNIPPETS_API_URL || process.env.SNIPPETS_API_URL
proxy = {
"/api": {
target: process.env.SNIPPETS_API_URL as string,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
}
}
export default defineConfig({
plugins,
define: {
global: {},
},
server: {
host: "127.0.0.1",
proxy,
},
build: {
minify: false,
terserOptions: {
compress: false,
mangle: false,
},
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
logLevel: "info",
})