From b116f22152d31d54991be9b0b2f081a976a0bdd0 Mon Sep 17 00:00:00 2001 From: garronej Date: Thu, 28 Jul 2022 04:24:10 +0200 Subject: [PATCH] =?UTF-8?q?Support=20for=20React.lazy()=20#141=20?= =?UTF-8?q?=F0=9F=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../replaceImportFromStatic.ts | 24 +++++++++++++++ src/test/bin/replaceImportFromStatic.ts | 30 +++++++++---------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/bin/build-keycloak-theme/replaceImportFromStatic.ts b/src/bin/build-keycloak-theme/replaceImportFromStatic.ts index bb0f9b3d..ad8548b3 100644 --- a/src/bin/build-keycloak-theme/replaceImportFromStatic.ts +++ b/src/bin/build-keycloak-theme/replaceImportFromStatic.ts @@ -17,11 +17,35 @@ 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]) => ` + ${n}[(function(){ + ${ + urlOrigin === undefined + ? ` + Object.defineProperty(${n}, "p", { + get: function() { return window.${ftlValuesGlobalName}.url.resourcesPath; }, + set: function (){} + }); + ` + : ` + var p= ""; + Object.defineProperty(${n}, "p", { + get: function() { return ("${ftlValuesGlobalName}" in window ? "${urlOrigin}" : "") + p; }, + set: function (value){ p = value;} + }); + ` + } + return "${u}"; + })()] = function(${e}) { return "${urlOrigin === undefined ? "/build/" : ""}static/js/"`, + ) .replace(/([a-zA-Z]+\.[a-zA-Z]+)\+"static\//g, (...[, group]) => urlOrigin === undefined ? `window.${ftlValuesGlobalName}.url.resourcesPath + "/build/static/` : `("${ftlValuesGlobalName}" in window ? "${urlOrigin}" : "") + ${group} + "static/`, ) + //TODO: Write a test case for this .replace(/".chunk.css",([a-zA-Z])+=([a-zA-Z]+\.[a-zA-Z]+)\+([a-zA-Z]+),/, (...[, group1, group2, group3]) => urlOrigin === undefined ? `".chunk.css",${group1} = window.${ftlValuesGlobalName}.url.resourcesPath + "/build/" + ${group3},` diff --git a/src/test/bin/replaceImportFromStatic.ts b/src/test/bin/replaceImportFromStatic.ts index 58b19ed1..0658dd63 100644 --- a/src/test/bin/replaceImportFromStatic.ts +++ b/src/test/bin/replaceImportFromStatic.ts @@ -23,20 +23,18 @@ import { assetIsSameCode } from "../tools/assertIsSameCode"; }[e] + ".chunk.js" } - function f2() { + function sameAsF() { return a.p+"static/js/" + ({}[e] || e) + "." + { 3: "0664cdc0" }[e] + ".chunk.js" } - n.u = function(e) { - return "static/js/" + e + "." + { + n.u=function(e){return"static/js/" + e + "." + { 147: "6c5cee76", 787: "8da10fcf", 922: "be170a73" } [e] + ".chunk.js" } - `; { @@ -52,17 +50,19 @@ import { assetIsSameCode } from "../tools/assertIsSameCode"; }[e] + ".chunk.js" } - function f2() { + function sameAsF() { return window.kcContext.url.resourcesPath + "/build/static/js/" + ({}[e] || e) + "." + { 3: "0664cdc0" }[e] + ".chunk.js" } - n.u = function(e) { + n[(function (){ Object.defineProperty(n, "p", { get: function() { return window.kcContext.url.resourcesPath; }, set: function (){} }); + return "u"; + })()] = function(e) { return "/build/static/js/" + e + "." + { 147: "6c5cee76", 787: "8da10fcf", @@ -87,20 +87,20 @@ import { assetIsSameCode } from "../tools/assertIsSameCode"; }[e] + ".chunk.js" } - function f2() { + function sameAsF() { return ("kcContext" in window ? "https://demo-app.keycloakify.dev" : "") + a.p + "static/js/" + ({}[e] || e) + "." + { 3: "0664cdc0" }[e] + ".chunk.js" } - n.u = function(e) { - { - var p= ""; - Object.defineProperty(n, "p", { - get: function() { return ("kcContext" in window ? "https://demo-app.keycloakify.dev" : "") + p; }, - set: function (value){ p = value;} - }); - } + n[(function (){ + var p= ""; + Object.defineProperty(n, "p", { + get: function() { return ("kcContext" in window ? "https://demo-app.keycloakify.dev" : "") + p; }, + set: function (value){ p = value; } + }); + return "u"; + })()] = function(e) { return "static/js/" + e + "." + { 147: "6c5cee76", 787: "8da10fcf",