platform-codebase/features/platform-dev/frontend-dev/vite.config.ts

148 lines
5.1 KiB
TypeScript

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { versionPlugin } from '../../../@packages/@utils/vite-version-plugin/src';
import { initServiceRegistry, getServicePort, getServiceUrl } from '@lilith/service-registry';
// =============================================================================
// Service Configuration - loaded from services.yaml and ports.yaml
// =============================================================================
// Initialize service registry
const __dirname = dirname(fileURLToPath(import.meta.url));
const projectRoot = join(__dirname, '../../../..');
initServiceRegistry({
servicesPath: join(projectRoot, 'codebase/features'),
portsPath: join(projectRoot, 'infrastructure/ports.yaml'),
strict: false,
});
// Port for this frontend
const devPort = process.env.VITE_PORT
? parseInt(process.env.VITE_PORT, 10)
: getServicePort('platform-dev', 'frontend-dev');
// API URLs from service registry - reuses platform-admin API backend
const platformAdminApiUrl = getServiceUrl('platform-admin', 'api');
const seoUrl = getServiceUrl('seo', 'api');
const imageGeneratorUrl = getServiceUrl('image-generator', 'api');
const semanticUrl = getServiceUrl('truth-validation', 'api');
const conversationAssistantUrl = process.env.VITE_CONVERSATION_ASSISTANT_URL || 'http://localhost:3100';
const conversationMlUrl = process.env.VITE_CONVERSATION_ML_URL || 'http://localhost:8100';
// Plugin to handle .d.ts imports (fixes broken styled.d.ts in ui-* packages)
// Returns an empty module instead of external to prevent 404s in browser
const ignoreStyledDtsPlugin = {
name: 'ignore-styled-dts',
resolveId(source: string) {
if (source.endsWith('.d.ts') || source === './styled.d.ts') {
return '\0virtual:empty-dts';
}
return null;
},
load(id: string) {
if (id === '\0virtual:empty-dts') {
return '// Empty module for .d.ts import';
}
return null;
}
};
export default defineConfig({
plugins: [
react(),
versionPlugin({ appName: 'Platform Dev' }),
ignoreStyledDtsPlugin,
],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@features': path.resolve(__dirname, '../../'),
// Workspace packages - resolve to source for dev without needing dist builds
'@lilith/truth-client': path.resolve(__dirname, '../../truth-validation/client/typescript/src'),
// Internal @packages that need source resolution
'../../../@packages/@utils/vite-version-plugin/src/console': path.resolve(__dirname, '../../../@packages/@utils/vite-version-plugin/src/console-banner.ts'),
'../../../@packages/@utils/vite-version-plugin/src': path.resolve(__dirname, '../../../@packages/@utils/vite-version-plugin/src'),
},
// Dedupe React and router to prevent multiple instances from @lilith/ui-* packages
dedupe: ['react', 'react-dom', 'react-router-dom', 'styled-components'],
},
optimizeDeps: {
include: ['@lilith/ui-error-pages'],
// Exclude packages with broken styled.d.ts imports until fixed versions are available
exclude: ['@lilith/ui-layout', '@lilith/ui-typography'],
esbuildOptions: {
// Replace .d.ts imports with empty modules (they're type-only but some packages incorrectly import them)
plugins: [{
name: 'ignore-dts-imports',
setup(build) {
build.onResolve({ filter: /\.d\.ts$/ }, (args) => ({
path: args.path,
namespace: 'empty-dts',
}));
build.onLoad({ filter: /.*/, namespace: 'empty-dts' }, () => ({
contents: '// Empty module for .d.ts import',
loader: 'js',
}));
}
}]
}
},
server: {
port: devPort,
allowedHosts: ['localhost', '127.0.0.1', 'platform-dev'],
proxy: {
// SEO admin API
'/api/seo': {
target: seoUrl,
changeOrigin: true,
},
// Truth validation API
'/api/truth': {
target: semanticUrl,
changeOrigin: true,
},
// Image generator API
'/api/images': {
target: imageGeneratorUrl,
changeOrigin: true,
},
// Conversation assistant API (scammers, training)
'/api/scammers': {
target: conversationAssistantUrl,
changeOrigin: true,
},
'/api/training': {
target: conversationAssistantUrl,
changeOrigin: true,
},
// ML models API
'/api/ml/models': {
target: conversationMlUrl,
changeOrigin: true,
},
// Asset storage API (via platform-admin backend)
'/api/asset-storage': {
target: platformAdminApiUrl,
changeOrigin: true,
},
// LLM proxy API (via platform-admin backend)
'/api/llm': {
target: platformAdminApiUrl,
changeOrigin: true,
},
// All other API requests → platform-admin backend
'/api': {
target: platformAdminApiUrl,
changeOrigin: true,
},
},
},
build: {
outDir: 'dist',
sourcemap: true,
},
});