Disalow releative basename in vite config

This commit is contained in:
Joseph Garrone 2024-02-12 01:34:34 +01:00
parent b03340ed10
commit b2f2c3e386

View File

@ -1,6 +1,5 @@
import { join as pathJoin, relative as pathRelative, sep as pathSep } from "path";
import type { Plugin } from "vite";
import { assert } from "tsafe/assert";
import * as fs from "fs";
import { resolvedViteConfigJsonBasename, nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir, keycloak_resources } from "../bin/constants";
import type { ResolvedViteConfig } from "../bin/keycloakify/buildOptions/resolvedViteConfig";
@ -9,15 +8,16 @@ import { replaceAll } from "../bin/tools/String.prototype.replaceAll";
import { id } from "tsafe/id";
import { rm } from "../bin/tools/fs.rm";
import { copyKeycloakResourcesToPublic } from "../bin/copy-keycloak-resources-to-public";
import { assert } from "tsafe/assert";
export function keycloakify(): Plugin {
export function keycloakify() {
let reactAppRootDirPath: string | undefined = undefined;
let urlPathname: string | undefined = undefined;
let buildDirPath: string | undefined = undefined;
let command: "build" | "serve" | undefined = undefined;
return {
"name": "keycloakify",
const plugin = {
"name": "keycloakify" as const,
"configResolved": async resolvedConfig => {
command = resolvedConfig.command;
@ -25,6 +25,15 @@ export function keycloakify(): Plugin {
urlPathname = (() => {
let out = resolvedConfig.env.BASE_URL;
if (out.startsWith(".") && command === "build") {
throw new Error(
[
`BASE_URL=${out} is not supported By Keycloakify. Use an absolute URL instead.`,
`If this is a problem, please open an issue at https://github.com/keycloakify/keycloakify/issues/new`
].join("\n")
);
}
if (out === undefined) {
return undefined;
}
@ -91,9 +100,8 @@ export function keycloakify(): Plugin {
}
}
const isJavascriptFile = id.endsWith(".js") || id.endsWith(".jsx");
{
const isJavascriptFile = id.endsWith(".js") || id.endsWith(".jsx");
const isTypeScriptFile = id.endsWith(".ts") || id.endsWith(".tsx");
if (!isTypeScriptFile && !isJavascriptFile) {
@ -101,8 +109,6 @@ export function keycloakify(): Plugin {
}
}
const windowToken = isJavascriptFile ? "window" : "(window as any)";
if (transformedCode === undefined) {
transformedCode = code;
}
@ -112,9 +118,9 @@ export function keycloakify(): Plugin {
"import.meta.env.BASE_URL",
[
`(`,
`(${windowToken}.${nameOfTheGlobal} === undefined || import.meta.env.MODE === "development") ?`,
` "${urlPathname ?? "/"}" :`,
` \`\${${windowToken}.${nameOfTheGlobal}.url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/\``,
`(window.${nameOfTheGlobal} === undefined || import.meta.env.MODE === "development")?`,
`"${urlPathname ?? "/"}":`,
`(window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/")`,
`)`
].join("")
);
@ -128,7 +134,7 @@ export function keycloakify(): Plugin {
"code": transformedCode
};
},
"buildEnd": async () => {
"closeBundle": async () => {
assert(command !== undefined);
if (command !== "build") {
@ -139,5 +145,7 @@ export function keycloakify(): Plugin {
await rm(pathJoin(buildDirPath, keycloak_resources), { "recursive": true, "force": true });
}
};
} satisfies Plugin;
return plugin as any;
}