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 path = url.substring(baseUrl.length).substring("islands".length);
const baseRoute = path.substring(1, path.length - extname(path).length); const baseRoute = path.substring(1, path.length - extname(path).length);
const name = baseRoute.replace("/", ""); const name = sanitizeIslandName(baseRoute);
const id = name.toLowerCase(); const id = name.toLowerCase();
if (typeof module.default !== "function") { if (typeof module.default !== "function") {
throw new TypeError( throw new TypeError(
@@ -676,6 +676,18 @@ function sanitizePathToRegex(path: string): string {
.replaceAll("\:", "\\:"); .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 { function serializeCSPDirectives(csp: ContentSecurityPolicyDirectives): string {
return Object.entries(csp) return Object.entries(csp)
.filter(([_key, value]) => value !== undefined) .filter(([_key, value]) => value !== undefined)
+2
View File
@@ -30,6 +30,7 @@ import * as $24 from "./routes/static.tsx";
import * as $25 from "./routes/wildcard.tsx"; import * as $25 from "./routes/wildcard.tsx";
import * as $$0 from "./islands/Counter.tsx"; import * as $$0 from "./islands/Counter.tsx";
import * as $$1 from "./islands/Test.tsx"; import * as $$1 from "./islands/Test.tsx";
import * as $$2 from "./islands/kebab-case-counter-test.tsx";
const manifest = { const manifest = {
routes: { routes: {
@@ -63,6 +64,7 @@ const manifest = {
islands: { islands: {
"./islands/Counter.tsx": $$0, "./islands/Counter.tsx": $$0,
"./islands/Test.tsx": $$1, "./islands/Test.tsx": $$1,
"./islands/kebab-case-counter-test.tsx": $$2,
}, },
baseUrl: import.meta.url, baseUrl: import.meta.url,
}; };
@@ -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 (
<div id={props.id}>
<p>{count}</p>
<button
id={`b-${props.id}`}
onClick={() => setCount(count + 1)}
disabled={!IS_BROWSER}
>
+1
</button>
</div>
);
}
+2
View File
@@ -1,6 +1,7 @@
/** @jsx h */ /** @jsx h */
import { h } from "preact"; import { h } from "preact";
import Counter from "../../islands/Counter.tsx"; import Counter from "../../islands/Counter.tsx";
import KebabCaseFileNameTest from "../../islands/kebab-case-counter-test.tsx";
import Test from "../../islands/Test.tsx"; import Test from "../../islands/Test.tsx";
export default function Home() { export default function Home() {
@@ -8,6 +9,7 @@ export default function Home() {
<div> <div>
<Counter id="counter1" start={3} /> <Counter id="counter1" start={3} />
<Counter id="counter2" start={10} /> <Counter id="counter2" start={10} />
<KebabCaseFileNameTest id="kebab-case-file-counter" start={5} />
<Test message="" /> <Test message="" />
</div> </div>
); );
+2 -1
View File
@@ -53,9 +53,10 @@ Deno.test({
waitUntil: "networkidle2", 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("counter1", 3);
await counterTest("counter2", 10); await counterTest("counter2", 10);
await counterTest("kebab-case-file-counter", 5);
}); });
await t.step("Ensure an island revive an img 'hash' path", async () => { await t.step("Ensure an island revive an img 'hash' path", async () => {