init
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
export * from "https://x.lcas.dev/preact@10.5.12/mod.js";
|
||||
export type {
|
||||
CreateHandle,
|
||||
EffectCallback,
|
||||
Inputs,
|
||||
PropRef,
|
||||
Reducer,
|
||||
Ref,
|
||||
StateUpdater,
|
||||
} from "https://x.lcas.dev/preact@10.5.12/hooks.js";
|
||||
export {
|
||||
useCallback,
|
||||
useContext,
|
||||
useDebugValue,
|
||||
useEffect,
|
||||
useErrorBoundary,
|
||||
useImperativeHandle,
|
||||
useLayoutEffect,
|
||||
useMemo,
|
||||
useReducer,
|
||||
useRef,
|
||||
useState,
|
||||
} from "https://x.lcas.dev/preact@10.5.12/hooks.js";
|
||||
@@ -0,0 +1,3 @@
|
||||
export interface PageProps {
|
||||
params: Record<string, string | string[]>;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { denoPlugin, esbuild } from "./deps.ts";
|
||||
import { Page } from "./routes.ts";
|
||||
|
||||
let esbuildInitalized: boolean | Promise<void> = false;
|
||||
async function ensureEsbuildInialized() {
|
||||
if (esbuildInitalized === false) {
|
||||
esbuildInitalized = await esbuild.initialize({
|
||||
wasmURL: "https://unpkg.com/esbuild-wasm@0.11.19/esbuild.wasm",
|
||||
worker: false,
|
||||
});
|
||||
await esbuildInitalized;
|
||||
esbuildInitalized = true;
|
||||
} else if (esbuildInitalized instanceof Promise) {
|
||||
await esbuildInitalized;
|
||||
}
|
||||
}
|
||||
|
||||
export async function bundle(page: Page): Promise<string> {
|
||||
const runtime = new URL("../../runtime.ts", import.meta.url);
|
||||
const contents = `
|
||||
import Page from "${page.url}";
|
||||
import { h, render } from "${runtime.href}";
|
||||
|
||||
addEventListener("DOMContentLoaded", () => {
|
||||
const props = JSON.parse(document.getElementById("__FRSH_PROPS").textContent);
|
||||
render(h(Page, props), document.body);
|
||||
});
|
||||
`;
|
||||
await ensureEsbuildInialized();
|
||||
const bundle = await esbuild.build({
|
||||
plugins: [denoPlugin({ loader: "portable" })],
|
||||
write: false,
|
||||
bundle: true,
|
||||
minify: true,
|
||||
platform: "neutral",
|
||||
outfile: "",
|
||||
jsxFactory: "h",
|
||||
jsxFragment: "Fragment",
|
||||
stdin: {
|
||||
contents,
|
||||
sourcefile: `fresh://entrypoint/${page.name}`,
|
||||
},
|
||||
});
|
||||
return bundle.outputFiles[0].text;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// -- preact --
|
||||
export { renderToString } from "https://x.lcas.dev/preact@10.5.12/ssr.js";
|
||||
|
||||
// -- std --
|
||||
export { extname } from "https://deno.land/std@0.95.0/path/mod.ts";
|
||||
|
||||
// -- oak --
|
||||
export * as oak from "https://deno.land/x/oak@v7.4.0/mod.ts";
|
||||
|
||||
// -- esbuild --
|
||||
import * as esbuild from "https://cdn.skypack.dev/esbuild-wasm?dts";
|
||||
export { esbuild };
|
||||
export { denoPlugin } from "https://raw.githubusercontent.com/lucacasonato/esbuild_deno_loader/c5ee642f4552078324badbf1b541c7222c07d5ff/mod.ts";
|
||||
@@ -0,0 +1,13 @@
|
||||
import { createPages, PageModules } from "./routes.ts";
|
||||
import { createServer } from "./server.ts";
|
||||
|
||||
export function setup(pageModules: PageModules[], selfUrl: string) {
|
||||
const baseUrl = new URL("./", selfUrl).href;
|
||||
const pages = createPages(pageModules, baseUrl);
|
||||
const app = createServer(pages);
|
||||
app.addEventListener("error", (err) => {
|
||||
console.error(err.message);
|
||||
});
|
||||
|
||||
addEventListener("fetch", app.fetchEventHandler());
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { renderToString } from "./deps.ts";
|
||||
import * as rt from "../runtime/deps.ts";
|
||||
import { Page } from "./routes.ts";
|
||||
|
||||
export function render(page: Page, params: Record<string, string>): string {
|
||||
const props = { params };
|
||||
const body = renderToString(rt.h(page.component, props));
|
||||
return `<!DOCTYPE html><html><head><script src="/_frsh/s/p/${page.name}.module.js" type="module"></script></head><body>${body}<script id="__FRSH_PROPS" type="application/json">${
|
||||
JSON.stringify(props)
|
||||
}</script></body></html>`;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { extname } from "./deps.ts";
|
||||
import * as rt from "../runtime/deps.ts";
|
||||
import { PageProps } from "../runtime/types.ts";
|
||||
|
||||
export interface PageModules {
|
||||
default: rt.ComponentType<PageProps>;
|
||||
self: () => string;
|
||||
}
|
||||
|
||||
export interface Page {
|
||||
route: string;
|
||||
url: string;
|
||||
name: string;
|
||||
component: rt.ComponentType<PageProps>;
|
||||
}
|
||||
|
||||
export function createPages(pageModules: PageModules[], baseUrl: string): Page[] {
|
||||
const pages: Page[] = [];
|
||||
for (const pageModule of pageModules) {
|
||||
const url = pageModule.self();
|
||||
if (!url.startsWith(baseUrl)) {
|
||||
throw new TypeError("Page is not a child of the basepath.");
|
||||
}
|
||||
const path = url.substring(baseUrl.length).substring("pages".length);
|
||||
const name = path.substring(1, path.length - extname(path).length);
|
||||
const route = pathToRoute(name);
|
||||
const page: Page = {
|
||||
route,
|
||||
url,
|
||||
name: name.replace("/", "-"),
|
||||
component: pageModule.default,
|
||||
};
|
||||
pages.push(page);
|
||||
}
|
||||
|
||||
pages.sort((a, b) => {
|
||||
const partsA = a.route.split("/");
|
||||
const partsB = b.route.split("/");
|
||||
for (let i = 0; i < Math.max(partsA.length, partsB.length); i++) {
|
||||
const partA = partsA[i];
|
||||
const partB = partsB[i];
|
||||
if (partA === undefined) return -1;
|
||||
if (partB === undefined) return 1;
|
||||
if (partA === partB) continue;
|
||||
const priorityA = partA.startsWith(":") ? partA.endsWith("*") ? 0 : 1 : 2;
|
||||
const priorityB = partB.startsWith(":") ? partB.endsWith("*") ? 0 : 1 : 2;
|
||||
return Math.max(Math.min(priorityB - priorityA, 1), -1);
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
|
||||
return pages;
|
||||
}
|
||||
|
||||
function pathToRoute(path: string): string {
|
||||
const parts = path.split("/");
|
||||
if (parts[parts.length - 1] === "index") {
|
||||
parts.pop();
|
||||
}
|
||||
const route = "/" + parts
|
||||
.map((part) => {
|
||||
if (part.startsWith("[...") && part.endsWith("]")) {
|
||||
return `:${part.slice(4, part.length - 1)}*`;
|
||||
}
|
||||
if (part.startsWith("[") && part.endsWith("]")) {
|
||||
return `:${part.slice(1, part.length - 1)}`;
|
||||
}
|
||||
return part;
|
||||
})
|
||||
.join("/");
|
||||
return route;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { oak } from "./deps.ts";
|
||||
import { bundle } from "./bundle.ts";
|
||||
import { render } from "./render.ts";
|
||||
import { Page } from "./routes.ts";
|
||||
|
||||
export function createServer(pages: Page[]): oak.Application {
|
||||
const router = new oak.Router();
|
||||
|
||||
for (const page of pages) {
|
||||
router.get<Record<string, string>>(page.route, (ctx) => {
|
||||
ctx.response.status = 200;
|
||||
ctx.response.type = "html";
|
||||
ctx.response.body = render(page, ctx.params);
|
||||
});
|
||||
|
||||
router.get(`/_frsh/s/p/${page.name}.module.js`, async (ctx) => {
|
||||
const js = await bundle(page);
|
||||
ctx.response.status = 200;
|
||||
ctx.response.type = "js";
|
||||
ctx.response.body = js;
|
||||
});
|
||||
}
|
||||
|
||||
const app = new oak.Application();
|
||||
|
||||
app.use(router.routes());
|
||||
app.use(router.allowedMethods());
|
||||
|
||||
return app;
|
||||
}
|
||||
Reference in New Issue
Block a user