mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-14 04:46:07 -08:00
25 lines
1.0 KiB
TypeScript
25 lines
1.0 KiB
TypeScript
/**
|
|
* Resolve a bundled public asset against Vite's base URL.
|
|
*
|
|
* The in-player stats window is loaded with `loadFile`, so the document lives on
|
|
* `file://`. A root-absolute path like `/favicon.png` resolves to the filesystem
|
|
* root there and 404s, while the HTTP-served web app resolves it fine. Vite
|
|
* rewrites asset refs in `index.html` but not string literals in JSX, so build
|
|
* the URL from the configured base instead of hardcoding a leading slash.
|
|
*/
|
|
export function resolveAssetUrl(path: string, base: string): string {
|
|
const normalizedBase = base.endsWith('/') ? base : `${base}/`;
|
|
return `${normalizedBase}${path.replace(/^\/+/, '')}`;
|
|
}
|
|
|
|
function currentBase(): string {
|
|
// Vite injects BASE_URL at build time ('./' per vite.config.ts) and serves '/'
|
|
// in dev. Outside a Vite bundle (tests) there is no env, so fall back to './'.
|
|
const env = (import.meta as { env?: Record<string, string | undefined> }).env;
|
|
return env?.BASE_URL || './';
|
|
}
|
|
|
|
export function assetUrl(path: string): string {
|
|
return resolveAssetUrl(path, currentBase());
|
|
}
|