import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';

import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import postcssPxtoRem from 'postcss-pxtorem';
import { visualizer } from 'rollup-plugin-visualizer';
import { lazyImport, VxeResolver } from 'vite-plugin-lazy-import';

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd());
  console.log(env);

  return {
    base: './',
    publicDir: 'public',
    build: {
      outDir: 'dist',
      sourcemap: false,
      minify: 'terser',
      terserOptions: {
        compress: {
          //drop_console: true,
          drop_debugger: true
        }
      }
    },
    plugins: [
      vue(),
      //vxe按需加载
      lazyImport({
        resolvers: [
          VxeResolver({
            libraryName: 'vxe-table'
          })
        ]
      }),
      //打包视图
      visualizer({
        open: true,
        gzipSize: true,
        brotliSize: true,
        filename: 'stats.html'
      }),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/plugin/svg-icon/svgs')],
        // 指定symbolId格式
        symbolId: '[name]'
      }),
      //按需加载
      Components({
        resolvers: [
          AntDesignVueResolver({
            importStyle: false // css in js
          })
        ]
      })
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      }
    },
    server: {
      host: '0.0.0.0',
      // port:'8080',
      proxy: {
        '/api': {
          target: env.VITE_SEVER_URL,
          // target: 'http://192.168.100.15:8000',
          // target: 'http://192.168.1.151:8000',
          // target: 'http://127.0.0.1:8000',
          // target: 'http://192.168.1.253:8000',
          // target: 'http://192.168.1.253:8000',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
    css: {
      postcss: {
        plugins: [
          postcssPxtoRem({
            rootValue: 64.7, // 按照自己的设计稿修改 1920/10
            unitPrecision: 5, // 保留到5位小数
            selectorBlackList: ['ignore', 'tab-bar', 'tab-bar-item', 'gasExperimentResult'], // 忽略转换正则匹配项
            propList: ['*'],
            replace: true,
            mediaQuery: false,
            minPixelValue: 0
          })
        ]
      }
    }
  };
});
