import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import * as path from 'path'
import AutoImport from 'unplugin-auto-import/vite'

// unocss
import UnoCSS from 'unocss/vite'

// Icons 自动按需引入图标库
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { FileSystemIconLoader } from 'unplugin-icons/loaders'

// Ant Design Vue 4.x 自动按需引入组件
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "")
  const proxyObj = {}
  console.log('mode-----当前环境', mode, env.VITE_APP_BASE_API,env.VITE_APP_BASE_URL);
  if (mode === 'development' && env.VITE_APP_BASE_API && env.VITE_APP_BASE_URL) {
    const httpsRE = /^https:\/\//;
    const isHttps = httpsRE.test(env.VITE_APP_BASE_URL);
    proxyObj[env.VITE_APP_BASE_API] = {
      target: env.VITE_APP_BASE_URL,
      changeOrigin: true,
      // rewrite: path => path.replace(new RegExp(`^${env.VITE_APP_BASE_API}`), ''),
      ws: true,
      ...(isHttps ? { secure: false } : {}),
    }
  }
  return {
    server: {
      port: 9000,
      proxy: {
        // '/jeecgboot': {
        //   target: 'http://47.99.151.233:9000',
        //   changeOrigin: true,
        //   followRedirects: true, // Cookie支持重定向
        //   // rewrite: (path) => path.replace(/^\/API/, ''),
        // },
        ...proxyObj
      },
    },
    plugins: [
      vue(),
      AutoImport({
        imports: ['vue', 'vue-router'],
        dts: true,
        resolvers: [],
      }),
      Components({
        resolvers: [
          // Ant Design Vue 4.x 自动按需引入组件
          AntDesignVueResolver({
            importStyle: false, // css in js
          }),
          IconsResolver({
            customCollections: ['custom'],
          }),
        ],
      }),
      UnoCSS(),
      Icons({
        autoInstall: true,
        compiler: 'vue3',
        customCollections: {
          custom: FileSystemIconLoader('src/assets/icons'),
        },
      }), // 自动安装
    ],
    resolve: {
      // 设置别名
      alias: {
        '@': path.resolve(__dirname, 'src'),
        Assets: path.resolve(__dirname, 'src/assets'),
        Components: path.resolve(__dirname, 'src/components'),
        Utils: path.resolve(__dirname, 'src/utils'), // 工具类方法（新创建的）
        Config: path.resolve(__dirname, 'src/config'),
        Views: path.resolve(__dirname, 'src/views'),
        Plugins: path.resolve(__dirname, 'src/plugins'),
        Routes: path.resolve(__dirname, 'src/routes'),
        API: path.resolve(__dirname, 'src/api'),
        Store: path.resolve(__dirname, 'src/store'),
        Types: path.resolve(__dirname, 'types'),
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import (reference) "${path.resolve(__dirname, 'src/assets/styles/variables.less')}";`,
          },
          javascriptEnabled: true,
        },
      },
    },
    build: {
      target: 'ESNext',
      minify: 'esbuild',
      // rollup 配置
      rollupOptions: {
        output: {
          chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
          entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
          assetFileNames: '[ext]/[name]-[hash].[ext]', // 资源文件像 字体，图片等
        },
      },
    },
    esbuild: {
      drop: [
        // 'console', // 如果线上需要打印，就把这行注释掉
        'debugger',
      ],
    },
    define: {
      'process.env.ENABLE_ANALYTICS': JSON.stringify(process.env.NODE_ENV === 'production')
    }
  }
})

