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

25 lines
880 B
TypeScript
Raw Normal View History

import type { BuildOptions } from "../../shared/buildOptions";
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
2023-08-21 05:54:17 +02:00
export type BuildOptionsLike = {
urlPathname: string | undefined;
};
2022-08-16 14:41:06 +07:00
2023-08-21 05:54:17 +02:00
assert<BuildOptions extends BuildOptionsLike ? true : false>();
2022-08-16 14:41:06 +07:00
export function replaceImportsInInlineCssCode(params: { cssCode: string; buildOptions: BuildOptionsLike }): {
fixedCssCode: string;
} {
const { cssCode, buildOptions } = params;
const fixedCssCode = cssCode.replace(
buildOptions.urlPathname === undefined
? /url\(["']?\/([^/][^)"']+)["']?\)/g
: new RegExp(`url\\(["']?${buildOptions.urlPathname}([^)"']+)["']?\\)`, "g"),
2024-01-30 00:06:17 +01:00
(...[, group]) => `url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/${group})`
2022-08-16 14:41:06 +07:00
);
return { fixedCssCode };
}