import { loadEnv } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
import { resolve } from "path";

const resolvePath = (path) => resolve(__dirname, path);
const rootWorkPath = process.cwd(); // root路径
// https://vitejs.dev/config/
export default ({mode}) => {
    let env = loadEnv(mode, rootWorkPath);
    return {
        plugins: [uni()],

        server: {
            https: false,
            port: 10086,
            proxy: {
                ["/appApi"]: {
                    target: env.VITE_BASE_URL,
                    ws: false,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/appApi/, ''),
                },
            },
        },
        css: {
            /* CSS 预处理器 */
            preprocessorOptions: {
                scss: {
                    additionalData: '@import "@/style/main.scss";'
                }
            }
        },
        // 打包相关
        build: {
            sourcemap: false, //构建后是否生成 source map 文件
            cssCodeSplit: true,// 启用/禁用 CSS 代码拆分
            assetsInlineLimit: 4096, // 图片转base64编码的阈值
            rollupOptions: {
                output: {
                    // 拆分包
                    // manualChunks(id){
                    //   if (id.includes("node_modules")) {
                    //     // 让每个插件都打包成独立的文件
                    //     return id.toString().split("node_modules/")[1].split("/")[0].toString();
                    //   }
                    // }
                }
            },
            // 去除控制台打印
            minify: 'terser',
            terserOptions: {
                compress: {
                    drop_console: false,
                    drop_debugger: false
                }
            }
        }
    }
}
