import { defineConfig, loadEnv, ConfigEnv, UserConfig } from 'vite'
import { createHtmlPlugin } from 'vite-plugin-html'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import path from 'path'
// import eslintPlugin from "vite-plugin-eslint";
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, process.cwd())

  return {
    resolve: {
      alias: {
        '~': resolve(__dirname, './'),
        '@': resolve(__dirname, './src'),
        '@assets': path.resolve(__dirname, './assets/images')
      }
    },
    server: {
      open: true,
      host: '0.0.0.0',
      proxy: {
        '/api': {
          target: 'https://erp.baozhida.cn',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, '/testapi')
        },
        '/testapi': {
          target: 'https://erp.baozhida.cn',
          changeOrigin: true
        },
        '/ams': {
          target: 'http://erp.baozhida.cn',
          changeOrigin: true
        }
      }
    },
    plugins: [
      vue(),
      createHtmlPlugin({
        inject: {
          data: {
            title: env.VITE_BZD_ERP_APP_TITLE
          }
        }
      }),
      // eslintPlugin({
      //   include: ["src/**/*.js", "src/**/*.vue", "src/*.js", "src/*.vue"],
      // }),
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      })
    ],
    // * 打包去除 console.log && debugger
    esbuild: {
      pure: env.VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : []
    }
    // build: {
    //   rollupOptions: {
    //     output: {
    //       // 打包后文件命名
    //       assetFileNames: '[hash].[name].[ext]'
    //     }
    //   },
    //   // 图片大小分界线：大于 4kb，图片正常打包。小于 4kb，图片会转化为 base64。
    //   assetsInlineLimit: 4096, // 4kb
    //   // 打包后名称，默认是 dist
    //   outDir: 'testDir',
    //   // 打包后静态目录名称
    //   assetsDir: 'static'
    // }
  }
})
