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';
import { createHtmlPlugin } from 'vite-plugin-html';
const getViteEnv = (mode, target) => {
  console.log(loadEnv(mode, process.cwd())[target], '>>>>>>');
  return loadEnv(mode, process.cwd())[target];
};
// 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(),
      createHtmlPlugin({
        inject: {
          data: {
            title: getViteEnv(mode, 'VITE_APP_TITLE')
          }
        }
      }),
      //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: 'http://192.168.1.37:9000',
          // target: 'http://192.168.1.253:8000',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
    css: {
      postcss: {
        plugins: [
          postcssPxtoRem({
            rootValue: 128, // 按照自己的设计稿修改 1920/10
            unitPrecision: 5, // 保留到5位小数
            selectorBlackList: ['ignore', 'tab-bar', 'tab-bar-item', 'gasExperimentResult'], // 忽略转换正则匹配项
            propList: ['*'],
            replace: true,
            mediaQuery: false,
            minPixelValue: 0
          })
        ]
      }
    }
  };
});