import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import VitePluginHtmlEnv from 'vite-plugin-html-env';
import { resolve } from 'path';
import WindiCSS from 'vite-plugin-windicss';
// import consola from 'consola';
import mkcert from 'vite-plugin-mkcert';
// import eslintPlugin from 'vite-plugin-eslint'
// import requireTransform from 'vite-plugin-require-transform'
import { chunkSplitPlugin } from 'vite-plugin-chunk-split';

function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir);
}

// https://vitejs.dev/config/
export default ({ mode }) =>
  defineConfig({
    base: loadEnv(mode, process.cwd()).VITE_PUBLIC_DIR,
    plugins: [
      // eslintPlugin({
      //   include: ['src/**/*.ts', 'src/**/*.vue', 'src/*.js', 'src/*.vue']
      // }),
      //@ts-ignore
      chunkSplitPlugin({
        strategy: 'default',
        customSplitting: {
          'vue-vendor': ['vue'],
          utils: [/src\/utils/],
        },
      }),
      mkcert(),
      vue(),
      WindiCSS(),
      VitePluginHtmlEnv({
        prefix: '<{',
        suffix: '}>',
        envPrefixes: ['VITE_', 'VUE_'],
      }),
    ],
    resolve: {
      alias: [
        {
          find: /\/@\//,
          replacement: pathResolve('src') + '/',
        },
        {
          find: /\/#\//,
          replacement: pathResolve('types') + '/',
        },
      ],
    },
    server: {
      https: Number(loadEnv(mode, process.cwd()).VITE_USE_HTTPS) == 1,
      host: true,
      port: 6100,
      headers: {
        'Cache-Control': 'no-store',
      },
      proxy: {
        '/service': {
          target: loadEnv(mode, process.cwd()).VITE_APP_APIS_URL,
          changeOrigin: true,
        },
        '/takelook': {
          target: loadEnv(mode, process.cwd()).VITE_APP_APIS_URL,
          changeOrigin: true,
        },
      },
    },
    build: {
      // minify: false,
      minify: 'terser',
      // target: 'es2015',
      // cssTarget: 'chrome86',
      outDir: 'dist',
      terserOptions: {
        compress: {
          keep_infinity: true,
          // Used to delete console in production environment
          drop_console: false,
        },
      },
      // Turning off brotliSize display can slightly reduce packaging time
      // brotliSize: false,
      chunkSizeWarningLimit: 2000,
    },
  });
