
import path from 'path'
import fs from 'fs'
import react from '@vitejs/plugin-react'
import commonjs from 'vite-plugin-commonjs'
import { defineConfig } from 'vite'
import ViteRequireContext from '@originjs/vite-plugin-require-context'

// https://vitejs.dev/config/
export default defineConfig({
  
  plugins: [
    ViteRequireContext(),
    commonjs(),
    react({
      babel: {
        plugins: [
          // js中写jsx的语法支持
          '@babel/plugin-transform-react-jsx',
          // 使用commonjs的语法导入导出支持
          '@babel/plugin-transform-modules-commonjs',
        ],
      },
    }),
  ],
  esbuild: {
    loader: 'jsx',
    include: /src\/.*\.jsx?$/,
    exclude: []
  },
  optimizeDeps: {
    esbuildOptions: {
      loader: { '.js': 'jsx' },
      plugins: [
        {
          name: 'load-js-files-as-jsx',
          setup (build) {
            build.onLoad({ filter: /src\/.*\.js$/ }, async (args) => ({
              loader: 'jsx',
              contents: await fs.readFile(args.path, () => {})
            }))
          }
        }
      ]
    }
  },
  extensions: ['.js', '.jsx', '.json', '.png'],
  resolve: {
    alias: {
      // https://github.com/alloc/vite-react-jsx/issues/4
      'react/jsx-runtime': 'react/cjs/react-jsx-runtime.production.min.js',
      'components': path.resolve(__dirname, './src/components'),
      'pages': path.resolve(__dirname, './src/pages'),
      'containers': path.resolve(__dirname, './src/containers'),
      'store': path.resolve(__dirname, './src/store'),
      'assets': path.resolve(__dirname, './src/assets'),
      'tools': path.resolve(__dirname, './src/tools'),
      '~antd': path.resolve(__dirname, './node_modules/antd'),
      'routers': path.resolve(__dirname, './src/routers'),
    },
  },
  css:{
    // 是对css模块化的默认行为进行覆盖
    modules:{
      // 是否开启模块化
      scopeBehaviour: 'local',
      // 修改生成的配置对象的key的展示形式(驼峰还是中划线形式)
      localsConvention: "camelCase",
    }
  },
  server: {
    port: 4003, // 默认启动端口
    // hmr: {
    //   // 禁用开发服务器错误的屏蔽
    //   overlay: false,
    // },
    hmr: false,
    proxy: {
      "/wsdl": {
        target: "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/wsdl/, ""),
      },
      "/bdhealth": {
        target: "https://xnetkf-hlwyy.hydproject.com",
        secure: false,
        ws: true,
        changeOrigin: true,
      },
    },
  }
})
