-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathvite.config.ts
More file actions
148 lines (137 loc) · 3.43 KB
/
vite.config.ts
File metadata and controls
148 lines (137 loc) · 3.43 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
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
147
148
import winterspecBundle from "@tscircuit/file-server/dist/bundle"
import react from "@vitejs/plugin-react"
import { resolve } from "node:path"
import { type Plugin, defineConfig } from "vite"
import { getNodeHandler } from "winterspec/adapters/node"
import fakeRegistryBundle from "@tscircuit/fake-snippets/bundle"
import { createDatabase } from "@tscircuit/fake-snippets"
import ky from "ky"
const registryDb = createDatabase()
const fileServerHandler = getNodeHandler(winterspecBundle as any, {})
const fakeRegistryHandler = getNodeHandler(fakeRegistryBundle as any, {
middleware: [
(req, ctx, next) => {
;(ctx as any).db = registryDb
return next(req, ctx)
},
],
})
function fileServerPlugin(): Plugin {
return {
name: "file-server",
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url?.startsWith("/api/")) {
req.url = req.url.replace("/api/", "/")
fileServerHandler(req, res)
} else {
next()
}
})
},
}
}
function fakeRegistryPlugin(): Plugin {
let initialized = false
return {
name: "fake-registry",
async configureServer(server) {
const port = server.config.server.port
server.middlewares.use(async (req, res, next) => {
if (process.env.REGISTRY_API_BASE_URL) {
// TODO Proxy requests to the registry API base URL
}
if (req.url?.startsWith("/registry/")) {
if (!initialized) {
initialized = true
await ky.post(`http://localhost:${port}/registry/_fake/seed`)
}
req.url = `/api/${req.url.replace("/registry/", "")}`
fakeRegistryHandler(req, res)
} else {
next()
}
})
},
}
}
const plugins: any[] = [react()]
if (!process.env.VERCEL && !process.env.STANDALONE) {
plugins.push(fileServerPlugin())
plugins.push(fakeRegistryPlugin())
}
let build: any = undefined
if (process.env.STANDALONE === "1") {
build = {
// metafile: "./metafile.json",
lib: {
entry: resolve(
__dirname,
"lib/components/RunFrameWithApi/standalone.tsx",
),
name: "standalone",
fileName: (format) => `standalone.min.js`,
formats: ["umd"],
},
minify: true,
}
}
if (process.env.STANDALONE === "iframe") {
build = {
lib: {
entry: resolve(__dirname, "lib/entrypoints/iframe.html"),
name: "iframe",
fileName: (format) => `iframe.min.js`,
formats: ["umd"],
},
minify: true,
}
}
if (process.env.STANDALONE === "preview") {
build = {
lib: {
entry: resolve(
__dirname,
"lib/components/CircuitJsonPreviewStandalone/standalone-preview.tsx",
),
name: "standalone-preview",
fileName: (format) => `standalone-preview.min.js`,
formats: ["umd"],
},
minify: true,
}
}
export default defineConfig({
plugins,
assetsInclude: ["**/*.glb", "**/*.step", "**/*.obj"],
resolve: {
alias: {
lib: resolve(__dirname, "./lib"),
},
},
define: {
process: {
env: {},
version: "",
},
},
root: ".",
publicDir: "public",
build: {
...build,
rollupOptions: {
external: [
"@resvg/resvg-js",
"@resvg/resvg-js-darwin-arm64",
"@resvg/resvg-wasm",
],
},
},
optimizeDeps: {
exclude: [
"@resvg/resvg-js",
"@resvg/resvg-js-darwin-arm64",
"@resvg/resvg-wasm",
],
},
})