Fix PUBLIC_URL typing

This commit is contained in:
Joseph Garrone 2024-02-11 10:36:50 +01:00
parent 26985f8d81
commit b3c242595e

View File

@ -1,4 +1,5 @@
import { nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir } from "keycloakify/bin/constants";
import { assert } from "tsafe/assert";
/**
* This is an equivalent of process.env.PUBLIC_URL thay you can use in Webpack projects.
@ -7,7 +8,14 @@ import { nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir } from "keycloaki
export const PUBLIC_URL = (() => {
const kcContext = (window as any)[nameOfTheGlobal];
return kcContext === undefined || process.env.NODE_ENV === "development"
? process.env.PUBLIC_URL
: `${kcContext.url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}`;
if (kcContext === undefined || process.env.NODE_ENV === "development") {
assert(
process.env.PUBLIC_URL !== undefined,
`If you use keycloakify/PUBLIC_URL you should be in Webpack and thus process.env.PUBLIC_URL should be defined`
);
return process.env.PUBLIC_URL;
}
return `${kcContext.url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}`;
})();