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 { join as pathJoin, relative as pathRelative, sep as pathSep } from "path";
import type { Plugin } from "vite"; import type { Plugin } from "vite";
import { assert } from "tsafe/assert";
import * as fs from "fs"; import * as fs from "fs";
import { resolvedViteConfigJsonBasename, nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir, keycloak_resources } from "../bin/constants"; import { resolvedViteConfigJsonBasename, nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir, keycloak_resources } from "../bin/constants";
import type { ResolvedViteConfig } from "../bin/keycloakify/buildOptions/resolvedViteConfig"; 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 { id } from "tsafe/id";
import { rm } from "../bin/tools/fs.rm"; import { rm } from "../bin/tools/fs.rm";
import { copyKeycloakResourcesToPublic } from "../bin/copy-keycloak-resources-to-public"; 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 reactAppRootDirPath: string | undefined = undefined;
let urlPathname: string | undefined = undefined; let urlPathname: string | undefined = undefined;
let buildDirPath: string | undefined = undefined; let buildDirPath: string | undefined = undefined;
let command: "build" | "serve" | undefined = undefined; let command: "build" | "serve" | undefined = undefined;
return { const plugin = {
"name": "keycloakify", "name": "keycloakify" as const,
"configResolved": async resolvedConfig => { "configResolved": async resolvedConfig => {
command = resolvedConfig.command; command = resolvedConfig.command;
@ -25,6 +25,15 @@ export function keycloakify(): Plugin {
urlPathname = (() => { urlPathname = (() => {
let out = resolvedConfig.env.BASE_URL; 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) { if (out === undefined) {
return 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"); const isTypeScriptFile = id.endsWith(".ts") || id.endsWith(".tsx");
if (!isTypeScriptFile && !isJavascriptFile) { if (!isTypeScriptFile && !isJavascriptFile) {
@ -101,8 +109,6 @@ export function keycloakify(): Plugin {
} }
} }
const windowToken = isJavascriptFile ? "window" : "(window as any)";
if (transformedCode === undefined) { if (transformedCode === undefined) {
transformedCode = code; transformedCode = code;
} }
@ -112,9 +118,9 @@ export function keycloakify(): Plugin {
"import.meta.env.BASE_URL", "import.meta.env.BASE_URL",
[ [
`(`, `(`,
`(${windowToken}.${nameOfTheGlobal} === undefined || import.meta.env.MODE === "development") ?`, `(window.${nameOfTheGlobal} === undefined || import.meta.env.MODE === "development")?`,
`"${urlPathname ?? "/"}":`, `"${urlPathname ?? "/"}":`,
` \`\${${windowToken}.${nameOfTheGlobal}.url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/\``, `(window.${nameOfTheGlobal}.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/")`,
`)` `)`
].join("") ].join("")
); );
@ -128,7 +134,7 @@ export function keycloakify(): Plugin {
"code": transformedCode "code": transformedCode
}; };
}, },
"buildEnd": async () => { "closeBundle": async () => {
assert(command !== undefined); assert(command !== undefined);
if (command !== "build") { if (command !== "build") {
@ -139,5 +145,7 @@ export function keycloakify(): Plugin {
await rm(pathJoin(buildDirPath, keycloak_resources), { "recursive": true, "force": true }); await rm(pathJoin(buildDirPath, keycloak_resources), { "recursive": true, "force": true });
} }
}; } satisfies Plugin;
return plugin as any;
} }