import type { BuildOptions } from 'vite';

export const buildEnvironment = () => {
	const environment: BuildOptions = {
		assetsInlineLimit: 20000,
		// 构建输出的目录,默认值为"dist"
		outDir: 'dist',
		// 用于指定使用的代码压缩工具。在这里,minify 被设置为 'terser',表示使用 Terser 进行代码压缩。默认值terser
		// esbuild 打包更快,但是不能去除 console.log,terser打包慢,但能去除 console.log
		minify: 'terser',
		// 用于配置 Terser 的选项
		terserOptions: {
			// 用于配置压缩选项
			compress: {
				drop_console: true, // 是否删除代码中的 console 语句, 默认值false
				drop_debugger: true, // 是否删除代码中的 debugger 语句, 默认值false
			},
		},
		// 禁用 gzip 压缩大小报告,可略微减少打包时间
		reportCompressedSize: false,
		// 用于指定是否生成源映射文件。源映射文件可以帮助调试和定位源代码中的错误。当设置为false时,构建过程不会生成源映射文件
		sourcemap: false,
		// 用于配置 CommonJS 模块的选项
		commonjsOptions: {
			// 用于指定是否忽略 CommonJS 模块中的 try-catch 语句。当设置为false时,构建过程会保留 CommonJS 模块中的 try-catch 语句
			ignoreTryCatch: false,
		},
		// 规定触发警告的 chunk 大小, 当某个代码分块的大小超过该限制时,Vite 会发出警告
		chunkSizeWarningLimit: 2000,
		// 用于配置 Rollup 打包工具的选项
		rollupOptions: {
			// 用于配置输出选项
			output: {
				// 静态资源分类和包装
				chunkFileNames: 'js/[name]-[hash].js', // 用于指定代码分块的输出文件名格式
				entryFileNames: 'js/[name]-[hash].js', // 用于指定入口文件的输出文件名格式
				assetFileNames: 'assets/[ext]/[name]-[hash].[ext]', // 用于指定静态资源的输出文件名格式
				// ? 配置文件生成方式
				manualChunks: (id, meta) => {
					// 如果是包含在包中则打包成 vendor
					if (id.includes('node_modules')) {
						return 'vendor';
					}
				},
			},
		},
	};

	return environment;
};