import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import tailwindcss from '@tailwindcss/vite';
import basicSsl from '@vitejs/plugin-basic-ssl';

const plugins = [basicSsl()];
// const plugins = [];
plugins.push(tailwindcss());
let proxy = {};
if (true) {
  proxy = {
    '/api': {
      target: 'https://kevisual.silkyai.cn',
      changeOrigin: true,
      ws: true,
      cookieDomainRewrite: 'localhost',
      rewrite: (path) => path.replace(/^\/api/, '/api'),
    },
    '/api/router': {
      target: 'wss://kevisual.silkyai.cn',
      changeOrigin: true,
      ws: true,
      rewriteWsOrigin: true,
      rewrite: (path) => path.replace(/^\/api/, '/api'),
    },
    '/user/login': {
      target: 'https://kevisual.silkyai.cn',
      changeOrigin: true,
      cookieDomainRewrite: 'localhost',
      rewrite: (path) => path.replace(/^\/user/, '/user'),
    },
  };
}
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), ...plugins],

  css: {
    postcss: {},
  },
  resolve: {
    alias: {
      '@kevisual/codemirror': path.resolve(__dirname, './src'),
    },
  },
  define: {
    DEV_SERVER: JSON.stringify(process.env.NODE_ENV === 'development'),
    BASE_NAME: JSON.stringify('/root/codemirror/'),
  },
  base: './',
  // base: isDev ? '/' : '/root/codemirror/',
  build: {
    rollupOptions: {
      // external: ['react', 'react-dom'],
    },
  },
  server: {
    port: 6022,
    host: '0.0.0.0',
    proxy: {
      '/system/lib': {
        target: 'https://kevisual.xiongxiao.me',
        changeOrigin: true,
      },
      '/api': {
        target: 'http://localhost:4005',
        changeOrigin: true,
        ws: true,
        cookieDomainRewrite: 'localhost',
        rewrite: (path) => path.replace(/^\/api/, '/api'),
      },
      '/api/router': {
        target: 'ws://localhost:4005',
        changeOrigin: true,
        ws: true,
        rewriteWsOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '/api'),
      },
      ...proxy,
    },
  },
});
