import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
    build: {
        rollupOptions: {
            output: {
                manualChunks: (id, { getModuleInfo }) => {
                    // 将 vendor 库分离到单独的 chunk
                    if (id.includes('node_modules')) {
                        // 按库名称分离 - 已优化的 chunk 配置
                        if (id.includes('@alpinejs')) {
                            return 'chunk-alpine';
                        }
                        if (id.includes('tailwindcss') || id.includes('daisyui')) {
                            return 'chunk-ui';
                        }
                        if (id.includes('@popperjs') || id.includes('tippy')) {
                            return 'chunk-popper';
                        }
                        if (id.includes('filament') || id.includes('forms')) {
                            return 'chunk-filament';
                        }
                        if (id.includes('livewire')) {
                            return 'chunk-livewire';
                        }
                        if (id.includes('@heroicons') || id.includes('lucide')) {
                            return 'chunk-icons';
                        }
                        if (id.includes('intersect') || id.includes('focus-trap')) {
                            return 'chunk-utils';
                        }
                        // 其他小库合并
                        return 'chunk-vendor';
                    }
                },
            },
        },
        // 将警告阈值提高到 1500KB (对于 Filament 应用是合理的)
        chunkSizeWarningLimit: 1500,
        // 启用高级压缩
        minify: 'terser',
        terserOptions: {
            compress: {
                drop_console: true,
                drop_debugger: true,
                pure_funcs: ['console.log'],
                passes: 2, // 压缩两遍获得更好效果
            },
        },
    },
});
