import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { ViteEjsPlugin } from 'vite-plugin-ejs'
import path from 'path'
// unplugin-vue-components 会解析模板并自动注册对应的组件,使用函数组件时，unplugin-vue-components 无法自动引入对应的样式，因此需要手动引入样式
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// https://vitejs.dev/config/
// @ts-ignore
export default ({ mode }) =>
  defineConfig({
    plugins: [
      vue(),
      vueJsx(),
      Components({
        resolvers: [ElementPlusResolver()]
      }),
      ViteEjsPlugin(() => {
        return {
          // BASE_URL: loadEnv(mode, process.cwd()).VITE_APP_BASE_DOMIAN,
          TITLE: '上海市CIM平台'
          // JS_MOUDLES: ['/websdk/latest/cimcube/cimcube.js'],
          // CSS_MOUDLES: [
          //   '/websdk/latest/cimcube/Widgets/widgets.css',
          //   '/websdk/latest/Resource/css/Widgets.css'
          // ]
        }
      }),
    ],
    base: 'a',
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@assets': path.resolve(__dirname, './src/assets'),
        '@common': path.resolve(__dirname, './src/common'),
        '@utils': path.resolve(__dirname, './src/utils'),
        '@components': path.resolve(__dirname, './src/components'),
        '@views': path.resolve(__dirname, './src/views'),
        '@styles': path.resolve(__dirname, './src/styles')
      }
    },
    // 服务配置
    server: {
      host: '0.0.0.0',
      port: 4450, // 类型： number 指定服务器端口;
      open: true, // 类型： boolean | string在服务器启动时自动在浏览器中打开应用程序；
      proxy: {
        '/api': {
          // target: `https://www${envStr}.bimface.com/api`,
          target: loadEnv(mode, process.cwd()).VITE_APP_BASE_DOMIAN,
          changeOrigin: true,
          rewrite: (apiPath) => apiPath.replace(/^\/api/, ''),
          cookieDomainRewrite: 'localhost'
        },
        '/bf': {
          // target: `https://www${envStr}.bimface.com/api`,
          target: loadEnv(mode, process.cwd()).VITE_APP_BASE_DOMIAN,
          changeOrigin: true,
          //rewrite: (apiPath) => apiPath.replace(/^\/bf/, ''),
          cookieDomainRewrite: 'localhost'
        }
      }
    },
    // css 处理
    css: {
      preprocessorOptions: {
        scss: {
          /* .scss全局预定义变量，引入多个文件 以;(分号分割)*/
          additionalData: '@import "./src/styles/elementPlus/var.scss";'
        }
      }
    },
    //  生产环境
    build: {
      minify: 'terser',
      target: 'es2015',
      // 指定输出路径
      assetsDir: './',
      // 指定输出文件路径
      outDir: 'dist',
      // 代码压缩配置
      terserOptions: {
        // 生产环境移除console
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      },
      chunkSizeWarningLimit: 2000
    },
  })
