import path from 'node:path';

import vue from '@vitejs/plugin-vue';
import { defineConfig, loadEnv } from 'vite';
import mkcert from 'vite-plugin-mkcert';

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), ['VITE_']);
  const isDev = env.VITE_NODE_ENV === 'development';

  return {
    // 服务器配置
    server: isDev
      ? {
          port: Number(env.VITE_PORT),
          // cors: true,
        }
      : undefined,

    // 插件配置
    plugins: [vue(), isDev && mkcert()],

    // 解析配置
    resolve: {
      alias: {
        '@': path.resolve(process.cwd(), 'src'),
        $vtj: path.resolve(process.cwd(), '.vtj'),
      },
    },

    // 定义全局变量
    define: {
      'process.env': JSON.stringify(env),
    },
  };
});
