Commit 8068b030 authored by ThinhNC's avatar ThinhNC

fix(router): use transform plugin on original ZMPRouter to fix local zmp start and vercel

parent 2ec6f4fd
Pipeline #53710 canceled with stage
...@@ -4,9 +4,9 @@ import { ...@@ -4,9 +4,9 @@ import {
App, App,
Route, Route,
SnackbarProvider, SnackbarProvider,
ZMPRouter,
useNavigate, useNavigate,
} from "zmp-ui"; } from "zmp-ui";
import { ZMPRouter } from "@/components/router/ZMPRouter";
import { QueryClientProvider } from "@tanstack/react-query"; import { QueryClientProvider } from "@tanstack/react-query";
import { queryClient } from "@/lib/query-client"; import { queryClient } from "@/lib/query-client";
import { useAuthStore, GUEST_STORAGE_KEY } from "@/stores/auth-store"; import { useAuthStore, GUEST_STORAGE_KEY } from "@/stores/auth-store";
......
import { defineConfig } from "vite"; import { defineConfig, Plugin } from "vite";
import zaloMiniApp from "zmp-vite-plugin"; import zaloMiniApp from "zmp-vite-plugin";
import react from "@vitejs/plugin-react"; import react from "@vitejs/plugin-react";
import path from "path"; import path from "path";
/**
* Patch ZMPRouter to only force `/zapps/<APP_ID>` basepath when inside Zalo (pathname starts with /zapps/).
* When running on Vercel or standalone web, basepath remains "" so standard routes (/, /login) work properly.
* In local `zmp start`, appEnv === "DEVELOPMENT" handles the basepath natively with zero context mismatch.
*/
function patchZmpRouterPlugin(): Plugin {
return {
name: "patch-zmp-router-for-web",
transform(code, id) {
if (id.includes("ZMPRouter")) {
return {
code: code.replace(
'process.env.NODE_ENV === "production"',
'(typeof window !== "undefined" && window.location.pathname.startsWith("/zapps/") && process.env.NODE_ENV === "production")'
),
map: null,
};
}
return null;
},
};
}
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default () => { export default () => {
return defineConfig({ return defineConfig({
base: "", base: "",
plugins: [zaloMiniApp(), react()], plugins: [patchZmpRouterPlugin(), zaloMiniApp(), react()],
build: { build: {
assetsInlineLimit: 0, assetsInlineLimit: 0,
}, },
resolve: { resolve: {
alias: [ alias: {
{ find: "@", replacement: path.resolve(__dirname, "./src") }, "@": path.resolve(__dirname, "./src"),
{ },
find: /.*\/components\/router\/ZMPRouter(\.js)?$/,
replacement: path.resolve(__dirname, "./src/components/router/ZMPRouter.tsx"),
},
],
}, },
}); });
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment