diff --git a/src/dev/deps.ts b/src/dev/deps.ts index d4da586b..218ac838 100644 --- a/src/dev/deps.ts +++ b/src/dev/deps.ts @@ -4,7 +4,9 @@ export { extname, fromFileUrl, join, + relative, resolve, + SEP, toFileUrl, } from "https://deno.land/std@0.189.0/path/mod.ts"; export { walk } from "https://deno.land/std@0.189.0/fs/walk.ts"; diff --git a/src/dev/mod.ts b/src/dev/mod.ts index f9329dc5..871fa3e0 100644 --- a/src/dev/mod.ts +++ b/src/dev/mod.ts @@ -1,12 +1,4 @@ -import { - dirname, - extname, - fromFileUrl, - gte, - join, - toFileUrl, - walk, -} from "./deps.ts"; +import { dirname, fromFileUrl, gte, join, toFileUrl, walk } from "./deps.ts"; import { error } from "./error.ts"; const MIN_DENO_VERSION = "1.25.0"; @@ -28,65 +20,34 @@ export function ensureMinDenoVersion() { } } +async function collectDir(dir: string): Promise { + const dirUrl = toFileUrl(dir); + const paths = []; + const routesFolder = walk(dir, { + includeDirs: false, + includeFiles: true, + exts: ["tsx", "jsx", "ts", "js"], + }); + for await (const entry of routesFolder) { + const path = toFileUrl(entry.path).href.substring( + dirUrl.href.length, + ); + paths.push(path); + } + paths.sort(); + return paths; +} + interface Manifest { routes: string[]; islands: string[]; } export async function collect(directory: string): Promise { - const routesDir = join(directory, "./routes"); - const islandsDir = join(directory, "./islands"); - - const routes = []; - try { - const routesUrl = toFileUrl(routesDir); - const routesFolder = walk(routesDir, { - includeDirs: false, - includeFiles: true, - exts: ["tsx", "jsx", "ts", "js"], - }); - for await (const entry of routesFolder) { - if (entry.isFile) { - const file = toFileUrl(entry.path).href.substring( - routesUrl.href.length, - ); - routes.push(file); - } - } - } catch (err) { - if (err.cause instanceof Deno.errors.NotFound) { - // Do nothing. - } else { - throw err; - } - } - routes.sort(); - - const islands = []; - try { - const islandsUrl = toFileUrl(islandsDir); - for await (const entry of Deno.readDir(islandsDir)) { - if (entry.isDirectory) { - error( - `Found subdirectory '${entry.name}' in islands/. The islands/ folder must not contain any subdirectories.`, - ); - } - if (entry.isFile) { - const ext = extname(entry.name); - if (![".tsx", ".jsx", ".ts", ".js"].includes(ext)) continue; - const path = join(islandsDir, entry.name); - const file = toFileUrl(path).href.substring(islandsUrl.href.length); - islands.push(file); - } - } - } catch (err) { - if (err instanceof Deno.errors.NotFound) { - // Do nothing. - } else { - throw err; - } - } - islands.sort(); + const [routes, islands] = await Promise.all([ + collectDir(join(directory, "./routes")), + collectDir(join(directory, "./islands")), + ]); return { routes, islands }; } diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index fee480d5..8b737694 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -40,7 +40,8 @@ import * as $$1 from "./islands/ReturningNull.tsx"; import * as $$2 from "./islands/RootFragment.tsx"; import * as $$3 from "./islands/RootFragmentWithConditionalFirst.tsx"; import * as $$4 from "./islands/Test.tsx"; -import * as $$5 from "./islands/kebab-case-counter-test.tsx"; +import * as $$5 from "./islands/folder/Counter.tsx"; +import * as $$6 from "./islands/kebab-case-counter-test.tsx"; const manifest = { routes: { @@ -83,7 +84,8 @@ const manifest = { "./islands/RootFragment.tsx": $$2, "./islands/RootFragmentWithConditionalFirst.tsx": $$3, "./islands/Test.tsx": $$4, - "./islands/kebab-case-counter-test.tsx": $$5, + "./islands/folder/Counter.tsx": $$5, + "./islands/kebab-case-counter-test.tsx": $$6, }, baseUrl: import.meta.url, config, diff --git a/tests/fixture/islands/folder/Counter.tsx b/tests/fixture/islands/folder/Counter.tsx new file mode 100644 index 00000000..953a2a60 --- /dev/null +++ b/tests/fixture/islands/folder/Counter.tsx @@ -0,0 +1,23 @@ +import { useState } from "preact/hooks"; +import { IS_BROWSER } from "$fresh/runtime.ts"; + +interface CounterProps { + start: number; + id: string; +} + +export default function Counter(props: CounterProps) { + 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 a0dfb4d1..29dc63b7 100644 --- a/tests/fixture/routes/islands/index.tsx +++ b/tests/fixture/routes/islands/index.tsx @@ -1,4 +1,5 @@ import Counter from "../../islands/Counter.tsx"; +import FolderCounter from "../../islands/folder/Counter.tsx"; import KebabCaseFileNameTest from "../../islands/kebab-case-counter-test.tsx"; import Test from "../../islands/Test.tsx"; @@ -7,6 +8,7 @@ export default function Home() {
+ `} /> diff --git a/tests/islands_test.ts b/tests/islands_test.ts index d95db1be..3e363865 100644 --- a/tests/islands_test.ts +++ b/tests/islands_test.ts @@ -30,9 +30,10 @@ Deno.test({ waitUntil: "networkidle2", }); - await t.step("Ensure 3 islands on 1 page are revived", async () => { + await t.step("Ensure 4 islands on 1 page are revived", async () => { await counterTest("counter1", 3); await counterTest("counter2", 10); + await counterTest("folder-counter", 3); await counterTest("kebab-case-file-counter", 5); }); @@ -103,7 +104,6 @@ async function didServerStart( return true; } } - return false; }