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'
import { visualizer } from 'rollup-plugin-visualizer' // Bundle 分析
// 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')
      }
    },
    define: {
      'process.env.NODE_ENV': '"production"'
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "@/styles/index.scss" as *;'
        }
      }
    },
    server: {
      open: false,//打开浏览器
      host: '0.0.0.0',
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,//需要代理跨域
          rewrite: (path) => path.replace(/^\/api/, '')
        },
        // '/testapi': {
        //   target: 'https://erptest.baozhida.cn',
        //   changeOrigin: true
        // },
        // '/ams': {
        //   target: 'http://erp.baozhida.cn',
        //   changeOrigin: true
        // }
      }
    },
    plugins: [
      vue(),
      createHtmlPlugin({
        inject: {
          data: {
            title: env.VITE_BZD_ERP_APP_TITLE
          }
        }
      }),
      visualizer(),
      // eslintPlugin({
      //   include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue']
      // }),
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      })
    ],
	  optimizeDeps: {
		  include: ['element-plus/es/locale/lang/zh-cn'],
	  },
    // * 打包去除 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: 'ERP'
      // 打包后静态目录名称
      // assetsDir: 'static'
    }
  }
})
