feat: island sub-directories (#1185)

This commit is contained in:
Asher Gomez
2023-05-31 05:29:56 +10:00
committed by GitHub
parent 8347eadbef
commit b5532aeb69
6 changed files with 56 additions and 66 deletions
+2
View File
@@ -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";
+23 -62
View File
@@ -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<string[]> {
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<Manifest> {
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 };
}
+4 -2
View File
@@ -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,
+23
View File
@@ -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 (
<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,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() {
<div>
<Counter id="counter1" start={3} />
<Counter id="counter2" start={10} />
<FolderCounter id="folder-counter" start={3} />
<KebabCaseFileNameTest id="kebab-case-file-counter" start={5} />
<Test message="" />
<Test message={`</script><script>alert('test')</script>`} />
+2 -2
View File
@@ -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;
}