forked from tscircuit/tscircuit.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
146 lines (137 loc) · 3.46 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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 { ViteImageOptimizer } from "vite-plugin-image-optimizer"
import { getNodeHandler } from "winterspec/adapters/node"
import vercel from "vite-plugin-vercel"
// @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(),
vercel({
prerender: false,
buildCommand: "bun run build",
}),
ViteImageOptimizer({
png: {
quality: 75,
compressionLevel: 9,
},
jpeg: {
quality: 75,
progressive: true,
},
jpg: {
quality: 75,
progressive: true,
},
webp: {
quality: 75,
lossless: false,
effort: 6,
},
avif: {
quality: 75,
effort: 6,
},
}),
]
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,
},
base: "/",
build: {
minify: "terser",
copyPublicDir: true,
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
},
format: {
comments: false,
},
},
reportCompressedSize: true, // https://github.com/vitejs/vite/issues/10086
rollupOptions: {
input: {
main: path.resolve(__dirname, "index.html"),
landing: path.resolve(__dirname, "landing.html"),
},
},
},
ssr: {
noExternal: ["react-dom/client"],
target: "node",
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
logLevel: "info",
}
})