From 0eb4ab85b33459510e4e5a47b0fdc8e7edc4bb1f Mon Sep 17 00:00:00 2001 From: garronej Date: Tue, 2 Aug 2022 21:00:52 +0200 Subject: [PATCH] Fix lazy loading of css chunks #141 --- .../replaceImportFromStatic.ts | 15 ++++---- src/test/bin/replaceImportFromStatic.ts | 34 +++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/bin/build-keycloak-theme/replaceImportFromStatic.ts b/src/bin/build-keycloak-theme/replaceImportFromStatic.ts index ad8548b3..9f0eb109 100644 --- a/src/bin/build-keycloak-theme/replaceImportFromStatic.ts +++ b/src/bin/build-keycloak-theme/replaceImportFromStatic.ts @@ -16,10 +16,9 @@ export function replaceImportsFromStaticInJsCode(params: { jsCode: string; urlOr const { jsCode, urlOrigin } = params; - const fixedJsCode = jsCode - .replace( - /([a-zA-Z]+)\.([a-zA-Z]+)=function\(([a-zA-Z]+)\){return"static\/js\/"/g, - (...[, n, u, e]) => ` + const getReplaceArgs = (language: "js" | "css"): Parameters => [ + new RegExp(`([a-zA-Z]+)\\.([a-zA-Z]+)=function\\(([a-zA-Z]+)\\){return"static\\/${language}\\/"`, "g"), + (...[, n, u, e]) => ` ${n}[(function(){ ${ urlOrigin === undefined @@ -38,8 +37,12 @@ export function replaceImportsFromStaticInJsCode(params: { jsCode: string; urlOr ` } return "${u}"; - })()] = function(${e}) { return "${urlOrigin === undefined ? "/build/" : ""}static/js/"`, - ) + })()] = function(${e}) { return "${urlOrigin === undefined ? "/build/" : ""}static/${language}/"`, + ]; + + const fixedJsCode = jsCode + .replace(...getReplaceArgs("js")) + .replace(...getReplaceArgs("css")) .replace(/([a-zA-Z]+\.[a-zA-Z]+)\+"static\//g, (...[, group]) => urlOrigin === undefined ? `window.${ftlValuesGlobalName}.url.resourcesPath + "/build/static/` diff --git a/src/test/bin/replaceImportFromStatic.ts b/src/test/bin/replaceImportFromStatic.ts index 0658dd63..a5aad440 100644 --- a/src/test/bin/replaceImportFromStatic.ts +++ b/src/test/bin/replaceImportFromStatic.ts @@ -35,6 +35,12 @@ import { assetIsSameCode } from "../tools/assertIsSameCode"; 922: "be170a73" } [e] + ".chunk.js" } + + t.miniCssF=function(e){return"static/css/"+e+"."+{ + 164:"dcfd7749", + 908:"67c9ed2c" + }[e]+".chunk.css" + } `; { @@ -69,6 +75,20 @@ import { assetIsSameCode } from "../tools/assertIsSameCode"; 922: "be170a73" } [e] + ".chunk.js" } + + t[(function (){ + Object.defineProperty(t, "p", { + get: function() { return window.kcContext.url.resourcesPath; }, + set: function (){} + }); + return "miniCssF"; + })()] = function(e) { + return "/build/static/css/" + e + "." + { + 164:"dcfd7749", + 908:"67c9ed2c" + } [e] + ".chunk.css" + } + `; assetIsSameCode(fixedJsCode, fixedJsCodeExpected); @@ -107,6 +127,20 @@ import { assetIsSameCode } from "../tools/assertIsSameCode"; 922: "be170a73" } [e] + ".chunk.js" } + + t[(function (){ + var p= ""; + Object.defineProperty(t, "p", { + get: function() { return ("kcContext" in window ? "https://demo-app.keycloakify.dev" : "") + p; }, + set: function (value){ p = value; } + }); + return "miniCssF"; + })()] = function(e) { + return "static/css/" + e + "." + { + 164:"dcfd7749", + 908:"67c9ed2c" + } [e] + ".chunk.css" + } `; assetIsSameCode(fixedJsCode, fixedJsCodeExpected);