115 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-05-20 15:48:51 +02:00
import {
nameOfTheGlobal,
basenameOfTheKeycloakifyResourcesDir
} from "../../../shared/constants";
2024-01-30 00:06:17 +01:00
import { assert } from "tsafe/assert";
import type { BuildOptions } from "../../../shared/buildOptions";
2024-01-30 00:10:59 +01:00
import * as nodePath from "path";
2024-01-30 00:06:17 +01:00
import { replaceAll } from "../../../tools/String.prototype.replaceAll";
export type BuildOptionsLike = {
reactAppBuildDirPath: string;
assetsDirPath: string;
urlPathname: string | undefined;
};
assert<BuildOptions extends BuildOptionsLike ? true : false>();
2024-05-20 15:48:51 +02:00
export function replaceImportsInJsCode_webpack(params: {
jsCode: string;
buildOptions: BuildOptionsLike;
systemType?: "posix" | "win32";
}): {
2024-01-30 00:10:59 +01:00
fixedJsCode: string;
} {
2024-05-20 15:48:51 +02:00
const {
jsCode,
buildOptions,
systemType = nodePath.sep === "/" ? "posix" : "win32"
} = params;
2024-01-30 00:10:59 +01:00
const { relative: pathRelative, sep: pathSep } = nodePath[systemType];
2024-01-30 00:06:17 +01:00
let fixedJsCode = jsCode;
if (buildOptions.urlPathname !== undefined) {
// "__esModule",{value:!0})},n.p="/foo-bar/",function(){if("undefined" -> ... n.p="/" ...
2024-01-30 00:06:17 +01:00
fixedJsCode = fixedJsCode.replace(
2024-05-20 15:48:51 +02:00
new RegExp(
`,([a-zA-Z]\\.[a-zA-Z])="${replaceAll(
buildOptions.urlPathname,
"/",
"\\/"
)}",`,
"g"
),
2024-01-30 00:06:17 +01:00
(...[, assignTo]) => `,${assignTo}="/",`
);
}
// Example: "static/ or "foo/bar/"
const staticDir = (() => {
2024-05-20 15:48:51 +02:00
let out = pathRelative(
buildOptions.reactAppBuildDirPath,
buildOptions.assetsDirPath
);
2024-01-30 00:06:17 +01:00
out = replaceAll(out, pathSep, "/") + "/";
if (out === "/") {
2024-05-20 15:48:51 +02:00
throw new Error(
`The assetsDirPath must be a subdirectory of reactAppBuildDirPath`
);
2024-01-30 00:06:17 +01:00
}
return out;
})();
2024-05-20 15:48:51 +02:00
const getReplaceArgs = (
language: "js" | "css"
): Parameters<typeof String.prototype.replace> => [
new RegExp(
`([a-zA-Z_]+)\\.([a-zA-Z]+)=(function\\(([a-z]+)\\){return|([a-z]+)=>)"${staticDir.replace(
/\//g,
"\\/"
)}${language}\\/"`,
"g"
),
2024-01-30 00:06:17 +01:00
(...[, n, u, matchedFunction, eForFunction]) => {
const isArrowFunction = matchedFunction.includes("=>");
2024-05-20 15:48:51 +02:00
const e = isArrowFunction
? matchedFunction.replace("=>", "").trim()
: eForFunction;
2024-01-30 00:06:17 +01:00
return `
${n}[(function(){
var pd = Object.getOwnPropertyDescriptor(${n}, "p");
if( pd === undefined || pd.configurable ){
Object.defineProperty(${n}, "p", {
get: function() { return window.${nameOfTheGlobal}.url.resourcesPath; },
set: function() {}
});
}
return "${u}";
2024-05-20 15:48:51 +02:00
})()] = ${
isArrowFunction ? `${e} =>` : `function(${e}) { return `
} "/${basenameOfTheKeycloakifyResourcesDir}/${staticDir}${language}/"`
2024-01-30 00:06:17 +01:00
.replace(/\s+/g, " ")
.trim();
}
];
fixedJsCode = fixedJsCode
.replace(...getReplaceArgs("js"))
.replace(...getReplaceArgs("css"))
.replace(
2024-05-20 15:48:51 +02:00
new RegExp(
`[a-zA-Z]+\\.[a-zA-Z]+\\+"${staticDir.replace(/\//g, "\\/")}`,
"g"
),
2024-01-30 00:06:17 +01:00
`window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/${staticDir}`
);
return { fixedJsCode };
}