import { defineConfig, loadEnv, type ConfigEnv, type UserConfig } from 'vite'
import { initVitePlugins } from './plugins'
import { initProxy } from './plugins/proxy'
import { resolve } from 'path'
import { wrapperEnv } from './getEnv'
const pathSrc = resolve(__dirname, 'src')
// https://vite.dev/config/
export default defineConfig(({ mode, command }: ConfigEnv): UserConfig => {
  const root = process.cwd()
  const env = loadEnv(mode, root)
  const viteEnv = wrapperEnv(env)
  return {
    server: {
      port: viteEnv.VITE_PORT,
      open: viteEnv.VITE_OPEN,
      proxy: initProxy(viteEnv.VITE_PROXY)
    },
    resolve: {
      alias: {
        '@': pathSrc
      }
    },
    plugins: initVitePlugins(viteEnv, command === 'build'),
    build: {
      outDir: 'dist',
      minify: 'esbuild',
      sourcemap: false
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/assets/styles/element/index.scss" as *;`
        }
      }
    },
    optimizeDeps: {
      include: ['vue', 'vue-router', 'pinia', 'axios', '@vueuse/core', 'element-plus', '@element-plus/icons-vue']
    }
  }
})
