import { UserConfig, ConfigEnv, defineConfig } from 'vite'
import { resolve } from 'path';
import { createVitePlugins } from './vite/plugin';
import { generateModifyVars } from './vite/themeConfig';
import { proxy } from './vite/proxy';
const root = process.cwd()
// https://vitejs.dev/config/

export default ({ command, mode }: ConfigEnv): UserConfig => {
  console.log('vite >>>', command, mode);
  const isBuild = command === 'build'
  return {
    root,
    resolve: {
      alias: [
        {
          find: /^@\//,
          replacement: resolve(root, './src') + '/'
        }
      ]
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          modifyVars: generateModifyVars()
        }
      }
    },
    build: {
      target: 'es2015',
      terserOptions: {
        compress: {
          // TODO:
          keep_infinity: true,
          drop_console: true,
          drop_debugger: true
        }
      },
      // TODO:
      brotliSize: false,
      // TODO:
      chunkSizeWarningLimit: 2000,
    },
    plugins: createVitePlugins(isBuild),
    server: {
      hmr: {
        // 禁用服务器错误的遮罩层
        overlay: false
      },
      open: true,
      proxy
    }
  }
}
