import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import styleImport, { VantResolve } from 'vite-plugin-style-import';
// import basicSsl from '@vitejs/plugin-basic-ssl'
import path from 'path';
import legacy from '@vitejs/plugin-legacy'
//移动端适配插件
import postCssPxToRem from "postcss-pxtorem"
import { minify } from 'terser';
// https://vitejs.dev/config/
export default defineConfig({
  build: {
    target: 'esnext',
    minify: 'terser',
    rollupOptions: {
      plugins: [
        {
          name: 'terser',
          async transform(code) {
            const result = await minify(code);
            if (result.error) {
              throw result.error;
            }
            return result.code;
          },
        },
      ],
    },
  },
  //移动端适配  单位转换基准
  css: {
    postcss: {
      plugins: [
        postCssPxToRem({
          rootValue: 72, // 1rem的大小
          propList: ['*'], // 需要转换的属性，这里选择全部都进行转换
        })
      ]
    }
  },
  plugins: [
    vue(),
    // styleImport({
    //   resolves: [VantResolve()],
    // }),
    // basicSsl(),
    legacy({
      targets: ['defaults', 'chrome > 52']
    })
  ],
  base: "./", //打包后静态资源访问路径前缀
  define: {
    'process.env': {
      // VUE_APP_BASE_API: 'http://8.130.13.94:8080'
      VUE_APP_BASE_API: '/api_app'
    }
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),//设置别名
      'src': path.resolve(__dirname, './src'),//设置别名
    }
  },
  // 当前server配置只支持开发环境，如果打包后请配置后台路径或nginx
  server: {
    port: 8093,//启动端口
    proxy: {
      // 选项写法
      '/api_app': {
        // target: 'http://192.168.31.121:8080',
        // target: 'https://192.168.31.88',
        target: 'http://192.168.31.89:8080',
        // target: 'http://8.130.13.94:8080',//阿里云访问地址
        // target: 'http://127.0.0.1:8080',
        changeOrigin: true,
        secure: false,
        rewrite: path => path.replace(/^\/api_app/, '')
      },
    },
    // https:true
  },
  mode:{

  }
})
