import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import {fileURLToPath} from 'url'

export default defineConfig(({mode, command}) => {
	const env = loadEnv(mode, process.cwd())
	const {VITE_API_BASE, VITE_APP_ENV} = env
	return {
		resolve: {
			alias: {
				// 设置路径
				'~': path.resolve(__dirname, './'),
				// 设置别名
				'@': fileURLToPath(new URL('./src', import.meta.url)),
				// 资源地址
				'@assets': path.resolve(__dirname, 'src/assets')
			},
			extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
		},
		base: VITE_APP_ENV == 'production' ? '/super/' : '/',
		// vite 相关配置
		server: {
			port: 8611,
			host: true,
			open: true,
			proxy: {
				'/api': {
					target: VITE_API_BASE,
					changeOrigin: true,
					rewrite: (p) => p.replace(/^\/api/, '')
				}
			},
		},
		build: {
			outDir: 'dist',
			assetsDir: 'static',
			chunkSizeWarningLimit: 2000,
			sourcemap: true,
			rollupOptions: {
				output: {
					manualChunks(id) {
						if (id.includes('node_modules')) {
							return 'vendor'
						}
					}
				}
			}
		},
		plugins: [
			vue(),
			{
				name: 'full-reload',
				handleHotUpdate({file, server}) {
					if (file.endsWith('.vue')) {
						server.ws.send({type: 'full-reload'});
					}
				},
			},
		]
	}
})
