import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from "node:url";
import copy from 'rollup-plugin-copy'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { VantResolver } from '@vant/auto-import-resolver';

// https://vite.dev/config/
export default defineConfig({
    base: './',
    plugins: [
        vue(),
        AutoImport({
            resolvers: [ElementPlusResolver(),VantResolver()],
        }),
        Components({
            resolvers: [ElementPlusResolver(),VantResolver()],
        }),
    ],
    resolve: {
        alias: {
            "@": fileURLToPath(new URL("./src", import.meta.url)),
        },
    },
    pluginOptions: {
        'style-resources-loader': {
            preProcessor: 'scss',
            patterns: []
        }
    },
    define: {
        'process.env': {}
    },
    // 完全禁用默认的 public 目录处理
    publicDir: false,
    build: {
        // 清空输出目录时保留自定义拷贝的文件
        emptyOutDir: true,

        rollupOptions: {
            plugins: [
                copy({
                    targets: [
                        {
                            src: 'public/**/*',  // 复制整个 public 目录
                            dest: 'dist/public'  // 输出到 dist/public
                        }
                    ],
                    hook: 'writeBundle',     // 在构建完成后执行
                    copyOnce: true          // 仅拷贝一次
                })
            ]
        }
    },
})
