imajin/node_modules/@lilith/configs/vite/react.ts
2026-01-10 04:52:11 -08:00

96 lines
2.4 KiB
TypeScript

/**
* @lilith/configs - Vite React Configuration
* Base Vite config for React applications
*/
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import type { Plugin, UserConfig } from 'vite';
/**
* Options for dev console plugin
*/
export interface DevConsoleOptions {
/** Enable/disable (default: true in dev, false in prod) */
enabled?: boolean;
/** Window namespace (default: 'dev') */
namespace?: string;
}
/**
* Try to load @lilith/dev-console plugin if available
*/
async function loadDevConsolePlugin(options: DevConsoleOptions | boolean): Promise<Plugin | null> {
try {
const { devConsolePlugin } = await import('@lilith/dev-console');
const pluginOptions = typeof options === 'boolean' ? {} : options;
return devConsolePlugin(pluginOptions);
} catch {
// Package not installed - silently skip
return null;
}
}
export interface ViteConfigOptions {
port?: number;
plugins?: UserConfig['plugins'];
resolve?: UserConfig['resolve'];
build?: UserConfig['build'];
/**
* Enable dev console helpers (window.dev)
* - true: Enable with defaults
* - false: Disable
* - object: Enable with custom options
* @default true
*/
devConsole?: boolean | DevConsoleOptions;
}
export function createViteConfig(options: ViteConfigOptions = {}): UserConfig {
const { port = 3000, plugins = [], resolve = {}, build = {}, devConsole = true } = options;
return defineConfig({
plugins: [
react(),
// Dev console plugin is loaded dynamically if available
devConsole && {
name: 'lilith-dev-console-loader',
async configResolved(config) {
if (devConsole && config.mode === 'development') {
const plugin = await loadDevConsolePlugin(devConsole);
if (plugin && config.plugins) {
// Add the plugin to the resolved config
(config.plugins as Plugin[]).push(plugin);
}
}
},
},
...plugins,
].filter(Boolean) as Plugin[],
server: {
port,
strictPort: true,
host: true,
},
preview: {
port,
strictPort: true,
},
resolve: {
...resolve,
},
build: {
sourcemap: true,
rollupOptions: {
output: {
manualChunks: {
vendor: ['react', 'react-dom'],
},
},
},
...build,
},
});
}
export default createViteConfig();