26 lines
978 B
TypeScript
26 lines
978 B
TypeScript
/// <reference types="vite/client" />
|
||
|
||
function normalizeBase(base?: string): string {
|
||
if (!base || base === '/') return '';
|
||
return base.replace(/\/$/, '');
|
||
}
|
||
|
||
/** React Router basename(来自 Vite `base`) */
|
||
export function getRouterBasename(baseUrl?: string): string | undefined {
|
||
const base = normalizeBase(baseUrl ?? import.meta.env?.BASE_URL);
|
||
return base || undefined;
|
||
}
|
||
|
||
/** 拼接带 Vite base 前缀的应用内路径(用于 window.location 跳转) */
|
||
export function toAppPath(path: string, baseUrl?: string): string {
|
||
const base = normalizeBase(baseUrl ?? import.meta.env?.BASE_URL);
|
||
const normalized = path.startsWith('/') ? path : `/${path}`;
|
||
return `${base}${normalized}`;
|
||
}
|
||
|
||
export function isOnAppPath(path: string, baseUrl?: string): boolean {
|
||
if (typeof window === 'undefined') return false;
|
||
const target = toAppPath(path, baseUrl);
|
||
return window.location.pathname === target || window.location.pathname.startsWith(`${target}/`);
|
||
}
|