From 4c2e01a7a89da2060a45a04e0ad0cf9e88b65cc0 Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Tue, 30 Jan 2024 00:56:47 +0100 Subject: [PATCH] Done with implementation of webpack js replacer test case for support of non root base --- .../replaceImportsInJsCode/webpack.ts | 8 ++--- test/bin/replacers.spec.ts | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/bin/keycloakify/replacers/replaceImportsInJsCode/webpack.ts b/src/bin/keycloakify/replacers/replaceImportsInJsCode/webpack.ts index dcbefdf6..5d49cb77 100644 --- a/src/bin/keycloakify/replacers/replaceImportsInJsCode/webpack.ts +++ b/src/bin/keycloakify/replacers/replaceImportsInJsCode/webpack.ts @@ -29,18 +29,18 @@ export function replaceImportsInJsCode_webpack(params: { jsCode: string; buildOp ); } - // d={NODE_ENV:"production",PUBLIC_URL:"/abcde12345",WDS_SOCKET_HOST + // d={NODE_ENV:"production",PUBLIC_URL:"/foo-bar",WDS_SOCKET_HOST // d={NODE_ENV:"production",PUBLIC_URL:"",WDS_SOCKET_HOST // -> - // PUBLIC_URL:`${window.kcContext.url.resourcesPath}/build"` + // ... PUBLIC_URL:window.kcContext.url.resourcesPath+"/build" ... fixedJsCode = fixedJsCode.replace( new RegExp( `NODE_ENV:"production",PUBLIC_URL:"${ buildOptions.urlPathname !== undefined ? replaceAll(buildOptions.urlPathname.slice(0, -1), "/", "\\/") : "" - }",`, + }"`, "g" ), - `NODE_ENV:"production",PUBLIC_URL: window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}",` + `NODE_ENV:"production",PUBLIC_URL:window.${nameOfTheGlobal}.url.resourcesPath+"/${basenameOfTheKeycloakifyResourcesDir}"` ); // Example: "static/ or "foo/bar/" diff --git a/test/bin/replacers.spec.ts b/test/bin/replacers.spec.ts index a006c438..d0dc1cb9 100644 --- a/test/bin/replacers.spec.ts +++ b/test/bin/replacers.spec.ts @@ -361,6 +361,40 @@ describe("js replacer - webpack", () => { expect(isSameCode(fixedJsCode, fixedJsCodeExpected)).toBe(true); }); + + it("replaceImportsInJsCode_webpack - 4", () => { + const jsCodeUntransformed = `d={NODE_ENV:"production",PUBLIC_URL:"",WDS_SOCKET_HOST`; + + const { fixedJsCode } = replaceImportsInJsCode_webpack({ + "jsCode": jsCodeUntransformed, + "buildOptions": { + "reactAppBuildDirPath": "/Users/someone/github/keycloakify-starter/build", + "assetsDirPath": "/Users/someone/github/keycloakify-starter/dist/build/static", + "urlPathname": undefined + } + }); + + const fixedJsCodeExpected = `d={NODE_ENV:"production",PUBLIC_URL:window.${nameOfTheGlobal}.url.resourcesPath+"/${basenameOfTheKeycloakifyResourcesDir}",WDS_SOCKET_HOST`; + + expect(isSameCode(fixedJsCode, fixedJsCodeExpected)).toBe(true); + }); + + it("replaceImportsInJsCode_webpack - 5", () => { + const jsCodeUntransformed = `d={NODE_ENV:"production",PUBLIC_URL:"/foo-bar",WDS_SOCKET_HOST`; + + const { fixedJsCode } = replaceImportsInJsCode_webpack({ + "jsCode": jsCodeUntransformed, + "buildOptions": { + "reactAppBuildDirPath": "/Users/someone/github/keycloakify-starter/build", + "assetsDirPath": "/Users/someone/github/keycloakify-starter/dist/build/static", + "urlPathname": "/foo-bar/" + } + }); + + const fixedJsCodeExpected = `d={NODE_ENV:"production",PUBLIC_URL:window.${nameOfTheGlobal}.url.resourcesPath+"/${basenameOfTheKeycloakifyResourcesDir}",WDS_SOCKET_HOST`; + + expect(isSameCode(fixedJsCode, fixedJsCodeExpected)).toBe(true); + }); }); describe("css replacer", () => {