fix: dots in island + route name not working (#2105)

This commit is contained in:
Marvin Hagemeister
2023-11-30 18:14:59 +01:00
committed by GitHub
parent a0483bc357
commit 054ffbb44a
7 changed files with 54 additions and 21 deletions
+2 -20
View File
@@ -1,7 +1,4 @@
import {
isIdentifierChar,
isIdentifierStart,
} from "../server/init_safe_deps.ts";
import { stringToIdentifier } from "../server/init_safe_deps.ts";
import { extname, join, normalize } from "./deps.ts";
/**
@@ -26,22 +23,7 @@ export function specifierToIdentifier(specifier: string, used: Set<string>) {
if (ext) specifier = specifier.slice(0, -ext.length);
// Turn the specifier into a readable JS identifier
let ident = "";
for (let i = 0; i < specifier.length; i++) {
const char = specifier.charCodeAt(i);
if (i === 0 && !isIdentifierStart(char)) {
ident += "_";
if (isIdentifierChar(char)) {
ident += specifier[i];
}
} else if (!isIdentifierChar(char)) {
if (ident[ident.length - 1] !== "_") {
ident += "_";
}
} else if (ident[ident.length - 1] !== "_" || specifier[i] !== "_") {
ident += specifier[i];
}
}
let ident = stringToIdentifier(specifier);
if (used.has(ident)) {
let check = ident;
+2 -1
View File
@@ -32,6 +32,7 @@ import {
} from "./deps.ts";
import { BUILD_ID } from "./build_id.ts";
import { toBaseRoute } from "./compose.ts";
import { stringToIdentifier } from "./init_safe_deps.ts";
export const ROOT_BASE_ROUTE = toBaseRoute("/");
@@ -525,7 +526,7 @@ function toPascalCase(text: string): string {
}
function sanitizeIslandName(name: string): string {
const fileName = name.replaceAll(/[/\\\\\(\)\[\]]/g, "_");
const fileName = stringToIdentifier(name);
return toPascalCase(fileName);
}
+25
View File
@@ -7,3 +7,28 @@ export {
isIdentifierChar,
isIdentifierStart,
} from "https://esm.sh/@babel/helper-validator-identifier@7.22.20";
import {
isIdentifierChar,
isIdentifierStart,
} from "https://esm.sh/@babel/helper-validator-identifier@7.22.20";
export function stringToIdentifier(str: string): string {
let ident = "";
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
if (i === 0 && !isIdentifierStart(char)) {
ident += "_";
if (isIdentifierChar(char)) {
ident += str[i];
}
} else if (!isIdentifierChar(char)) {
if (ident[ident.length - 1] !== "_") {
ident += "_";
}
} else if (ident[ident.length - 1] !== "_" || str[i] !== "_") {
ident += str[i];
}
}
return ident;
}
+4
View File
@@ -26,6 +26,7 @@ import * as $event_handler_string_island from "./routes/event_handler_string_isl
import * as $evil from "./routes/evil.tsx";
import * as $failure from "./routes/failure.ts";
import * as $foo_bar_baz from "./routes/foo.bar.baz.tsx";
import * as $foo_bar from "./routes/foo.bar.tsx";
import * as $head_deduplicate from "./routes/head_deduplicate.tsx";
import * as $hooks_server_island from "./routes/hooks-server/island.tsx";
import * as $hooks_server_useReducer from "./routes/hooks-server/useReducer.tsx";
@@ -82,6 +83,7 @@ import * as $status_overwrite from "./routes/status_overwrite.tsx";
import * as $umlaut_äöüß from "./routes/umlaut-äöüß.tsx";
import * as $wildcard from "./routes/wildcard.tsx";
import * as $Counter from "./islands/Counter.tsx";
import * as $Foo_Bar from "./islands/Foo.Bar.tsx";
import * as $FormIsland from "./islands/FormIsland.tsx";
import * as $Greeter from "./islands/Greeter.tsx";
import * as $HookIsland from "./islands/HookIsland.tsx";
@@ -127,6 +129,7 @@ const manifest = {
"./routes/evil.tsx": $evil,
"./routes/failure.ts": $failure,
"./routes/foo.bar.baz.tsx": $foo_bar_baz,
"./routes/foo.bar.tsx": $foo_bar,
"./routes/head_deduplicate.tsx": $head_deduplicate,
"./routes/hooks-server/island.tsx": $hooks_server_island,
"./routes/hooks-server/useReducer.tsx": $hooks_server_useReducer,
@@ -200,6 +203,7 @@ const manifest = {
},
islands: {
"./islands/Counter.tsx": $Counter,
"./islands/Foo.Bar.tsx": $Foo_Bar,
"./islands/FormIsland.tsx": $FormIsland,
"./islands/Greeter.tsx": $Greeter,
"./islands/HookIsland.tsx": $HookIsland,
+9
View File
@@ -0,0 +1,9 @@
import { useEffect, useState } from "preact/hooks";
export default function FooBar() {
const [css, setCss] = useState("");
useEffect(() => {
setCss("ready");
}, []);
return <h1 class={css}>FooBar island</h1>;
}
+5
View File
@@ -0,0 +1,5 @@
import FooBar from "../islands/Foo.Bar.tsx";
export default function Page() {
return <FooBar />;
}
+7
View File
@@ -1001,6 +1001,13 @@ Deno.test({
},
});
Deno.test("support dots in route + island name", async () => {
await withPageName("./tests/fixture/main.ts", async (page, address) => {
await page.goto(`${address}/foo.bar`);
await page.waitForSelector(".ready");
});
});
Deno.test("De-duplicates <Head /> nodes by key", async () => {
await withFakeServe("./tests/fixture/main.ts", async (server) => {
const res = await server.get(`/head_deduplicate`);