2022-08-16 14:41:06 +07:00
|
|
|
import { ftlValuesGlobalName } from "../ftlValuesGlobalName";
|
|
|
|
|
2023-08-21 05:54:17 +02:00
|
|
|
export function replaceImportsFromStaticInJsCode(params: { jsCode: string }): { fixedJsCode: string } {
|
2022-08-16 14:41:06 +07:00
|
|
|
/*
|
|
|
|
NOTE:
|
|
|
|
|
|
|
|
When we have urlOrigin defined it means that
|
|
|
|
we are building with --external-assets
|
|
|
|
so we have to make sur that the fixed js code will run
|
|
|
|
inside and outside keycloak.
|
|
|
|
|
|
|
|
When urlOrigin isn't defined we can assume the fixedJsCode
|
|
|
|
will always run in keycloak context.
|
|
|
|
*/
|
|
|
|
|
2023-08-21 05:54:17 +02:00
|
|
|
const { jsCode } = params;
|
2022-08-16 14:41:06 +07:00
|
|
|
|
|
|
|
const getReplaceArgs = (language: "js" | "css"): Parameters<typeof String.prototype.replace> => [
|
2022-09-05 00:08:50 +02:00
|
|
|
new RegExp(`([a-zA-Z_]+)\\.([a-zA-Z]+)=function\\(([a-zA-Z]+)\\){return"static\\/${language}\\/"`, "g"),
|
2022-08-16 14:41:06 +07:00
|
|
|
(...[, n, u, e]) => `
|
|
|
|
${n}[(function(){
|
2022-09-05 00:08:50 +02:00
|
|
|
var pd= Object.getOwnPropertyDescriptor(${n}, "p");
|
2022-09-01 17:22:15 +02:00
|
|
|
if( pd === undefined || pd.configurable ){
|
2022-08-16 14:41:06 +07:00
|
|
|
Object.defineProperty(${n}, "p", {
|
2023-08-21 05:54:17 +02:00
|
|
|
get: function() { return window.${ftlValuesGlobalName}.url.resourcesPath; },
|
|
|
|
set: function (){}
|
2022-08-16 14:41:06 +07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return "${u}";
|
2023-08-21 05:54:17 +02:00
|
|
|
})()] = function(${e}) { return "${true ? "/build/" : ""}static/${language}/"`
|
2022-08-16 14:41:06 +07:00
|
|
|
];
|
|
|
|
|
|
|
|
const fixedJsCode = jsCode
|
|
|
|
.replace(...getReplaceArgs("js"))
|
|
|
|
.replace(...getReplaceArgs("css"))
|
2023-08-21 05:54:17 +02:00
|
|
|
.replace(/[a-zA-Z]+\.[a-zA-Z]+\+"static\//g, `window.${ftlValuesGlobalName}.url.resourcesPath + "/build/static/`)
|
2022-08-16 14:41:06 +07:00
|
|
|
//TODO: Write a test case for this
|
2023-08-21 05:54:17 +02:00
|
|
|
.replace(
|
|
|
|
/".chunk.css",([a-zA-Z])+=[a-zA-Z]+\.[a-zA-Z]+\+([a-zA-Z]+),/,
|
|
|
|
(...[, group1, group2]) => `".chunk.css",${group1} = window.${ftlValuesGlobalName}.url.resourcesPath + "/build/" + ${group2},`
|
2022-08-16 14:41:06 +07:00
|
|
|
);
|
|
|
|
|
|
|
|
return { fixedJsCode };
|
|
|
|
}
|