feat: support kebab-case filenames for islands (#404)

This commit is contained in:
Stephen Delaney
2022-07-06 12:42:25 +02:00
committed by GitHub
parent 38c1a2f426
commit a7b198fdf4
5 changed files with 46 additions and 2 deletions
+13 -1
View File
@@ -196,7 +196,7 @@ export class ServerContext {
}
const path = url.substring(baseUrl.length).substring("islands".length);
const baseRoute = path.substring(1, path.length - extname(path).length);
const name = baseRoute.replace("/", "");
const name = sanitizeIslandName(baseRoute);
const id = name.toLowerCase();
if (typeof module.default !== "function") {
throw new TypeError(
@@ -676,6 +676,18 @@ function sanitizePathToRegex(path: string): string {
.replaceAll("\:", "\\:");
}
function toPascalCase(text: string): string {
return text.replace(
/(^\w|-\w)/g,
(substring) => substring.replace(/-/, "").toUpperCase(),
);
}
function sanitizeIslandName(name: string): string {
const fileName = name.replace("/", "");
return toPascalCase(fileName);
}
function serializeCSPDirectives(csp: ContentSecurityPolicyDirectives): string {
return Object.entries(csp)
.filter(([_key, value]) => value !== undefined)