import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import alias from "@rollup/plugin-alias";
import createVitePlugins from './vite/plugins'
import resolveExternalsPlugin from 'vite-plugin-resolve-externals'
// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
    const env = loadEnv(mode, process.cwd())
    const { VITE_APP_ENV } = env
    plugins: [alias(),
        resolveExternalsPlugin({
            AMap: 'AMap'
        }),
    ]
    const config = loadEnv(mode, './')
    return {
        // 部署生产环境和开发环境下的URL。
        // 默认情况下，vite 会假设你的应用是被部署在一个域名的根路径上
        base: VITE_APP_ENV === 'production' ? '/' : '/',
        plugins: createVitePlugins(env, command === 'build'),
        resolve: {
            // https://cn.vitejs.dev/config/#resolve-alias
            alias: {
                // 设置路径
                '~': path.resolve(__dirname, './'),
                // 设置别名
                '@': path.resolve(__dirname, './src'),
            },
            // https://cn.vitejs.dev/config/#resolve-extensions
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
        },
        build: {
            rollupOptions: {
                external: [
                    "Vue3SeamlessScroll"
                ]
            },

            //<----------去除console------
            minify: 'terser',
            terserOptions: {
                compress: {
                    drop_console: true,
                    drop_debugger: true
                }
            },
            //-----------去除console----->
        },
        // vite 相关配置
        server: {
            hmr: true, //热更新
            port: 81, //端口号
            host: true,
            open: true, // 自动打开浏览器
            proxy: {
                '/dev-api': {
                    target: '',
                    ws: true,
                    changeOrigin: true,
                    pathRewrite: {
                        '^/api': ''
                    },
                    //rewrite: (p) => p.replace(/^\/dev-api/, '')
                },
            },
        },
        css: {
            postcss: {
                plugins: [{
                    postcssPlugin: 'internal:charset-removal',
                    AtRule: {
                        charset: (atRule) => {
                            if (atRule.name === 'charset') {
                                atRule.remove();
                            }
                        }
                    }
                }]
            }
        },
        // configureWebpack() {
        //     return {
        //         extensions: ['.AMap']
        //     }
        // },
    }
})