import { defineConfig, UserConfig } from 'vite'
import react from '@vitejs/plugin-react'

import dts from 'vite-plugin-dts';

import { libInjectCss } from 'vite-plugin-lib-inject-css';

// https://vite.dev/config/
export default defineConfig(({ mode }): UserConfig => {

  const isDev = mode == "development";

  return {
    build: {
      cssCodeSplit: true,
      sourcemap: isDev,
      // target: "modules",
      lib: {
        entry: "./src/index.tsx",
        name: 'UI',
        fileName: 'index',
      },
      cssTarget: 'chrome61',
      minify: false,
      rollupOptions: {
        // 确保外部化处理那些你不想打包进库的依赖
        external: ['react', 'react-dom', "react/jsx-runtime"],
        output: {
          // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
          globals: {
            React: 'react',
            "react-dom": 'react-dom',
          },
          // preserveModules: true,
          // preserveModulesRoot:'src',
          // entryFileNames: "[name].js",
          // exports: "named",
        },
      },
    },
    plugins: [
      react(),
      libInjectCss(),
      dts({ tsconfigPath: './tsconfig.app.json' })
    ],
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
        },
      },
      modules: {
        localsConvention: 'camelCase'
      }
    }
  }
})
