import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import { defineConfig, ConfigEnv } from 'vite';
import vueSetupExtend from 'vite-plugin-vue-setup-extend-plus'; // setup语法糖设置name
import viteCompression from 'vite-plugin-compression'; //开启gzip打包压缩
import vueJsx from '@vitejs/plugin-vue-jsx'; // jsx
import { visualizer } from "rollup-plugin-visualizer";
import { lazyImport, VxeResolver } from 'vite-plugin-lazy-import'
// @ts-ignore
export default defineConfig((mode: ConfigEnv) => {
	return {
		plugins: [
			vue(),
			vueSetupExtend(),
			viteCompression(),
			vueJsx({
				// options are passed on to @vue/babel-plugin-jsx
			}),
			visualizer({
				open: true,  // true 打包完自动打开分析页面，false 不会自动弹出
				filename: "stats.html", // 分析图生成的文件名
				gzipSize: true, // 是否统计并显示gzip
				brotliSize: true, // 是否统计并显示brotli
			}),
			lazyImport({
				resolvers: [
					VxeResolver({
						libraryName: 'vxe-table'
					}),
					VxeResolver({
						libraryName: 'vxe-pc-ui'
					})
				]
			})
		],
		root: process.cwd(),
		resolve: { alias: { '@': resolve(__dirname, './src') } },
		base: mode.command === 'serve' ? './' : '/',
		hmr: true,
		server: {
			host: '0.0.0.0',
			port: 8888,
			open: true, // 是否打开浏览器
		},
		build: {
			outDir: 'dist',
			sourcemap: false, // 是否生成 sourceMap 文件
			reportCompressedSize: false, //   关闭文件计算
			chunkSizeWarningLimit: 1500,
			rollupOptions: {
				output: {
					// 最小化拆分包
					manualChunks(id: any): string {
						if (id.includes('node_modules')) {
							return id.toString().split('node_modules/')[1].split('/')[0].toString();
						}
					},
					entryFileNames: `assets/[name].[hash].js`,
					chunkFileNames: `assets/[name].[hash].js`,
					assetFileNames: `assets/[name].[hash].[ext]`,
					compact: true,
				},
			},
			minify: 'terser',
			terserOptions: {
				//生产环境时移除console和debugger
				compress: {
					drop_console: false,
					drop_debugger: true,
				},
			},
		},
		css: {
			preprocessorOptions: {
				css: { charset: false },
				scss: {
					api: 'modern-compiler', // or 'modern'
					silenceDeprecations: ['legacy-js-api'],
				},
			},
		},
	};
});
