2024-06-09 09:15:16 +02:00
|
|
|
import type { BuildContext } from "../../shared/buildContext";
|
2024-07-13 19:33:59 +02:00
|
|
|
import { BASENAME_OF_KEYCLOAKIFY_RESOURCES_DIR } from "../../shared/constants";
|
2022-08-16 14:41:06 +07:00
|
|
|
import { assert } from "tsafe/assert";
|
2024-06-19 01:41:22 +02:00
|
|
|
import { posix } from "path";
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-06-09 09:15:16 +02:00
|
|
|
export type BuildContextLike = {
|
2022-08-16 14:41:06 +07:00
|
|
|
urlPathname: string | undefined;
|
|
|
|
};
|
|
|
|
|
2024-06-09 09:15:16 +02:00
|
|
|
assert<BuildContext extends BuildContextLike ? true : false>();
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-06-19 01:41:22 +02:00
|
|
|
export function replaceImportsInCssCode(params: {
|
|
|
|
cssCode: string;
|
2024-06-19 22:41:25 +02:00
|
|
|
cssFileRelativeDirPath: string | undefined;
|
2024-06-09 09:15:16 +02:00
|
|
|
buildContext: BuildContextLike;
|
2024-05-20 15:48:51 +02:00
|
|
|
}): {
|
2024-06-19 01:41:22 +02:00
|
|
|
fixedCssCode: string;
|
2022-08-16 14:41:06 +07:00
|
|
|
} {
|
2024-07-13 18:17:21 +02:00
|
|
|
const { cssCode, cssFileRelativeDirPath, buildContext } = params;
|
2024-06-19 01:41:22 +02:00
|
|
|
|
2024-07-14 17:45:34 +02:00
|
|
|
let fixedCssCode = cssCode;
|
|
|
|
|
|
|
|
[
|
|
|
|
/url\("(\/[^/][^"]+)"\)/g,
|
|
|
|
/url\('(\/[^/][^']+)'\)/g,
|
|
|
|
/url\((\/[^/][^)]+)\)/g
|
|
|
|
].forEach(
|
|
|
|
regex =>
|
|
|
|
(fixedCssCode = fixedCssCode.replace(
|
|
|
|
regex,
|
|
|
|
(match, assetFileAbsoluteUrlPathname) => {
|
|
|
|
if (buildContext.urlPathname !== undefined) {
|
|
|
|
if (
|
|
|
|
!assetFileAbsoluteUrlPathname.startsWith(
|
|
|
|
buildContext.urlPathname
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// NOTE: Should never happen
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
assetFileAbsoluteUrlPathname =
|
|
|
|
assetFileAbsoluteUrlPathname.replace(
|
|
|
|
buildContext.urlPathname,
|
|
|
|
"/"
|
|
|
|
);
|
|
|
|
}
|
2024-06-19 22:41:25 +02:00
|
|
|
|
2024-07-14 17:45:34 +02:00
|
|
|
inline_style_in_html: {
|
|
|
|
if (cssFileRelativeDirPath !== undefined) {
|
|
|
|
break inline_style_in_html;
|
|
|
|
}
|
2024-06-19 22:41:25 +02:00
|
|
|
|
2024-07-14 17:45:34 +02:00
|
|
|
return `url("\${xKeycloakify.resourcesPath}/${BASENAME_OF_KEYCLOAKIFY_RESOURCES_DIR}${assetFileAbsoluteUrlPathname}")`;
|
|
|
|
}
|
2024-06-19 01:41:22 +02:00
|
|
|
|
2024-07-14 17:45:34 +02:00
|
|
|
const assetFileRelativeUrlPathname = posix.relative(
|
|
|
|
cssFileRelativeDirPath.replace(/\\/g, "/"),
|
|
|
|
assetFileAbsoluteUrlPathname.replace(/^\//, "")
|
|
|
|
);
|
|
|
|
|
|
|
|
return `url("${assetFileRelativeUrlPathname}")`;
|
|
|
|
}
|
|
|
|
))
|
2024-06-19 01:41:22 +02:00
|
|
|
);
|
2022-08-16 14:41:06 +07:00
|
|
|
|
2024-06-19 01:41:22 +02:00
|
|
|
return { fixedCssCode };
|
2022-08-16 14:41:06 +07:00
|
|
|
}
|