import path from 'node:path';

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

// @ts-ignore
export default defineConfig(({ mode }) => {
  console.log('mode', 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,
    // @ts-ignore coding
    vitePlugins: [vue(), mkcert({ source: 'coding' })],
    compilation: {
      resolve: {
        alias: {
          '@': path.resolve(process.cwd(), 'src'),
          $vtj: path.resolve(process.cwd(), '.vtj'),
        },
      },
      define: {
        // 注入环境变量到客户端
        'process.env': JSON.stringify(env),
      },
    },
  };
});
