-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathvite.config.ts
More file actions
472 lines (410 loc) · 14.2 KB
/
vite.config.ts
File metadata and controls
472 lines (410 loc) · 14.2 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import fs from 'fs';
import path from 'path';
import { defineConfig, loadEnv, type ServerOptions } from 'vite';
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';
const appPackageVersion =
JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf8')).version ?? '0.0.0';
const threeRoot = path.resolve(__dirname, 'node_modules/three');
const threeModuleEntry = path.resolve(threeRoot, 'build/three.module.js');
const threeExamplesDir = path.resolve(threeRoot, 'examples/jsm');
function buildConfigurationFileIndex(rootDirs: string[]): Map<string, string> {
const fileIndex = new Map<string, string>();
const visitDirectory = (currentDir: string) => {
let entries: fs.Dirent[] = [];
try {
entries = fs.readdirSync(currentDir, { withFileTypes: true });
} catch {
return;
}
entries.forEach((entry) => {
const fullPath = path.join(currentDir, entry.name);
if (entry.isDirectory()) {
visitDirectory(fullPath);
return;
}
if (!entry.isFile()) return;
if (!/\.(usd|usda|usdc)$/i.test(entry.name)) return;
if (!fullPath.includes(`${path.sep}configuration${path.sep}`)) return;
if (fileIndex.has(entry.name)) return;
fileIndex.set(entry.name, fullPath);
});
};
rootDirs.forEach((rootDir) => visitDirectory(rootDir));
return fileIndex;
}
function resolveUsdConfigurationRootDirs(): string[] {
const candidateDirs = [
path.resolve(__dirname, 'public/unitree_model'),
path.resolve(__dirname, 'public/Robots'),
];
return candidateDirs.filter((dirPath) => fs.existsSync(dirPath));
}
function createUsdConfigurationProxyPlugin() {
const configurationFileIndex = buildConfigurationFileIndex(resolveUsdConfigurationRootDirs());
return {
name: 'usd-configuration-proxy',
configureServer(server: import('vite').ViteDevServer) {
server.middlewares.use((request, response, next) => {
const requestUrl = String(request.url || '');
const urlMatch = requestUrl.match(/^\/configuration\/([^/?#]+)$/);
if (!urlMatch) {
next();
return;
}
const fileName = decodeURIComponent(urlMatch[1] || '');
const filePath = configurationFileIndex.get(fileName);
if (!filePath) {
next();
return;
}
response.statusCode = 200;
response.setHeader('Content-Type', 'application/octet-stream');
fs.createReadStream(filePath).pipe(response);
});
},
generateBundle(this: import('rollup').PluginContext) {
configurationFileIndex.forEach((filePath, fileName) => {
this.emitFile({
type: 'asset',
fileName: `configuration/${fileName}`,
source: fs.readFileSync(filePath),
});
});
},
};
}
function isMonacoReactChunkModule(normalizedId: string): boolean {
return normalizedId.includes('/@monaco-editor/react/');
}
function isMonacoEditorChunkModule(normalizedId: string): boolean {
return normalizedId.includes('/monaco-editor/esm/vs/');
}
const INITIAL_HTML_MODULE_PRELOAD_BLOCKLIST = [
'feature-file-io-',
'export-vendor-',
'feature-property-editor-',
'feature-robot-tree-',
'feature-editor-runtime-',
'feature-urdf-viewer-runtime-',
'ViewerSceneConnector-',
'ViewerJointsPanel-',
];
function shouldSkipInitialHtmlModulePreload(dependency: string): boolean {
return INITIAL_HTML_MODULE_PRELOAD_BLOCKLIST.some((token) => dependency.includes(token));
}
const GENERATED_ARTIFACT_WATCH_IGNORE_ROOTS = [
path.resolve(__dirname, '.venv'),
path.resolve(__dirname, '.omx'),
path.resolve(__dirname, 'tmp'),
path.resolve(__dirname, '.tmp'),
path.resolve(__dirname, 'output'),
path.resolve(__dirname, 'dist'),
path.resolve(__dirname, 'log'),
path.resolve(__dirname, 'test'),
].map((entryPath) => entryPath.replace(/\\/g, '/'));
const GENERATED_ARTIFACT_WATCH_IGNORE_SEGMENTS = ['/.git/', '/.svn/', '/.hg/'];
const ISOLATED_DOCUMENT_HEADERS = {
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Resource-Policy': 'same-site',
} as const;
// Vite crawls HTML entrypoints to discover dependency optimizer inputs.
// This repository intentionally keeps many fixture/example HTML files under
// tmp/, .tmp/, and test/, so constrain discovery to the actual app entry.
const OPTIMIZE_DEPS_ENTRY_FILES = ['index.html'];
const OPTIMIZE_DEPS_INCLUDE = [
'three',
'@react-three/fiber',
'@react-three/drei',
'zustand/react/shallow',
'lucide-react',
'zustand',
'@monaco-editor/react',
'jszip',
'zustand/middleware/immer',
'zustand/middleware',
'immer',
'three/examples/jsm/loaders/GLTFLoader.js',
'three/examples/jsm/utils/SkeletonUtils.js',
'three/examples/jsm/loaders/VTKLoader.js',
'three/examples/jsm/loaders/STLLoader.js',
'three/examples/jsm/geometries/ConvexGeometry.js',
'three/examples/jsm/loaders/ColladaLoader.js',
'three/examples/jsm/loaders/OBJLoader.js',
'three/addons/exporters/OBJExporter.js',
'three/examples/jsm/environments/RoomEnvironment.js',
'three-stdlib',
'linkedom',
'html2canvas',
'jspdf',
'three/examples/jsm/postprocessing/EffectComposer.js',
'three/examples/jsm/postprocessing/RenderPass.js',
'three/examples/jsm/postprocessing/BokehPass.js',
'three/addons/loaders/GLTFLoader.js',
];
function resolveDevServerHost(env: Record<string, string | undefined>): string {
const configuredHost = env.URDF_STUDIO_DEV_HOST?.trim();
return configuredHost || 'localhost';
}
function resolveDevServerAllowedHosts(
env: Record<string, string | undefined>,
): ServerOptions['allowedHosts'] | undefined {
const configuredHosts = env.URDF_STUDIO_DEV_ALLOWED_HOSTS?.trim();
if (!configuredHosts) {
return undefined;
}
if (configuredHosts === '*' || configuredHosts.toLowerCase() === 'true') {
return true;
}
const allowedHosts = configuredHosts
.split(',')
.map((host) => host.trim())
.filter(Boolean);
return allowedHosts.length > 0 ? allowedHosts : undefined;
}
function shouldIgnoreWatchPath(watchPath: string): boolean {
const normalizedPath = watchPath.replace(/\\/g, '/');
return (
GENERATED_ARTIFACT_WATCH_IGNORE_ROOTS.some(
(rootPath) => normalizedPath === rootPath || normalizedPath.startsWith(`${rootPath}/`),
) ||
GENERATED_ARTIFACT_WATCH_IGNORE_SEGMENTS.some((segment) => normalizedPath.includes(segment))
);
}
interface IsolationHeaderRequestLike {
headers?: {
host?: string | string[];
'x-forwarded-proto'?: string | string[];
};
socket?: {
encrypted?: boolean;
};
url?: string;
}
function readFirstHeaderValue(value: string | string[] | undefined): string {
return Array.isArray(value) ? (value[0] ?? '') : (value ?? '');
}
function normalizeRequestHostname(hostHeader: string): string {
const trimmedHost = hostHeader.trim();
if (!trimmedHost) {
return '';
}
try {
return new URL(`http://${trimmedHost}`).hostname.replace(/^\[|\]$/g, '').toLowerCase();
} catch {
return trimmedHost
.replace(/^\[|\]$/g, '')
.replace(/:\d+$/, '')
.toLowerCase();
}
}
function isTrustedLocalDevHostname(hostname: string): boolean {
const normalizedHostname = hostname.replace(/\.$/, '').toLowerCase();
return (
normalizedHostname === 'localhost' ||
normalizedHostname.endsWith('.localhost') ||
normalizedHostname === '::1' ||
/^127(?:\.\d{1,3}){3}$/.test(normalizedHostname)
);
}
function isHttpsDevRequest(request: IsolationHeaderRequestLike): boolean {
if (request.socket?.encrypted) {
return true;
}
const forwardedProto = readFirstHeaderValue(request.headers?.['x-forwarded-proto'])
.split(',')[0]
?.trim()
.toLowerCase();
return forwardedProto === 'https';
}
function applyDocumentHeaders(
response: import('node:http').ServerResponse,
headers: Readonly<Record<string, string>>,
): void {
Object.entries(headers).forEach(([headerName, headerValue]) => {
response.setHeader(headerName, headerValue);
});
}
function shouldApplyIsolatedDocumentHeaders(request: IsolationHeaderRequestLike): boolean {
if (isHttpsDevRequest(request)) {
return true;
}
return isTrustedLocalDevHostname(
normalizeRequestHostname(readFirstHeaderValue(request.headers?.host)),
);
}
function createConditionalIsolationHeadersPlugin() {
const installHeaderMiddleware = (middlewareStack: {
use: (handler: (req: any, res: any, next: () => void) => void) => void;
}): void => {
middlewareStack.use((request, response, next) => {
if (shouldApplyIsolatedDocumentHeaders(request)) {
applyDocumentHeaders(response, ISOLATED_DOCUMENT_HEADERS);
}
next();
});
};
return {
name: 'conditional-isolation-headers',
configureServer(server: import('vite').ViteDevServer) {
installHeaderMiddleware(server.middlewares);
},
configurePreviewServer(server: import('vite').PreviewServer) {
installHeaderMiddleware(server.middlewares);
},
};
}
function createStaticHostingHeadersAssetPlugin() {
return {
name: 'static-hosting-headers-asset',
generateBundle(this: import('rollup').PluginContext) {
const headersPath = path.resolve(__dirname, 'public/_headers');
if (!fs.existsSync(headersPath)) {
return;
}
this.emitFile({
type: 'asset',
fileName: '_headers',
source: fs.readFileSync(headersPath),
});
},
};
}
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, '.', '');
const devServerAllowedHosts = resolveDevServerAllowedHosts(env);
return {
server: {
port: 3000,
strictPort: false,
host: resolveDevServerHost(env),
...(devServerAllowedHosts ? { allowedHosts: devServerAllowedHosts } : {}),
// Verification artifacts are intentionally written into tmp/ by repo policy.
// Ignore generated directories so exports, screenshots, logs, and pid files
// do not trigger full-page reloads and wipe imported workspace state.
// Root-level test fixtures contain vendored repositories large enough to
// exhaust OS watcher limits, so filter them explicitly by absolute path.
watch: {
ignored: shouldIgnoreWatchPath,
},
},
build: {
chunkSizeWarningLimit: 800,
modulePreload: {
resolveDependencies(_filename, deps, context) {
if (context.hostType !== 'html') {
return deps;
}
return deps.filter((dependency) => !shouldSkipInitialHtmlModulePreload(dependency));
},
},
rollupOptions: {
input: [path.resolve(__dirname, 'index.html')],
output: {
manualChunks(id) {
const normalizedId = id.replace(/\\/g, '/');
if (normalizedId.includes('/src/core/parsers/')) {
return 'core-parsers';
}
if (!normalizedId.includes('/node_modules/')) return;
if (normalizedId.includes('/@react-three/drei/')) {
return 'drei-vendor';
}
if (normalizedId.includes('/@react-three/fiber/')) {
return 'r3f-vendor';
}
if (
normalizedId.includes('/three/examples/') ||
normalizedId.includes('/three-stdlib/')
) {
return 'three-addons';
}
if (normalizedId.includes('/three/')) {
return 'three-core';
}
if (isMonacoReactChunkModule(normalizedId)) {
return 'editor-monaco-react';
}
if (isMonacoEditorChunkModule(normalizedId)) {
return 'editor-monaco';
}
if (
normalizedId.includes('/react-syntax-highlighter/') ||
normalizedId.includes('/react-simple-code-editor/') ||
normalizedId.includes('/prismjs/')
) {
return 'code-vendor';
}
if (normalizedId.includes('/jspdf/') || normalizedId.includes('/jszip/')) {
return 'export-vendor';
}
if (normalizedId.includes('/lucide-react/')) {
return 'icon-vendor';
}
if (normalizedId.includes('/zustand/') || normalizedId.includes('/immer/')) {
return 'state-vendor';
}
if (
normalizedId.includes('/react/') ||
normalizedId.includes('/react-dom/') ||
normalizedId.includes('/scheduler/')
) {
return 'react-vendor';
}
},
},
},
},
worker: {
format: 'es',
},
plugins: [
react(),
tailwindcss(),
createUsdConfigurationProxyPlugin(),
createConditionalIsolationHeadersPlugin(),
createStaticHostingHeadersAssetPlugin(),
],
define: {
__APP_VERSION__: JSON.stringify(appPackageVersion),
'process.env.API_KEY': JSON.stringify(env.OPENAI_API_KEY || env.GEMINI_API_KEY),
'process.env.GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.OPENAI_API_KEY),
'process.env.OPENAI_API_KEY': JSON.stringify(env.OPENAI_API_KEY),
'process.env.OPENAI_BASE_URL': JSON.stringify(env.OPENAI_BASE_URL),
'process.env.OPENAI_MODEL': JSON.stringify(env.OPENAI_MODEL),
},
optimizeDeps: {
entries: OPTIMIZE_DEPS_ENTRY_FILES,
// Keep the dependency optimizer on the same Three.js entry that the
// application source and R3F use, otherwise optimized deps can pull in
// a second copy from a different workspace path.
include: OPTIMIZE_DEPS_INCLUDE,
// The oxipng codec ships a `.wasm?url` import that esbuild's dependency
// pre-bundling can't resolve; let Vite's asset pipeline handle it inside
// the PNG optimization worker instead.
exclude: ['@jsquash/oxipng'],
},
resolve: {
dedupe: ['three', '@react-three/fiber', '@react-three/drei'],
alias: [
{
find: '@',
replacement: path.resolve(__dirname, './src'),
},
{
find: /^three$/,
replacement: threeModuleEntry,
},
{
find: /^three\/addons\//,
replacement: `${threeExamplesDir}/`,
},
{
find: /^three\/examples\/jsm\//,
replacement: `${threeExamplesDir}/`,
},
],
},
};
});