import { exec } from 'node:child_process' import { createRequire } from 'node:module' import os from 'node:os' import react, { reactCompilerPreset } from '@vitejs/plugin-react' import babel from '@rolldown/plugin-babel' import { defineConfig, type Plugin } from 'vite' const require = createRequire(import.meta.url) const addDataLocator = require('./add-data-locator.cjs') const { openInAntigravityApp } = require('./server/open-in-antigravity-app.cjs') as { openInAntigravityApp: (locator: string) => Promise<{ success: boolean via?: string uri?: string }> } function clickToComponentServerPlugin(): Plugin { return { name: 'click-to-component-server', configureServer(server) { server.middlewares.use((req, res, next) => { if (req.url !== '/api/open-in-ide' || req.method !== 'POST') { next() return } let body = '' req.on('data', (chunk: Buffer | string) => { body += chunk.toString() }) req.on('end', async () => { const sendJson = (statusCode: number, payload: Record) => { res.statusCode = statusCode res.setHeader('Content-Type', 'application/json') res.end(JSON.stringify(payload)) } try { const parsedBody = JSON.parse(body || '{}') as { locator?: string target?: 'ide' | 'app' } const { locator, target = 'ide' } = parsedBody if (!locator) { sendJson(400, { message: 'Locator is required' }) return } if (target === 'app') { try { const result = await openInAntigravityApp(locator) sendJson(200, result) } catch (appError: unknown) { const message = appError instanceof Error ? appError.message : String(appError) console.error('[open-in-ide] Error opening in Antigravity App:', message) sendJson(500, { message: 'Failed to open file in Antigravity App Auxiliary Panel', error: message, }) } return } const isWindows = os.platform() === 'win32' const bin = isWindows ? 'antigravity-ide.cmd' : 'antigravity-ide' const cmd = `${bin} -r -g "${locator}"` exec(cmd, (error) => { if (!error) { sendJson(200, { success: true, via: 'antigravity-ide' }) return } const fallbackBin = isWindows ? 'code.cmd' : 'code' const fallbackCmd = `${fallbackBin} -r -g "${locator}"` exec(fallbackCmd, (fallbackError) => { if (!fallbackError) { sendJson(200, { success: true, via: 'code' }) return } const cursorBin = isWindows ? 'cursor.cmd' : 'cursor' const cursorCmd = `${cursorBin} -r -g "${locator}"` exec(cursorCmd, (cursorError) => { if (!cursorError) { sendJson(200, { success: true, via: 'cursor' }) return } console.error( `[open-in-ide] Error opening file: ${error.message} / Fallback error: ${fallbackError.message}`, ) sendJson(500, { message: 'Failed to open file in IDE', error: error.message, }) }) }) }) } catch (err: unknown) { const message = err instanceof Error ? err.message : String(err) console.error('[open-in-ide] Handler error:', message) sendJson(500, { message: 'Internal server error', error: message }) } }) }) }, } } const backendOrigin = process.env.VITE_BACKEND_ORIGIN || 'http://127.0.0.1:8000' const apiProxy = { '/api/shop': { target: backendOrigin, changeOrigin: true, }, '/habcoin': { target: backendOrigin, changeOrigin: true, }, '/upload-tmp-media': { target: backendOrigin, changeOrigin: true, }, '/media': { target: backendOrigin, changeOrigin: true, }, } // https://vite.dev/config/ export default defineConfig(({ command }) => ({ plugins: [ react(), babel({ presets: [reactCompilerPreset()], plugins: command === 'serve' ? [addDataLocator] : [], }), clickToComponentServerPlugin(), ], server: { host: '0.0.0.0', port: 5173, strictPort: true, allowedHosts: true, proxy: apiProxy, }, preview: { host: '0.0.0.0', port: 5178, strictPort: true, allowedHosts: true, proxy: apiProxy, }, }))