import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import topLevelAwait from 'vite-plugin-top-level-await'
import { resolve } from 'path'
import viteCompression from 'vite-plugin-compression';//Gzip
import { visualizer } from "rollup-plugin-visualizer";
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons' // 【svg-icons相关】
import VitePluginHtmlEnv from 'vite-plugin-html-env'
const time = new Date().getTime()
// @ts-ignore
import {getApiProxy} from './apiProxy'
const apiProxyVersion = 'local'

// https://vitejs.dev/config/
export default defineConfig({
  define: {
    'process.env': process.env
  },
  plugins: [vue(), viteCompression(), VitePluginHtmlEnv(), visualizer(), createSvgIconsPlugin({
    // 指定需要缓存的图标文件夹
    iconDirs: [resolve(process.cwd(), 'src/assets/svg/global'), resolve(process.cwd(), 'src/assets/svg/business')],
    // 指定symbolId格式
    symbolId: 'icon-[dir]-[name]',
  }), topLevelAwait({
    promiseExportName: '__tla',
    promiseImportName: i => `__tla_${i}`
  })],
  base: '/',
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
      '~@': resolve(__dirname, 'src'),
      '@views': resolve(__dirname, 'src/views'),
      '@components': resolve(__dirname, 'src/components')
    },
  },
  server: {
    port: 8090,
    // open: true,
    https: false,
    host: '0.0.0.0',
    strictPort: false,
    proxy: {
      '/mock-api': {
        target: 'https://www.fastmock.site/mock/002bd20e30dcef8f255675f80a50382c/mock',
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/mock-api/, '')
        }
      },
      '/api': {
        target: getApiProxy('api', apiProxyVersion),
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/api/, '')
        }
      },
      '/img-api': {
        target: getApiProxy('img-api'),
        changeOrigin: true
      },
      '/geoserver-api': {
        target: getApiProxy('geoserver-api'),
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/geoserver-api/, '')
        }
      },
      '/hlx-firstly': {
        target: 'http://74.10.30.38:30944/',
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/hlx-firstly/, '')
        }
      },
      '/hlx-second': {
        target: 'http://74.10.28.119:30944/',
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/hlx-second/, '')
        }
      },
      '/hlx-thirdly': {
        target: 'http://74.10.30.39:30944/',
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/hlx-thirdly/, '')
        }
      },
      '/EzServer6-api': {
        target: getApiProxy('EzServer6-api'),
        changeOrigin: true,
        rewrite: path => {
          return path
        }
      },
      '/rh-find-ship-api': {
        target: getApiProxy('rh-find-ship-api', apiProxyVersion),
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/rh-find-ship-api/, '')
        }
      },
      '/history-track-ws-api': {
        target: getApiProxy('history-track-ws-api', apiProxyVersion),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/history-track-ws-api/, '')
        }
      },
      '/rh-ws-api': {
        target: getApiProxy('rh-ws-api', apiProxyVersion), // fk
        // target: 'ws://74.10.28.38:8015/', // fk117
        // target: 'ws://74.10.28.28:6990/', // hlx
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/rh-ws-api/, '')
        }
      },
      '/ais-ws-api': {
        target: getApiProxy('ais-ws-api'),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/ais-ws-api/, '')
        }
      },
      '/dc-ws-api': {
        target: getApiProxy('dc-ws-api', apiProxyVersion),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/dc-ws-api/, '')
        }
      },
      '/kx-ws-api': {
        target: getApiProxy('kx-ws-api', apiProxyVersion),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/kx-ws-api/, '')
        }
      },
      '/bd-ws-api': {
        target: getApiProxy('bd-ws-api'),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/bd-ws-api/, '')
        }
      },
      '/xmb-ws-api': {
        target: getApiProxy('xmb-ws-api'),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/xmb-ws-api/, '')
        }
      },
      '/zyh-ws-api': {
        target: getApiProxy('zyh-ws-api'),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/zyh-ws-api/, '')
        }
      },
      '/hlx-ws-api': {
        target: getApiProxy('hlx-ws-api'),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/hlx-ws-api/, '')
        }
      },
      '/hlx-radar-ws-api': {
        target: getApiProxy('geoserver-api', apiProxyVersion),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/hlx-radar-ws-api/, '')
        }
      },
      '/warning-ws-api': {
        target: getApiProxy('warning-ws-api', apiProxyVersion),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/warning-ws-api/, '')
        }
      },
      '/info-center-ws-api': {
        target: getApiProxy('info-center-ws-api', apiProxyVersion),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/info-center-ws-api/, '')
        }
      },
      '/police-man-ws-api': {
        target: getApiProxy('police-man-ws-api'),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/police-man-ws-api/, '')
        }
      },
      '/police-boat-ws-api': {
        target: getApiProxy('police-boat-ws-api'),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/police-boat-ws-api/, '')
        }
      },
      '/pdt-ws-api': {
        target: getApiProxy('pdt-ws-api'),
        ws: true,
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/pdt-ws-api/, '')
        }
      },
      '/speed-analysis-api': {
        target: getApiProxy('speed-analysis-api'),
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/speed-analysis-api/, '')
        }
      },
      '/s3-api': {
        target: 'http://74.10.28.116:8090/',
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/s3-api/, 's3-api')
        }
      },
    }
  },
  build: {
    outDir: "seat",
    rollupOptions: {//分包优化
      output: {
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return time + id.toString().split('node_modules/')[1].split('/')[0].toString();
          } else {
            return time + id.toString();
          }
          // if(id.indexOf('node_modules') > -1) {
          //   return 'vendor'
          // }
        }
      }
    }
  },
  publicDir: 'src/out',
  optimizeDeps: {
    include: []
  }
})
