From 1d6e15ba36131a519965b8904e306c73f3d5b7a6 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 10 Jul 2023 14:13:01 +0200 Subject: [PATCH] fix: manifest file encoding non-latin chars (#1433) --- src/dev/deps.ts | 1 + src/dev/mod.ts | 45 +++++++++++++++++++++------- tests/fixture/fresh.gen.ts | 6 ++-- tests/fixture/routes/umlaut-äöüß.tsx | 3 ++ tests/main_test.ts | 25 ++++++++++++++++ 5 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 tests/fixture/routes/umlaut-äöüß.tsx diff --git a/src/dev/deps.ts b/src/dev/deps.ts index e6d2e1a2..0cd60bba 100644 --- a/src/dev/deps.ts +++ b/src/dev/deps.ts @@ -5,6 +5,7 @@ export { extname, fromFileUrl, join, + posix, relative, resolve, SEP, diff --git a/src/dev/mod.ts b/src/dev/mod.ts index c176363b..7c62a1ea 100644 --- a/src/dev/mod.ts +++ b/src/dev/mod.ts @@ -1,4 +1,12 @@ -import { dirname, fromFileUrl, gte, join, toFileUrl, walk } from "./deps.ts"; +import { + dirname, + fromFileUrl, + gte, + join, + posix, + relative, + walk, +} from "./deps.ts"; import { error } from "./error.ts"; const MIN_DENO_VERSION = "1.31.0"; @@ -30,7 +38,6 @@ async function collectDir(dir: string): Promise { throw err; } - const dirUrl = toFileUrl(dir); const paths = []; const routesFolder = walk(dir, { includeDirs: false, @@ -39,10 +46,7 @@ async function collectDir(dir: string): Promise { }); for await (const entry of routesFolder) { - const path = toFileUrl(entry.path).href.substring( - dirUrl.href.length, - ); - paths.push(path); + paths.push(relative(dir, entry.path)); } paths.sort(); @@ -63,6 +67,17 @@ export async function collect(directory: string): Promise { return { routes, islands }; } +/** + * Import specifiers must have forward slashes + */ +function toImportSpecifier(file: string) { + let specifier = posix.normalize(file).replace(/\\/g, "/"); + if (!specifier.startsWith(".")) { + specifier = "./" + specifier; + } + return specifier; +} + export async function generate(directory: string, manifest: Manifest) { const { routes, islands } = manifest; @@ -71,25 +86,35 @@ export async function generate(directory: string, manifest: Manifest) { // This file is automatically updated during development when running \`dev.ts\`. ${ - routes.map((file, i) => `import * as $${i} from "./routes${file}";`).join( + routes.map((file, i) => + `import * as $${i} from "${toImportSpecifier(join("routes", file))}";` + ).join( "\n", ) } ${ - islands.map((file, i) => `import * as $$${i} from "./islands${file}";`) + islands.map((file, i) => + `import * as $$${i} from "${toImportSpecifier(join("islands", file))}";` + ) .join("\n") } const manifest = { routes: { ${ - routes.map((file, i) => `${JSON.stringify(`./routes${file}`)}: $${i},`) + routes.map((file, i) => + `${JSON.stringify(`${toImportSpecifier(join("routes", file))}`)}: $${i},` + ) .join("\n ") } }, islands: { ${ - islands.map((file, i) => `${JSON.stringify(`./islands${file}`)}: $$${i},`) + islands.map((file, i) => + `${ + JSON.stringify(`${toImportSpecifier(join("islands", file))}`) + }: $$${i},` + ) .join("\n ") } }, diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index 0d222de1..71f987c3 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -38,7 +38,8 @@ import * as $32 from "./routes/state-in-props/_middleware.ts"; import * as $33 from "./routes/state-in-props/index.tsx"; import * as $34 from "./routes/static.tsx"; import * as $35 from "./routes/status_overwrite.tsx"; -import * as $36 from "./routes/wildcard.tsx"; +import * as $36 from "./routes/umlaut-äöüß.tsx"; +import * as $37 from "./routes/wildcard.tsx"; import * as $$0 from "./islands/Counter.tsx"; import * as $$1 from "./islands/MultipleCounters.tsx"; import * as $$2 from "./islands/ReturningNull.tsx"; @@ -87,7 +88,8 @@ const manifest = { "./routes/state-in-props/index.tsx": $33, "./routes/static.tsx": $34, "./routes/status_overwrite.tsx": $35, - "./routes/wildcard.tsx": $36, + "./routes/umlaut-äöüß.tsx": $36, + "./routes/wildcard.tsx": $37, }, islands: { "./islands/Counter.tsx": $$0, diff --git a/tests/fixture/routes/umlaut-äöüß.tsx b/tests/fixture/routes/umlaut-äöüß.tsx new file mode 100644 index 00000000..e5f63070 --- /dev/null +++ b/tests/fixture/routes/umlaut-äöüß.tsx @@ -0,0 +1,3 @@ +export default function Umlaut() { + return

it works

; +} diff --git a/tests/main_test.ts b/tests/main_test.ts index c86c251d..94eda071 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -879,3 +879,28 @@ Deno.test("renders error boundary", { assertEquals(text, "it works"); }); }); + +Deno.test({ + name: "Resolves routes with non-latin characters", + + async fn() { + await withPageName("./tests/fixture/main.ts", async (page, address) => { + // Check that we can navigate to the page + await page.goto(`${address}/umlaut-äöüß`); + await page.waitForSelector("h1"); + const text = await page.$eval("h1", (el) => el.textContent); + assertEquals(text, "it works"); + + // Check the manifest + const mod = (await import("./fixture/fresh.gen.ts")).default; + + assert( + "./routes/umlaut-äöüß.tsx" in mod.routes, + "Umlaut route not found", + ); + }); + }, + + sanitizeOps: false, + sanitizeResources: false, +});