From 217439d67337673b00be2b06485224900f57d539 Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Fri, 9 Feb 2024 17:52:26 +0100 Subject: [PATCH] Export polyfill of process.env.PUBLIC_URL --- src/PUBLIC_URL.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/PUBLIC_URL.ts diff --git a/src/PUBLIC_URL.ts b/src/PUBLIC_URL.ts new file mode 100644 index 00000000..cfd6d701 --- /dev/null +++ b/src/PUBLIC_URL.ts @@ -0,0 +1,13 @@ +import { nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir } from "keycloakify/bin/constants"; + +/** + * This is an equivalent of process.env.PUBLIC_URL thay you can use in Webpack projects. + * This works both in your main app and in your Keycloak theme. + */ +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}`; +})();