keycloak_theme/src/bin/keycloakify/replacers/replaceImportsInInlineCssCode.ts

29 lines
901 B
TypeScript
Raw Normal View History

2024-06-09 09:15:16 +02:00
import type { BuildContext } from "../../shared/buildContext";
2022-08-16 14:41:06 +07:00
import { assert } from "tsafe/assert";
import { basenameOfTheKeycloakifyResourcesDir } from "../../shared/constants";
2022-08-16 14:41:06 +07:00
2024-06-09 09:15:16 +02:00
export type BuildContextLike = {
2023-08-21 05:54:17 +02:00
urlPathname: string | undefined;
};
2022-08-16 14:41:06 +07:00
2024-06-09 09:15:16 +02:00
assert<BuildContext extends BuildContextLike ? true : false>();
2022-08-16 14:41:06 +07:00
2024-05-20 15:48:51 +02:00
export function replaceImportsInInlineCssCode(params: {
cssCode: string;
2024-06-09 09:15:16 +02:00
buildContext: BuildContextLike;
2024-05-20 15:48:51 +02:00
}): {
2022-08-16 14:41:06 +07:00
fixedCssCode: string;
} {
2024-06-09 09:15:16 +02:00
const { cssCode, buildContext } = params;
2022-08-16 14:41:06 +07:00
const fixedCssCode = cssCode.replace(
2024-06-09 09:15:16 +02:00
buildContext.urlPathname === undefined
2022-08-16 14:41:06 +07:00
? /url\(["']?\/([^/][^)"']+)["']?\)/g
2024-06-09 09:15:16 +02:00
: new RegExp(`url\\(["']?${buildContext.urlPathname}([^)"']+)["']?\\)`, "g"),
2024-05-20 15:48:51 +02:00
(...[, group]) =>
`url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/${group})`
2022-08-16 14:41:06 +07:00
);
return { fixedCssCode };
}