Files
Time/src/main/rendererTarget.ts
2026-09-12 13:49:44 +08:00

25 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 渲染进程入口解析 —— 与 Electron 解耦,方便单测。
*
* electron-vite 在 dev 模式下会把 dev server 地址写进 process.env.ELECTRON_RENDERER_URL
* (形如 http://localhost:5173无尾斜杠。多入口项目里各入口分别位于
* `<devUrl>/index.html`、`<devUrl>/floating-pomodoro/index.html` 等。
* 历史 bugdev 分支写死加载 dev server 根目录,导致任何窗口都渲染主界面。
*/
export type RendererTarget =
| { kind: 'url'; url: string }
| { kind: 'file'; file: string; query: Record<string, string> | undefined };
export function resolveRendererTarget(
file: string,
query?: Record<string, string>,
devServerUrl: string | undefined = process.env.ELECTRON_RENDERER_URL
): RendererTarget {
if (devServerUrl) {
const base = devServerUrl.replace(/\/+$/, '');
const search = query ? '?' + new URLSearchParams(query).toString() : '';
return { kind: 'url', url: `${base}/${file}${search}` };
}
return { kind: 'file', file, query };
}