import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import path from "path";
import tailwindcss from "tailwindcss";
import uniTailwind from '@uni-helper/vite-plugin-uni-tailwind';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { resolve } from 'node:path';
import viteCompression from "vite-plugin-compression";
import { env as envConfig } from './src/config';
// https://vitejs.dev/config/
const env = JSON.parse(process.env.UNI_CUSTOM_DEFINE || '{}');
const mode = env.IE_ENV || 'development';
const baseUrl = envConfig[mode]?.serverBaseUrl || '';
console.log('当前模式：', mode);
console.log('当前baseUrl：', baseUrl);

export default defineConfig(({ mode }) => ({
  outDir: 'h5',
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
      '~': resolve(__dirname, '')
    }
  },
  server: {
    port: 5174,
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
  plugins: [
    viteCompression({
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz'
    }),
    AutoImport({
      dts: 'src/auto-imports.d.ts',
      imports: [
        'vue',
        'uni-app',
        'pinia'
      ],
      exclude: ['createApp'],
      eslintrc: {
        enabled: true
      }
    }),
    Components({
      dirs: [
        'src/components'
      ],
      extensions: ['vue', 'tsx'],
      include: [
        'src/**/*.vue',
        'src/**/*.tsx'
      ],
      directoryAsNamespace: true,
    }),
    uni(),
    uniTailwind(),
  ],
  css: {
    postcss: {
      plugins: [tailwindcss()],
    },
  }
}));
