import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from 'unplugin-vue-components/resolvers';
import viteESLint from 'vite-plugin-eslint';
import legacy from '@vitejs/plugin-legacy';
import { terser } from 'rollup-plugin-terser';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');

function resolve(dir: string) {
  return path.join(__dirname, dir);
}
// https://vitejs.dev/config/
// https://github.com/vitejs/vite/issues/1930 .env
const proxyUrl = 'https://test.dayaedu.com';
export default defineConfig({
  base: './',
  plugins: [
    legacy({
      targets: ['chrome 52'],
      additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
      renderLegacyChunks: true,
      polyfills: [
        'es.symbol',
        'es.promise',
        'es.promise.finally',
        'es/map',
        'es/set',
        'es.array.filter',
        'es.array.for-each',
        'es.array.flat-map',
        'es.object.define-properties',
        'es.object.define-property',
        'es.object.get-own-property-descriptor',
        'es.object.get-own-property-descriptors',
        'es.object.keys',
        'es.object.to-string',
        'web.dom-collections.for-each',
        'esnext.global-this',
        'esnext.string.match-all'
      ]
    }),
    vue(),
    vueJsx(),
    viteESLint(),
    Components({
      resolvers: [VantResolver()]
    })
  ],
  build: {
    target: 'es2015',
    rollupOptions: {
      plugins: [terser()]
    },
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      },
      ie8: true,
      output: {
        comments: true
      }
    }
    // reportCompressedSize: false
  },
  resolve: {
    alias: {
      '@': resolve('./src'),
      '@common': resolve('./src/common'),
      '@components': resolve('./src/components'),
      '@store': resolve('./src/store'),
      '@views': resolve('./src/views')
    }
  },
  server: {
    host: '0.0.0.0',
    port: 9005,
    strictPort: true,
    cors: true,
    https: false,
    proxy: {
      '/api-student': {
        target: proxyUrl,
        changeOrigin: true
      },
      '/api-teacher': {
        target: proxyUrl,
        changeOrigin: true
      },
      '/api-admin': {
        target: proxyUrl,
        changeOrigin: true
      },
      '/api-auth': {
        target: proxyUrl,
        changeOrigin: true
      }
    }
  }
});
