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-05-15 05:14:01 +02:00
|
|
|
import type { BuildOptions } from "../../../shared/buildOptions";
|
2024-01-30 00:06:17 +01:00
|
|
|
import * as nodePath from "path";
|
|
|
|
import { replaceAll } from "../../../tools/String.prototype.replaceAll";
|
|
|
|
|
|
|
|
export type BuildOptionsLike = {
|
|
|
|
reactAppBuildDirPath: string;
|
|
|
|
assetsDirPath: string;
|
|
|
|
urlPathname: string | undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
assert<BuildOptions extends BuildOptionsLike ? true : false>();
|
|
|
|
|
|
|
|
export function replaceImportsInJsCode_vite(params: {
|
|
|
|
jsCode: string;
|
|
|
|
buildOptions: BuildOptionsLike;
|
|
|
|
basenameOfAssetsFiles: string[];
|
|
|
|
systemType?: "posix" | "win32";
|
|
|
|
}): {
|
|
|
|
fixedJsCode: string;
|
|
|
|
} {
|
2024-05-20 15:48:51 +02:00
|
|
|
const {
|
|
|
|
jsCode,
|
|
|
|
buildOptions,
|
|
|
|
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: {
|
|
|
|
if (buildOptions.urlPathname === undefined) {
|
|
|
|
break replace_base_javacript_import;
|
|
|
|
}
|
|
|
|
// Optimization
|
|
|
|
if (!jsCode.includes(buildOptions.urlPathname)) {
|
|
|
|
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(
|
|
|
|
buildOptions.urlPathname,
|
|
|
|
"/",
|
|
|
|
"\\/"
|
|
|
|
)}"\\+\\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(
|
|
|
|
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;
|
|
|
|
})();
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
`"${buildOptions.urlPathname ?? "/"}${relativePathOfAssetFile}"`,
|
|
|
|
`(window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/${relativePathOfAssetFile}")`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return { fixedJsCode };
|
|
|
|
}
|