import { defineConfig, type Plugin } from 'vite' import react from '@vitejs/plugin-react' import path from 'path' import fs from 'fs' import { fileURLToPath } from 'url' import { earthRuntimePlugin } from './scripts/earth-runtime-plugin.mjs' const EARTH_BUILTIN_ASSET_PREFIX = '@earth-builtin-asset/' const VIRTUAL_EARTH_ASSET_PREFIX = '\0earth-builtin-asset:' const EARTH_BUILTIN_CONTENT_TYPES: Record = { '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.svg': 'image/svg+xml', } function earthBuiltinAssetsPlugin(rootDir: string): Plugin { let isBuild = false let isAdminOnly = false const assetsRoot = path.resolve(rootDir, 'public/earth/assets') return { name: 'earth-builtin-assets', enforce: 'pre', configResolved(config) { isBuild = config.command === 'build' isAdminOnly = config.publicDir === false }, resolveId(source) { if (!source.startsWith(EARTH_BUILTIN_ASSET_PREFIX)) return null const rel = source.slice(EARTH_BUILTIN_ASSET_PREFIX.length).replace(/^\/+/, '') const absPath = path.resolve(assetsRoot, rel) if (!absPath.startsWith(assetsRoot + path.sep) || !fs.existsSync(absPath)) { return null } return `${VIRTUAL_EARTH_ASSET_PREFIX}${rel}` }, load(id) { if (!id.startsWith(VIRTUAL_EARTH_ASSET_PREFIX)) return null const rel = id.slice(VIRTUAL_EARTH_ASSET_PREFIX.length) const absPath = path.resolve(assetsRoot, rel) if (isBuild) { const refId = this.emitFile({ type: 'asset', name: path.basename(rel), source: fs.readFileSync(absPath), }) return `export default "__VITE_ASSET__${refId}__"` } if (isAdminOnly) { return `export default "/__earth_builtin_assets/${rel}"` } return `export default "/earth/assets/${rel}"` }, configureServer(server) { if (!isAdminOnly) return server.middlewares.use('/__earth_builtin_assets', (req, res, next) => { const rel = decodeURIComponent((req.url || '').split('?')[0]).replace(/^\/+/, '') const absPath = path.resolve(assetsRoot, rel) if ( !absPath.startsWith(assetsRoot + path.sep) || !fs.existsSync(absPath) || !fs.statSync(absPath).isFile() ) { return next() } const ext = path.extname(absPath).toLowerCase() res.setHeader('Content-Type', EARTH_BUILTIN_CONTENT_TYPES[ext] || 'application/octet-stream') fs.createReadStream(absPath).pipe(res) }) }, } } const frontendTarget = process.env.PLANET_FRONTEND_TARGET === 'admin' ? 'admin' : 'full' const isAdminOnlyTarget = frontendTarget === 'admin' const backendPort = process.env.PLANET_BACKEND_PORT || '8000' const backendHttpTarget = `http://localhost:${backendPort}` const backendWsTarget = `ws://localhost:${backendPort}` const __dirname = path.dirname(fileURLToPath(import.meta.url)) let adminOutDir = 'dist' const frontendHttpsEnabled = process.env.PLANET_FRONTEND_HTTPS === '1' const frontendHttpsKeyPath = process.env.PLANET_FRONTEND_HTTPS_KEY const frontendHttpsCertPath = process.env.PLANET_FRONTEND_HTTPS_CERT const frontendHttps = frontendHttpsEnabled && frontendHttpsKeyPath && frontendHttpsCertPath ? { key: fs.readFileSync(frontendHttpsKeyPath), cert: fs.readFileSync(frontendHttpsCertPath), } : undefined export default defineConfig({ define: { __PLANET_FRONTEND_TARGET__: JSON.stringify(frontendTarget), }, plugins: [ earthBuiltinAssetsPlugin(__dirname), ...(!isAdminOnlyTarget ? [ earthRuntimePlugin(__dirname), { name: 'legacy-earth', configureServer(server) { server.middlewares.use('/legacy/earth', (req, res, next) => { let url = req.url || '/' if (url === '' || url === '/') { url = '/3dearthmult.html' } const filePath = path.resolve(__dirname, 'legacy/3dearthmult' + url) if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) { const ext = path.extname(filePath).toLowerCase() const contentTypes = { '.html': 'text/html', '.json': 'application/json', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.png': 'image/png', '.js': 'application/javascript', '.css': 'text/css', } res.setHeader('Content-Type', contentTypes[ext] || 'text/plain') fs.createReadStream(filePath).pipe(res) } else { next() } }) }, }, ] : [ { name: 'admin-favicon-copy', configResolved(config) { adminOutDir = config.build.outDir }, closeBundle() { const targetDir = path.resolve(__dirname, adminOutDir || 'dist') fs.mkdirSync(targetDir, { recursive: true }) for (const fileName of ['favicon.ico', 'favicon.png']) { const src = path.join(__dirname, 'public', fileName) if (fs.existsSync(src)) { fs.copyFileSync(src, path.join(targetDir, fileName)) } } }, }, ]), react(), ], server: { port: 3000, https: frontendHttps, allowedHosts: true, proxy: { '/api/health': { target: backendHttpTarget, changeOrigin: true, secure: false, rewrite: (reqPath) => reqPath.replace(/^\/api/, ''), }, '/api': { target: backendHttpTarget, changeOrigin: true, secure: false, }, '/ws': { target: backendWsTarget, ws: true, changeOrigin: true, secure: false, }, '/health': { target: backendHttpTarget, changeOrigin: true, secure: false, }, '/earth-brand-assets': { target: backendHttpTarget, changeOrigin: true, secure: false, }, }, fs: { allow: ['..'], }, }, publicDir: isAdminOnlyTarget ? false : 'public', resolve: { alias: { '@': path.resolve(__dirname, './src'), }, }, optimizeDeps: { entries: ['src/main.tsx'], exclude: ['satellite.js'], }, build: { rollupOptions: { output: { manualChunks(id) { if (!id.includes('node_modules')) { return } if ( id.includes('/react/') || id.includes('/react-dom/') || id.includes('/react-router-dom/') || id.includes('/scheduler/') ) { return 'react-vendor' } if (id.includes('/axios/')) { return 'network-vendor' } if (id.includes('/echarts/') || id.includes('/zrender/')) { return 'charts-vendor' } if ( id.includes('/@radix-ui/') || id.includes('/@tanstack/react-table/') || id.includes('/lucide-react/') ) { return 'admin-vendor' } if ( !isAdminOnlyTarget && (id.includes('/three/') || id.includes('/simplex-noise/') || id.includes('/satellite.js/')) ) { return 'earth-vendor' } }, }, }, }, })