112 lines
3.5 KiB
TypeScript
Raw Normal View History

import { basenameOfTheKeycloakifyResourcesDir } from "../../../shared/constants";
2024-01-30 00:06:17 +01:00
import { assert } from "tsafe/assert";
2024-06-09 09:15:16 +02:00
import type { BuildContext } from "../../../shared/buildContext";
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";
2024-06-09 09:15:16 +02:00
export type BuildContextLike = {
projectBuildDirPath: string;
2024-01-30 00:06:17 +01:00
assetsDirPath: string;
urlPathname: string | undefined;
};
2024-06-09 09:15:16 +02:00
assert<BuildContext extends BuildContextLike ? true : false>();
2024-01-30 00:06:17 +01:00
2024-05-20 15:48:51 +02:00
export function replaceImportsInJsCode_webpack(params: {
jsCode: string;
2024-06-09 09:15:16 +02:00
buildContext: BuildContextLike;
2024-05-20 15:48:51 +02:00
systemType?: "posix" | "win32";
}): {
2024-01-30 00:10:59 +01:00
fixedJsCode: string;
} {
2024-05-20 15:48:51 +02:00
const {
jsCode,
2024-06-09 09:15:16 +02:00
buildContext,
2024-05-20 15:48:51 +02:00
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;
2024-06-09 09:15:16 +02:00
if (buildContext.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(
2024-06-09 09:15:16 +02:00
buildContext.urlPathname,
2024-05-20 15:48:51 +02:00
"/",
"\\/"
)}",`,
"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(
2024-06-09 09:15:16 +02:00
buildContext.projectBuildDirPath,
buildContext.assetsDirPath
2024-05-20 15:48:51 +02:00
);
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 projectBuildDirPath`
2024-05-20 15:48:51 +02:00
);
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.kcContext.url.resourcesPath; },
2024-01-30 00:06:17 +01:00
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"
),
`window.kcContext.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/${staticDir}`
2024-01-30 00:06:17 +01:00
);
return { fixedJsCode };
}