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

// 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: 6969,
    // open: true,
    https: false,
    host: '0.0.0.0',
    strictPort: false,
    proxy: {
      '/BaseMap1-api/': {
        // target: 'http://59.255.48.160:81/',
        target: 'http://127.0.0.1:3999/',
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/BaseMap1-api/, '')
        }
      },
      '/BaseMap2-api/': {
        // target: 'http://59.212.37.22/',
        target: 'http://127.0.0.1:3998/',
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/BaseMap2-api/, '')
        }
      },
      '/BaseMap3-api/': {
        // target: 'http://59.212.146.170/',
        target: 'http://127.0.0.1:3997/',
        changeOrigin: true,
        rewrite: path => {
          return path.replace(/^\/BaseMap3-api/, '')
        }
      },
    }
  },
  build: {
    outDir: "gis-web",
    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: []
  }
})
