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}
+ +