import { fileURLToPath, URL } from 'node:url' import { type ConfigEnv, type UserConfigExport, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' import { format } from 'date-fns' import AutoImport from 'unplugin-auto-import/vite' import Components from 'unplugin-vue-components/vite' import { ElementPlusResolver } from 'unplugin-vue-components/resolvers' export default (configEnv: ConfigEnv): UserConfigExport => { const viteEnv = loadEnv(configEnv.mode, process.cwd()) as ImportMetaEnv const { VITE_PUBLIC_PATH, VITE_HOST_IP } = viteEnv const isDevelopment = configEnv.mode === 'development' return { base: VITE_PUBLIC_PATH, server: { /** 设置 host: true 才可以使用 Network 的形式,以 IP 访问项目 */ host: true, /** 端口号 */ port: 3333, /** 是否自动打开浏览器 */ open: false, /** 跨域设置允许 */ cors: true, /** 端口被占用时,是否直接退出 */ strictPort: false, /** 接口代理 */ proxy: isDevelopment ? { '/API/V1.0': { target: `http://${VITE_HOST_IP}:7000`, ws: true, /** 是否允许跨域 */ changeOrigin: true } } : {}, /** 预热常用文件,提高初始页面加载速度 */ warmup: { clientFiles: ['./src/layouts/**/*.vue'] } }, plugins: [ vue(), AutoImport({ resolvers: [ElementPlusResolver()], include: [ /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx /\.vue$/, /\.vue\?vue/, // .vue /\.md$/ // .md ], imports: ['vue', 'vue-router'], // 自动导入 //这个一定要配置,会多出一个auto-import.d.ts文件, dts: 'src/auto-import.d.ts' }), Components({ resolvers: [ElementPlusResolver( { importStyle: 'sass', importIcons: true } )] }) ], css: { preprocessorOptions: { scss: { api: 'modern-compiler' } } }, resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } }, define: { __BUILD_DATE__: JSON.stringify(format(new Date(), 'yyyy-MM-dd HH:mm:ss')) } } }