-
-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathvite.config.ts
More file actions
269 lines (259 loc) · 8.53 KB
/
vite.config.ts
File metadata and controls
269 lines (259 loc) · 8.53 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { sentryTanstackStart } from '@sentry/tanstackstart-react/vite'
import { defineConfig } from 'vite'
import { tanstackDom } from '@tanstack/dom-vite'
import contentCollections from '@content-collections/vite'
import { devtools as tanstackDevtools } from '@tanstack/devtools-vite'
import { tanstackStart } from '@tanstack/react-start/plugin/vite'
import tailwindcss from '@tailwindcss/vite'
import { analyzer } from 'vite-bundle-analyzer'
import viteReact from '@vitejs/plugin-react'
import rsc from '@vitejs/plugin-rsc'
import netlify from '@netlify/vite-plugin-tanstack-start'
import path from 'node:path'
const isDev = process.env.NODE_ENV !== 'production'
const shouldUseSentryPlugin =
process.env.NODE_ENV === 'production' &&
Boolean(process.env.SENTRY_AUTH_TOKEN)
const rscSsrExternals = [
// OpenTelemetry uses require-in-the-middle which is CJS-only and breaks
// under Vite's ESM module runner during dev SSR.
'require-in-the-middle',
'@opentelemetry/instrumentation',
// HTML parsing stack has known CJS/ESM interop issues in SSR module runner.
'cheerio',
'iconv-lite',
'encoding-sniffer',
'parse5',
'parse5-parser-stream',
// Compression/archive stack has known CJS transform issues in dev SSR.
'jszip',
'pako',
// These packages also have known CJS/ESM interop issues in the RSC/SSR path.
'discord-interactions',
]
const sentrySsrExternals = ['@sentry/node', '@sentry/tanstackstart-react']
const dbSsrExternals = ['drizzle-orm', 'drizzle-orm/postgres-js']
// Runtime-specific `react-dom/server` variants aren't in @tanstack/dom-vite's
// default alias map — our shim ships a single universal server build, unlike
// React which maintains per-runtime forks (edge/node/bun/browser + static.*).
// @vitejs/plugin-rsc and Netlify's edge adapter import them conditionally, so
// we funnel them all to `@tanstack/react-dom-server` at the top-level resolve
// (Vite 8's `EnvironmentResolveOptions` doesn't accept `alias`, so env-scoped
// aliasing isn't an option).
const serverVariantAliases: Record<string, string> = {
'react-dom/server.edge': '@tanstack/react-dom-server',
'react-dom/server.node': '@tanstack/react-dom-server',
'react-dom/server.bun': '@tanstack/react-dom-server',
'react-dom/server.browser': '@tanstack/react-dom-server',
'react-dom/static.edge': '@tanstack/react-dom-server',
'react-dom/static.node': '@tanstack/react-dom-server',
'react-dom/static': '@tanstack/react-dom-server',
}
export default defineConfig({
resolve: {
alias: {
'~': path.resolve(__dirname, './src'),
...serverVariantAliases,
},
},
server: {
port: Number(process.env.PORT) || 3000,
// WebContainer headers for /builder route (SharedArrayBuffer support)
headers: {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
},
// Watch linked @tanstack/cli for hot reload during development
watch: isDev
? {
ignored: ['!**/node_modules/@tanstack/cli/**'],
}
: undefined,
},
environments: {
rsc: {
resolve: {
external: [
'@tanstack/react-start-server',
'@tanstack/react-router/ssr/server',
],
},
},
ssr: {
resolve: {
external: [
...rscSsrExternals,
...sentrySsrExternals,
...dbSsrExternals,
],
},
},
},
ssr: {
external: [
'postgres',
...dbSsrExternals,
// CTA packages use execa which has a broken unicorn-magic dependency
'@tanstack/create',
// Externalize CLI so server reloads it on changes
'@tanstack/cli',
...rscSsrExternals,
...sentrySsrExternals,
],
noExternal: [
'@uploadthing/react',
'file-selector',
'normalize-wheel',
'@tanstack/react-hotkeys',
'@webcontainer/api',
],
},
optimizeDeps: {
exclude: [
'postgres',
// CTA packages use execa which has a broken unicorn-magic dependency
'@tanstack/create',
'discord-interactions',
// Don't pre-bundle CLI so we always get fresh changes during dev
...(isDev ? ['@tanstack/cli'] : []),
// `use client` libraries that plugin-rsc pre-bundles inconsistently
// across client/ssr/rsc envs when combined with our React shim — each
// env resolves `react` to a different target, so the optimizer's hash
// diverges. Excluding from optimize keeps resolution deterministic per
// env and silences the 50k+ "inconsistently optimized" warning flood.
'lucide-react',
],
},
build: {
sourcemap: process.env.NODE_ENV === 'production',
rollupOptions: {
external: (id) => {
// Externalize postgres from client bundle
return id.includes('postgres')
},
output: {
manualChunks: (id) => {
// Keep the app shell and docs runtime from shattering into dozens of
// tiny eagerly preloaded chunks.
if (
id.includes('/src/components/Navbar') ||
id.includes('/src/components/Theme') ||
id.includes('/src/components/SearchButton') ||
id.includes('/src/components/NetlifyImage') ||
id.includes('/src/components/Card') ||
id.includes('/src/components/Footer') ||
id.includes('/src/components/ToastProvider') ||
id.includes('/src/components/icons/') ||
id.includes('/src/contexts/SearchContext') ||
id.includes('/src/hooks/useCurrentUser') ||
id.includes('/src/hooks/useCapabilities') ||
id.includes('/src/libraries/libraries.ts') ||
id.includes('/src/ui/')
) {
return 'app-shell'
}
if (
id.includes('/node_modules/@tanstack/react-router') ||
id.includes('/node_modules/@tanstack/router-core') ||
id.includes('/node_modules/@tanstack/history')
) {
return 'tanstack-router'
}
if (
id.includes('/node_modules/@tanstack/react-query') ||
id.includes('/node_modules/@tanstack/query-core')
) {
return 'tanstack-query'
}
// Vendor chunk splitting for better caching
if (id.includes('node_modules')) {
// Lucide icons (tree-shaken but still significant)
if (id.includes('lucide-react')) {
return 'icons'
}
if (
id.includes('node_modules/react-dom/') ||
id.includes('node_modules/react/') ||
id.includes('node_modules/scheduler/')
) {
return 'react'
}
}
},
},
},
},
plugins: [
tanstackDom(),
...(isDev
? [
tanstackDevtools({
// react-instantsearch's <Configure> forwards all JSX props as
// Algolia search parameters. Injecting `data-tsd-source` as a
// JSX attr leaks it into the request and Algolia 400s with
// "Unknown parameter: data-tsd-source" — breaks site search in dev.
injectSource: {
enabled: true,
ignore: { components: ['Configure'] },
},
}),
]
: []),
tanstackStart({
rsc: {
enabled: true,
},
importProtection: {
behavior: 'error',
client: {
files: ['**/*.server.*', '**/server/**'],
specifiers: [
'@tanstack/react-start/server',
'uploadthing/server',
/^@modelcontextprotocol\/sdk\/server\//,
'discord-interactions',
],
},
},
router: {
codeSplittingOptions: {
defaultBehavior: [
[
'component',
'pendingComponent',
'errorComponent',
'notFoundComponent',
'loader',
],
],
},
},
}),
rsc(),
// Only enable Netlify plugin during build or when NETLIFY env is set
...(process.env.NETLIFY || process.env.NODE_ENV === 'production'
? [netlify()]
: []),
viteReact(),
...(shouldUseSentryPlugin
? [
sentryTanstackStart({
authToken: process.env.SENTRY_AUTH_TOKEN,
org: 'tanstack',
project: 'tanstack-com',
}),
]
: []),
contentCollections(),
tailwindcss(),
...(process.env.ANALYZE
? [
analyzer({
analyzerMode: 'json',
fileName: 'bundle-analysis',
defaultSizes: 'stat',
}),
]
: []),
],
})