From f1475e5cdfca3b082894d910b214fbccac78482f Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Fri, 14 Jun 2024 22:24:51 +0200 Subject: [PATCH] Settle on calling the global 'kcContext' and reduce levels of indirections --- src/PUBLIC_URL.ts | 7 +-- src/bin/keycloakify/buildJars/buildJar.ts | 4 +- .../keycloakify/generateFtl/generateFtl.ts | 10 ++- ...t.ftl => kcContextDeclarationTemplate.ftl} | 62 +++++++++---------- .../replacers/replaceImportsInJsCode/vite.ts | 9 +-- .../replaceImportsInJsCode/webpack.ts | 9 +-- src/bin/shared/constants.ts | 1 - src/vite-plugin/vite-plugin.ts | 5 +- test/bin/replacers.spec.ts | 23 +++---- 9 files changed, 55 insertions(+), 75 deletions(-) rename src/bin/keycloakify/generateFtl/{ftl_object_to_js_code_declaring_an_object.ftl => kcContextDeclarationTemplate.ftl} (92%) diff --git a/src/PUBLIC_URL.ts b/src/PUBLIC_URL.ts index c21f1f05..5de5a1c8 100644 --- a/src/PUBLIC_URL.ts +++ b/src/PUBLIC_URL.ts @@ -1,7 +1,4 @@ -import { - nameOfTheGlobal, - basenameOfTheKeycloakifyResourcesDir -} from "keycloakify/bin/shared/constants"; +import { basenameOfTheKeycloakifyResourcesDir } from "keycloakify/bin/shared/constants"; import { assert } from "tsafe/assert"; /** @@ -9,7 +6,7 @@ import { assert } from "tsafe/assert"; * This works both in your main app and in your Keycloak theme. */ export const PUBLIC_URL = (() => { - const kcContext = (window as any)[nameOfTheGlobal]; + const kcContext = (window as any).kcContext; if (kcContext === undefined || process.env.NODE_ENV === "development") { assert( diff --git a/src/bin/keycloakify/buildJars/buildJar.ts b/src/bin/keycloakify/buildJars/buildJar.ts index e86cdff0..eb57adea 100644 --- a/src/bin/keycloakify/buildJars/buildJar.ts +++ b/src/bin/keycloakify/buildJars/buildJar.ts @@ -168,8 +168,8 @@ export async function buildJar(params: { })(); const modifiedFtlFileContent = ftlFileContent.replace( - `out.pageId = "\${pageId}";`, - `out.pageId = "${pageId}"; out.realPageId = "${realPageId}";` + `kcContext.pageId = "\${pageId}";`, + `kcContext.pageId = "${pageId}"; kcContext.realPageId = "${realPageId}";` ); assert(modifiedFtlFileContent !== ftlFileContent); diff --git a/src/bin/keycloakify/generateFtl/generateFtl.ts b/src/bin/keycloakify/generateFtl/generateFtl.ts index 56475d66..582aeeae 100644 --- a/src/bin/keycloakify/generateFtl/generateFtl.ts +++ b/src/bin/keycloakify/generateFtl/generateFtl.ts @@ -8,7 +8,6 @@ import type { BuildContext } from "../../shared/buildContext"; import { assert } from "tsafe/assert"; import { type ThemeType, - nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir, resources_common, nameOfTheLocalizationRealmOverridesUserProfileProperty @@ -116,7 +115,7 @@ export function generateFtlFilesCodeFactory(params: { } //FTL is no valid html, we can't insert with cheerio, we put placeholder for injecting later. - const ftlObjectToJsCodeDeclaringAnObject = fs + const kcContextDeclarationTemplateFtl = fs .readFileSync( pathJoin( getThisCodebaseRootDirPath(), @@ -124,11 +123,10 @@ export function generateFtlFilesCodeFactory(params: { "bin", "keycloakify", "generateFtl", - "ftl_object_to_js_code_declaring_an_object.ftl" + "kcContextDeclarationTemplate.ftl" ) ) .toString("utf8") - .match(/^` + `` ); // Remove part of the document marked as ignored. @@ -189,7 +187,7 @@ export function generateFtlFilesCodeFactory(params: { Object.entries({ [ftlObjectToJsCodeDeclaringAnObjectPlaceholder]: - ftlObjectToJsCodeDeclaringAnObject, + kcContextDeclarationTemplateFtl, PAGE_ID_xIgLsPgGId9D8e: pageId }).map( ([searchValue, replaceValue]) => diff --git a/src/bin/keycloakify/generateFtl/ftl_object_to_js_code_declaring_an_object.ftl b/src/bin/keycloakify/generateFtl/kcContextDeclarationTemplate.ftl similarity index 92% rename from src/bin/keycloakify/generateFtl/ftl_object_to_js_code_declaring_an_object.ftl rename to src/bin/keycloakify/generateFtl/kcContextDeclarationTemplate.ftl index 4bcff97a..c2cad3ff 100644 --- a/src/bin/keycloakify/generateFtl/ftl_object_to_js_code_declaring_an_object.ftl +++ b/src/bin/keycloakify/generateFtl/kcContextDeclarationTemplate.ftl @@ -1,10 +1,8 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/src/bin/keycloakify/replacers/replaceImportsInJsCode/vite.ts b/src/bin/keycloakify/replacers/replaceImportsInJsCode/vite.ts index 9fa0dc1f..e71b677e 100644 --- a/src/bin/keycloakify/replacers/replaceImportsInJsCode/vite.ts +++ b/src/bin/keycloakify/replacers/replaceImportsInJsCode/vite.ts @@ -1,7 +1,4 @@ -import { - nameOfTheGlobal, - basenameOfTheKeycloakifyResourcesDir -} from "../../../shared/constants"; +import { basenameOfTheKeycloakifyResourcesDir } from "../../../shared/constants"; import { assert } from "tsafe/assert"; import type { BuildContext } from "../../../shared/buildContext"; import * as nodePath from "path"; @@ -88,13 +85,13 @@ export function replaceImportsInJsCode_vite(params: { fixedJsCode = replaceAll( fixedJsCode, `"${relativePathOfAssetFile}"`, - `(window.${nameOfTheGlobal}.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/${relativePathOfAssetFile}")` + `(window.kcContext.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/${relativePathOfAssetFile}")` ); fixedJsCode = replaceAll( fixedJsCode, `"${buildContext.urlPathname ?? "/"}${relativePathOfAssetFile}"`, - `(window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/${relativePathOfAssetFile}")` + `(window.kcContext.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/${relativePathOfAssetFile}")` ); }); } diff --git a/src/bin/keycloakify/replacers/replaceImportsInJsCode/webpack.ts b/src/bin/keycloakify/replacers/replaceImportsInJsCode/webpack.ts index 3f392062..31848ed9 100644 --- a/src/bin/keycloakify/replacers/replaceImportsInJsCode/webpack.ts +++ b/src/bin/keycloakify/replacers/replaceImportsInJsCode/webpack.ts @@ -1,7 +1,4 @@ -import { - nameOfTheGlobal, - basenameOfTheKeycloakifyResourcesDir -} from "../../../shared/constants"; +import { basenameOfTheKeycloakifyResourcesDir } from "../../../shared/constants"; import { assert } from "tsafe/assert"; import type { BuildContext } from "../../../shared/buildContext"; import * as nodePath from "path"; @@ -86,7 +83,7 @@ export function replaceImportsInJsCode_webpack(params: { var pd = Object.getOwnPropertyDescriptor(${n}, "p"); if( pd === undefined || pd.configurable ){ Object.defineProperty(${n}, "p", { - get: function() { return window.${nameOfTheGlobal}.url.resourcesPath; }, + get: function() { return window.kcContext.url.resourcesPath; }, set: function() {} }); } @@ -107,7 +104,7 @@ export function replaceImportsInJsCode_webpack(params: { `[a-zA-Z]+\\.[a-zA-Z]+\\+"${staticDir.replace(/\//g, "\\/")}`, "g" ), - `window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/${staticDir}` + `window.kcContext.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/${staticDir}` ); return { fixedJsCode }; diff --git a/src/bin/shared/constants.ts b/src/bin/shared/constants.ts index 64f03217..ca6d4b5d 100644 --- a/src/bin/shared/constants.ts +++ b/src/bin/shared/constants.ts @@ -1,4 +1,3 @@ -export const nameOfTheGlobal = "kcContext"; export const nameOfTheLocalizationRealmOverridesUserProfileProperty = "__localizationRealmOverridesUserProfile"; export const keycloak_resources = "keycloak-resources"; diff --git a/src/vite-plugin/vite-plugin.ts b/src/vite-plugin/vite-plugin.ts index 0c2941c1..dc78cef5 100644 --- a/src/vite-plugin/vite-plugin.ts +++ b/src/vite-plugin/vite-plugin.ts @@ -1,7 +1,6 @@ import { join as pathJoin, relative as pathRelative, sep as pathSep } from "path"; import type { Plugin } from "vite"; import { - nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir, keycloak_resources, vitePluginSubScriptEnvNames @@ -170,9 +169,9 @@ export function keycloakify(params?: Params) { /import\.meta\.env(?:(?:\.BASE_URL)|(?:\["BASE_URL"\]))/g, [ `(`, - `(window.${nameOfTheGlobal} === undefined || import.meta.env.MODE === "development")?`, + `(window.kcContext === undefined || import.meta.env.MODE === "development")?`, `"${urlPathname ?? "/"}":`, - `(window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/")`, + `(window.kcContext.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/")`, `)` ].join("") ); diff --git a/test/bin/replacers.spec.ts b/test/bin/replacers.spec.ts index 975ea00b..ef849cf8 100644 --- a/test/bin/replacers.spec.ts +++ b/test/bin/replacers.spec.ts @@ -7,10 +7,7 @@ import { import { replaceImportsInInlineCssCode } from "keycloakify/bin/keycloakify/replacers/replaceImportsInInlineCssCode"; import { same } from "evt/tools/inDepth/same"; import { expect, it, describe } from "vitest"; -import { - basenameOfTheKeycloakifyResourcesDir, - nameOfTheGlobal -} from "keycloakify/bin/shared/constants"; +import { basenameOfTheKeycloakifyResourcesDir } from "keycloakify/bin/shared/constants"; describe("js replacer - vite", () => { it("replaceImportsInJsCode_vite - 1", () => { @@ -95,13 +92,13 @@ describe("js replacer - vite", () => { }); const fixedJsCodeExpected = ` - S=(window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/assets/keycloakify-logo-mqjydaoZ.png"),H=(()=>{ + S=(window.kcContext.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/assets/keycloakify-logo-mqjydaoZ.png"),H=(()=>{ function __vite__mapDeps(indexes) { if (!__vite__mapDeps.viteFileDeps) { __vite__mapDeps.viteFileDeps = [ - (window.${nameOfTheGlobal}.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/assets/Login-dJpPRzM4.js"), - (window.${nameOfTheGlobal}.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/assets/index-XwzrZ5Gu.js") + (window.kcContext.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/assets/Login-dJpPRzM4.js"), + (window.kcContext.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/assets/index-XwzrZ5Gu.js") ] } return indexes.map((i) => __vite__mapDeps.viteFileDeps[i]) @@ -154,13 +151,13 @@ describe("js replacer - vite", () => { }); const fixedJsCodeExpected = ` - S=(window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/foo/bar/keycloakify-logo-mqjydaoZ.png"),H=(()=>{ + S=(window.kcContext.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/foo/bar/keycloakify-logo-mqjydaoZ.png"),H=(()=>{ function __vite__mapDeps(indexes) { if (!__vite__mapDeps.viteFileDeps) { __vite__mapDeps.viteFileDeps = [ - (window.${nameOfTheGlobal}.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/foo/bar/Login-dJpPRzM4.js"), - (window.${nameOfTheGlobal}.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/foo/bar/index-XwzrZ5Gu.js") + (window.kcContext.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/foo/bar/Login-dJpPRzM4.js"), + (window.kcContext.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/foo/bar/index-XwzrZ5Gu.js") ] } return indexes.map((i) => __vite__mapDeps.viteFileDeps[i]) @@ -213,13 +210,13 @@ describe("js replacer - vite", () => { }); const fixedJsCodeExpected = ` - S=(window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/assets/keycloakify-logo-mqjydaoZ.png"),H=(()=>{ + S=(window.kcContext.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/assets/keycloakify-logo-mqjydaoZ.png"),H=(()=>{ function __vite__mapDeps(indexes) { if (!__vite__mapDeps.viteFileDeps) { __vite__mapDeps.viteFileDeps = [ - (window.${nameOfTheGlobal}.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/assets/Login-dJpPRzM4.js"), - (window.${nameOfTheGlobal}.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/assets/index-XwzrZ5Gu.js") + (window.kcContext.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/assets/Login-dJpPRzM4.js"), + (window.kcContext.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/assets/index-XwzrZ5Gu.js") ] } return indexes.map((i) => __vite__mapDeps.viteFileDeps[i])