import {defineConfig, loadEnv} from "vite";
import path from "node:path";
import uni from "@dcloudio/vite-plugin-uni";
import AutoImport from 'unplugin-auto-import/vite'
import ViteRestart from 'vite-plugin-restart'
import UnoCSS from 'unocss/vite'
import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'

// https://vitejs.dev/config/
export default ({command, mode}) => {
    const {UNI_PLATFORM} = process.env
    console.log('UNI_PLATFORM -> ', UNI_PLATFORM) // 得到 mp-weixin, h5, app 等

    const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
    const {
        VITE_APP_PORT,
        VITE_SERVER_BASEURL,
        VITE_DELETE_CONSOLE,
        VITE_SHOW_SOURCEMAP,
        VITE_APP_PROXY,
        VITE_APP_PROXY_PREFIX
    } = env
    console.log('环境变量 env -> ', env)


    return defineConfig({
        envDir: './env', // 自定义env目录
        css: {
            preprocessorOptions: {
                scss: {
                    api: 'modern-compiler', // or 'modern'
                },
            },
        },
        plugins: [
            UniLayouts(),
            uni(),
            AutoImport({
                imports: ['vue', 'uni-app', 'vue-i18n', 'pinia'],
                dts: 'src/types/auto-import.d.ts',
                dirs: ['src/hooks'], // 自动导入 hooks
                eslintrc: {enabled: true},
                vueTemplate: true, // default false
            }),
            UnoCSS(),

            ViteRestart({
                // 通过这个插件，在修改vite.config.js文件则不需要重新运行也生效配置
                restart: ['vite.config.js'],
            }),

        ],
        define: {
            __UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM),
            __VITE_APP_PROXY__: JSON.stringify(VITE_APP_PROXY),
        },
        optimizeDeps: {
            exclude: process.env.UNI_PLATFORM === 'h5' && process.env.NODE_ENV === 'development' ? ['wot-design-uni'] : []
        },
        resolve: {
            alias: {
                '@': path.join(process.cwd(), './src'),
                '@img': path.join(process.cwd(), './src/static/images'),
                '@img-login': path.join(process.cwd(), './src/pages-login/static/images'),
                '@img-user': path.join(process.cwd(), './src/pages-user/static/images'),
            },
        },
        server: {
            host: '0.0.0.0',
            hmr: {
                overlay: false,
            },
            open: true,
            port: Number.parseInt(VITE_APP_PORT, 10),
            // 仅 H5 端生效，其他端不生效（其他端走build，不走devServer)
            proxy: JSON.parse(VITE_APP_PROXY)
                ? {
                    [VITE_APP_PROXY_PREFIX]: {
                        target: VITE_SERVER_BASEURL,
                        changeOrigin: true,
                        rewrite: (path) => path.replace(new RegExp(`^${VITE_APP_PROXY_PREFIX}`), ''),
                    },
                }
                : undefined,
        },
        build: {
            // 方便非h5端调试
            sourcemap: false, // 默认是false
            target: 'es6',
            minify: 'terser',
            terserOptions: {
                compress: {
                    drop_console: VITE_DELETE_CONSOLE === 'true',
                    drop_debugger: true,
                },
            },
        },
    })
}
