import { defineConfig } from 'vite'

import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'url'

import AutoImport from 'unplugin-auto-import/vite'
import { visualizer } from 'rollup-plugin-visualizer'
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import viteCompression from 'vite-plugin-compression'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
	console.log(mode)
	return {
		resolve: {
			alias: {
				'@': fileURLToPath(new URL('./src', import.meta.url)),
			},
		},
		optimizeDeps: {
			include: ['@ant-design/icons-vue', 'ant-design-vue'],
		},
		plugins: [
			vue(),
			vueJsx(),
			vueDevTools(),
			process.env.npm_lifecycle_event === 'report'
				? visualizer({ open: true, brotliSize: true, filename: 'report.html' })
				: null,
			viteCompression({
				threshold: 1024000, // 对大于 1mb 的文件进行压缩
			}),
			AutoImport({
				dts: 'src/auto-imports.d.ts',
				imports: ['vue', 'vue-router', '@vueuse/core'],

				eslintrc: {
					enabled: true,
					filepath: './.eslintrc-auto-import.json',
					globalsPropValue: true,
				},
			}),
			Components({
				dts: 'src/components.d.ts',
				deep: true,
				dirs: ['src/components'],
				extensions: ['vue', 'tsx'],
				resolvers: [
					AntDesignVueResolver({
						importStyle: false,
					}),
				],
			}),
		],
		server: {
			open: true,
			proxy: {
				'/api': {
					target: 'https://ims.riemann.tech/api/',
					// target: 'http://127.0.0.1:8888',
					changeOrigin: true,
					rewrite: (path) => path.replace(/^\/api/, ''),
				},
			},
		},
		esbuild: {
			drop: ['console', 'debugger'], // 删除 所有的console 和 debugger
		},
		build: {
			chunkSizeWarningLimit: 1500,
			rollupOptions: {
				output: {
					//每个node_modules模块分成一个js文件
					manualChunks(id) {
						if (id.includes('node_modules')) {
							return id.toString().split('node_modules/')[1].split('/')[1].toString()
						}
					},
					// 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
					entryFileNames: 'assets/js/[name].[hash].js', // 用于命名代码拆分时创建的共享块的输出命名
					chunkFileNames: 'assets/js/[name].[hash].js', // 用于输出静态资源的命名，[ext]表示文件扩展名
					assetFileNames: 'assets/[ext]/[name].[hash].[ext]',
				},
			},
		},
	}
})
