remove need for export function self

This commit is contained in:
Luca Casonato
2021-05-08 03:12:56 +02:00
parent 303f4fd140
commit 9145711e2e
6 changed files with 12 additions and 15 deletions
+4 -1
View File
@@ -21,7 +21,10 @@ const output = `import { setup } from "../server.ts";
${files.map((file, i) => `import * as $${i} from "./pages${file}";`).join("\n")}
setup([${files.map((_, i) => `$${i}`).join(", ")}], import.meta.url);
setup([${
files.map((file, i) => `[$${i}, "./pages${file}"]`)
.join(", ")
}], import.meta.url);
`;
Deno.writeTextFile("./server.ts", output);
-4
View File
@@ -10,7 +10,3 @@ export default function Home(props: PageProps) {
</div>
);
}
export function self() {
return import.meta.url;
}
-4
View File
@@ -3,7 +3,3 @@ import { h } from "../deps.ts";
export default function Home(props: {}) {
return <div>Hello World!</div>;
}
export function self() {
return import.meta.url;
}
+1 -1
View File
@@ -3,4 +3,4 @@ import { setup } from "../server.ts";
import * as $0 from "./pages/[name].tsx";
import * as $1 from "./pages/index.tsx";
setup([$0, $1], import.meta.url);
setup([[$0, "./pages/[name].tsx"], [$1, "./pages/index.tsx"]], import.meta.url);
+1 -1
View File
@@ -1,7 +1,7 @@
import { createPages, PageModules } from "./routes.ts";
import { createServer } from "./server.ts";
export function setup(pageModules: PageModules[], selfUrl: string) {
export function setup(pageModules: [PageModules, string][], selfUrl: string) {
const baseUrl = new URL("./", selfUrl).href;
const pages = createPages(pageModules, baseUrl);
const app = createServer(pages);
+6 -4
View File
@@ -4,7 +4,6 @@ import { PageProps } from "../runtime/types.ts";
export interface PageModules {
default: rt.ComponentType<PageProps>;
self: () => string;
}
export interface Page {
@@ -14,10 +13,13 @@ export interface Page {
component: rt.ComponentType<PageProps>;
}
export function createPages(pageModules: PageModules[], baseUrl: string): Page[] {
export function createPages(
pageModules: [PageModules, string][],
baseUrl: string,
): Page[] {
const pages: Page[] = [];
for (const pageModule of pageModules) {
const url = pageModule.self();
for (const [pageModule, self] of pageModules) {
const url = new URL(self, baseUrl).href;
if (!url.startsWith(baseUrl)) {
throw new TypeError("Page is not a child of the basepath.");
}