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
// @ts-ignore
export default defineConfig((mode: ConfigEnv) => {
	return {
		plugins: [
			vue(),
			vueSetupExtend(),
			viteCompression(),
			vueJsx({
				// options are passed on to @vue/babel-plugin-jsx
			}),
		],
		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,
			chunkSizeWarningLimit: 1500,
			rollupOptions: {
				output: {
					entryFileNames: `assets/[name].[hash].js`,
					chunkFileNames: `assets/[name].[hash].js`,
					assetFileNames: `assets/[name].[hash].[ext]`,
					compact: true,
					manualChunks: {
						vue: ['vue', 'vue-router', 'pinia'],
					},
				},
			},
			minify: 'terser',
			terserOptions: {
				//生产环境时移除console和debugger
				compress: {
					drop_console: false,
					drop_debugger: true,
				},
			},
		},
		css: { preprocessorOptions: { css: { charset: false } } },
	};
});
