import { defineConfig, loadEnv } from 'vite';

const { resolve } = require('path');
import createVitePlugins from './vite/plugins';
// https://vitejs.dev/config/
export default ({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_APP_BASE } = env;
  return defineConfig({
    base: VITE_APP_BASE,
    define: {
      __VUE_I18N_FULL_INSTALL__: true,
      __VUE_I18N_LEGACY_API__: true,
      __INTLIFY_PROD_DEVTOOLS__: false,
    },
    server: {
      port: 8083,
      host: '0.0.0.0',
      proxy: {
        '/api': {
          target: 'http://localhost:8123',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, ''),
        },
        // // 武术业务模块保留 /api 前缀
        // '/api': {
        //   target: 'http://localhost:8123',
        //   changeOrigin: true,
        // },
      },
    },
    resolve: {
      alias: {
        '~': resolve(__dirname, './'),
        '@': resolve(__dirname, './src'),
        components: resolve(__dirname, './src/components'),
        styles: resolve(__dirname, './src/styles'),
        utils: resolve(__dirname, './src/utils'),
      },
    },
    plugins: createVitePlugins(env, command === 'build'),
    build: {
      target: 'esnext',
    },
    optimizeDeps: {
      esbuildOptions: {
        target: 'esnext',
      },
    },
  });
};
