Base url that works everywhere in mocks

This commit is contained in:
Joseph Garrone
2024-02-10 19:51:24 +01:00
parent db0dc96cc7
commit bda76200d7
6 changed files with 49 additions and 20 deletions

41
src/lib/BASE_URL.ts Normal file
View File

@ -0,0 +1,41 @@
import { assert } from "tsafe/assert";
/**
* WARNING: Internal use only!!
* This is just a way to know what's the base url that works
* both in webpack and vite.
* THIS DOES NOT WORK IN KEYCLOAK! It's only for resolving mock assets.
*/
export const BASE_URL = (() => {
vite: {
let BASE_URL: string;
try {
// @ts-expect-error
BASE_URL = import.meta.env.BASE_URL;
assert(typeof BASE_URL === "string");
} catch {
break vite;
}
return BASE_URL;
}
webpack: {
let BASE_URL: string;
try {
// @ts-expect-error
BASE_URL = process.env.PUBLIC_URL;
assert(typeof BASE_URL === "string");
} catch {
break webpack;
}
return BASE_URL === "" ? "/" : `${BASE_URL}/`;
}
return "/";
})();