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

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());

  return {
    base: env.VITE_PUBLIC || './', //使用环境变量设置资源路径，默认为相对路径
  
    server: {
      host: true,
      port: 8080, //vite项目启动时自定义端口

      open: true,
      proxy: {
        // 正则表达式写法
        '^/api': {
          target: 'http://192.168.3.13:3000/api', // 后端服务实际地址
          changeOrigin: true, //开启代理
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },

    plugins: [
      vue(),
    ],

    resolve: {
      //别名
      alias: {
        '@': resolve(__dirname, './src'),
        components: resolve(__dirname, './src/components'),
        script: resolve(__dirname, './src/script'),
        utils: resolve(__dirname, './src/utils'),
        stores: resolve(__dirname, './src/stores')
      }
    },
    build: {
      assetsDir: 'static', //打包后的公共文件夹名
      target: 'es2015',
      cssTarget: ['chrome61'],
      chunkSizeWarningLimit: 5000
    }
  }
})
