import { resolve } from 'path';
import { ConfigEnv, loadEnv, UserConfigExport } from 'vite';
// @ts-ignore
import { getPluginsList } from './build/plugins';
// @ts-ignore
import { buildEnvironment } from './build/buildEnv';
// @ts-ignore
import { serverOptions } from './build/server';
// @ts-ignore
import { exclude, include } from './build/optimize';
// @ts-ignore
import { pathResolve } from './build/utils.ts';
// @ts-ignore
import { create } from './build/version';
import { root, wrapperEnv } from './build/utils';

create();

export default ({ mode }: ConfigEnv): UserConfigExport => {
	const { VITE_CDN, VITE_COMPRESSION, VITE_PORT, VITE_MOCK_SERVER_ENABLE } = wrapperEnv(loadEnv(mode, root));
	return {
		resolve: {
			alias: {
				'@': resolve(__dirname, './src'),
				'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
				'@build': pathResolve(),
			},
		},
		optimizeDeps: { include, exclude },
		server: serverOptions(mode),
		preview: serverOptions(mode),
		plugins: getPluginsList(VITE_CDN, VITE_COMPRESSION, VITE_PORT, VITE_MOCK_SERVER_ENABLE),
		esbuild: {
			pure: ['console.log', 'debugger'],
			jsxFactory: 'h',
			jsxFragment: 'Fragment',
			jsxInject: "import { h } from 'vue';",
		},
		// 配置构建过程的选项，例如是否生成压缩文件和源映射
		build: buildEnvironment(),
	};
};
