-
Notifications
You must be signed in to change notification settings - Fork 33
/
vite.config.ts
113 lines (104 loc) · 2.9 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { createDatabase } from "./fake-snippets-api/lib/db/db-client"
import { defineConfig, Plugin, UserConfig } from "vite"
import type { PluginOption } 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"
// Create database instance with seed data for development
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()
}
})
},
}
}
export default defineConfig(async (): Promise<UserConfig> => {
let proxyConfig: Record<string, any> | undefined
const plugins: PluginOption[] = [react()]
if (process.env.VITE_BUNDLE_ANALYZE === "true" || 1) {
const { visualizer } = await import("rollup-plugin-visualizer")
plugins.push(
visualizer({
filename: "dist/stats.html",
open: false,
gzipSize: true,
brotliSize: true,
}),
)
}
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
proxyConfig = {
"/api": {
target: process.env.SNIPPETS_API_URL as string,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
}
}
return {
plugins,
define: {
global: {},
},
server: {
host: "127.0.0.1",
proxy: proxyConfig,
},
build: {
minify: false,
terserOptions: {
compress: false,
mangle: false,
},
reportCompressedSize: true, // https://github.com/vitejs/vite/issues/10086
rollupOptions: {
output: {
manualChunks: {
"react-vendor": ["react", "react-dom"],
codemirror: [
"@codemirror/autocomplete",
"@codemirror/lang-javascript",
"@codemirror/lang-json",
"@codemirror/lint",
"@codemirror/state",
"@codemirror/view",
],
},
},
},
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
logLevel: "info",
}
})