104 lines
3.3 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";
2024-06-09 09:15:16 +02:00
import type { BuildContext } from "../../../shared/buildContext";
2024-01-30 00:06:17 +01:00
import * as nodePath from "path";
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
export function replaceImportsInJsCode_vite(params: {
jsCode: string;
2024-06-09 09:15:16 +02:00
buildContext: BuildContextLike;
2024-01-30 00:06:17 +01:00
basenameOfAssetsFiles: string[];
systemType?: "posix" | "win32";
}): {
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
basenameOfAssetsFiles,
systemType = nodePath.sep === "/" ? "posix" : "win32"
} = params;
2024-01-30 00:06:17 +01:00
const { relative: pathRelative, sep: pathSep } = nodePath[systemType];
let fixedJsCode = jsCode;
replace_base_javacript_import: {
2024-06-09 09:15:16 +02:00
if (buildContext.urlPathname === undefined) {
2024-01-30 00:06:17 +01:00
break replace_base_javacript_import;
}
// Optimization
2024-06-09 09:15:16 +02:00
if (!jsCode.includes(buildContext.urlPathname)) {
2024-01-30 00:06:17 +01:00
break replace_base_javacript_import;
}
// Replace `Hv=function(e){return"/abcde12345/"+e}` by `Hv=function(e){return"/"+e}`
fixedJsCode = fixedJsCode.replace(
new RegExp(
2024-05-20 15:48:51 +02:00
`([\\w\\$][\\w\\d\\$]*)=function\\(([\\w\\$][\\w\\d\\$]*)\\)\\{return"${replaceAll(
2024-06-09 09:15:16 +02:00
buildContext.urlPathname,
2024-05-20 15:48:51 +02:00
"/",
"\\/"
)}"\\+\\2\\}`,
2024-01-30 00:06:17 +01:00
"g"
),
2024-05-20 15:48:51 +02:00
(...[, funcName, paramName]) =>
`${funcName}=function(${paramName}){return"/"+${paramName}}`
2024-01-30 00:06:17 +01:00
);
}
replace_javascript_relatives_import_paths: {
// Example: "assets/ 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;
})();
// Optimization
if (!jsCode.includes(staticDir)) {
break replace_javascript_relatives_import_paths;
}
basenameOfAssetsFiles
.map(basenameOfAssetsFile => `${staticDir}${basenameOfAssetsFile}`)
.forEach(relativePathOfAssetFile => {
fixedJsCode = replaceAll(
fixedJsCode,
`"${relativePathOfAssetFile}"`,
`(window.${nameOfTheGlobal}.url.resourcesPath.substring(1) + "/${basenameOfTheKeycloakifyResourcesDir}/${relativePathOfAssetFile}")`
);
fixedJsCode = replaceAll(
fixedJsCode,
2024-06-09 09:15:16 +02:00
`"${buildContext.urlPathname ?? "/"}${relativePathOfAssetFile}"`,
2024-01-30 00:06:17 +01:00
`(window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/${relativePathOfAssetFile}")`
);
});
}
return { fixedJsCode };
}