From a7b198fdf49381743588ee953cf82adb68bc4a70 Mon Sep 17 00:00:00 2001 From: Stephen Delaney Date: Wed, 6 Jul 2022 11:42:25 +0100 Subject: [PATCH] feat: support kebab-case filenames for islands (#404) --- src/server/context.ts | 14 +++++++++- tests/fixture/fresh.gen.ts | 2 ++ .../islands/kebab-case-counter-test.tsx | 27 +++++++++++++++++++ tests/fixture/routes/islands/index.tsx | 2 ++ tests/islands_test.ts | 3 ++- 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tests/fixture/islands/kebab-case-counter-test.tsx diff --git a/src/server/context.ts b/src/server/context.ts index 1a4aa266..63f71983 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -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) diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index f62e7b40..959c4829 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -30,6 +30,7 @@ import * as $24 from "./routes/static.tsx"; import * as $25 from "./routes/wildcard.tsx"; import * as $$0 from "./islands/Counter.tsx"; import * as $$1 from "./islands/Test.tsx"; +import * as $$2 from "./islands/kebab-case-counter-test.tsx"; const manifest = { routes: { @@ -63,6 +64,7 @@ const manifest = { islands: { "./islands/Counter.tsx": $$0, "./islands/Test.tsx": $$1, + "./islands/kebab-case-counter-test.tsx": $$2, }, baseUrl: import.meta.url, }; diff --git a/tests/fixture/islands/kebab-case-counter-test.tsx b/tests/fixture/islands/kebab-case-counter-test.tsx new file mode 100644 index 00000000..90f16aa5 --- /dev/null +++ b/tests/fixture/islands/kebab-case-counter-test.tsx @@ -0,0 +1,27 @@ +/** @jsx h */ +import { h } from "preact"; +import { useState } from "preact/hooks"; +import { IS_BROWSER } from "$fresh/runtime.ts"; + +interface KebabCaseFileNameTestProps { + start: number; + id: string; +} + +export default function KebabCaseFileNameTest( + props: KebabCaseFileNameTestProps, +) { + const [count, setCount] = useState(props.start); + return ( +
+

{count}

+ +
+ ); +} diff --git a/tests/fixture/routes/islands/index.tsx b/tests/fixture/routes/islands/index.tsx index ab478293..527cb9c9 100644 --- a/tests/fixture/routes/islands/index.tsx +++ b/tests/fixture/routes/islands/index.tsx @@ -1,6 +1,7 @@ /** @jsx h */ import { h } from "preact"; import Counter from "../../islands/Counter.tsx"; +import KebabCaseFileNameTest from "../../islands/kebab-case-counter-test.tsx"; import Test from "../../islands/Test.tsx"; export default function Home() { @@ -8,6 +9,7 @@ export default function Home() {
+
); diff --git a/tests/islands_test.ts b/tests/islands_test.ts index 461615e6..412cbf24 100644 --- a/tests/islands_test.ts +++ b/tests/islands_test.ts @@ -53,9 +53,10 @@ Deno.test({ waitUntil: "networkidle2", }); - await t.step("Ensure 2 islands on 1 page are revived", async () => { + await t.step("Ensure 3 islands on 1 page are revived", async () => { await counterTest("counter1", 3); await counterTest("counter2", 10); + await counterTest("kebab-case-file-counter", 5); }); await t.step("Ensure an island revive an img 'hash' path", async () => {